1*b0d29bc4SBrooks Davis // Copyright 2010 The Kyua Authors. 2*b0d29bc4SBrooks Davis // All rights reserved. 3*b0d29bc4SBrooks Davis // 4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without 5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are 6*b0d29bc4SBrooks Davis // met: 7*b0d29bc4SBrooks Davis // 8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright 9*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer. 10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright 11*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer in the 12*b0d29bc4SBrooks Davis // documentation and/or other materials provided with the distribution. 13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors 14*b0d29bc4SBrooks Davis // may be used to endorse or promote products derived from this software 15*b0d29bc4SBrooks Davis // without specific prior written permission. 16*b0d29bc4SBrooks Davis // 17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*b0d29bc4SBrooks Davis 29*b0d29bc4SBrooks Davis /// \file utils/cmdline/base_command.hpp 30*b0d29bc4SBrooks Davis /// Provides the utils::cmdline::base_command class. 31*b0d29bc4SBrooks Davis 32*b0d29bc4SBrooks Davis #if !defined(UTILS_CMDLINE_BASE_COMMAND_HPP) 33*b0d29bc4SBrooks Davis #define UTILS_CMDLINE_BASE_COMMAND_HPP 34*b0d29bc4SBrooks Davis 35*b0d29bc4SBrooks Davis #include "utils/cmdline/base_command_fwd.hpp" 36*b0d29bc4SBrooks Davis 37*b0d29bc4SBrooks Davis #include <string> 38*b0d29bc4SBrooks Davis 39*b0d29bc4SBrooks Davis #include "utils/cmdline/options_fwd.hpp" 40*b0d29bc4SBrooks Davis #include "utils/cmdline/parser_fwd.hpp" 41*b0d29bc4SBrooks Davis #include "utils/cmdline/ui_fwd.hpp" 42*b0d29bc4SBrooks Davis #include "utils/noncopyable.hpp" 43*b0d29bc4SBrooks Davis 44*b0d29bc4SBrooks Davis namespace utils { 45*b0d29bc4SBrooks Davis namespace cmdline { 46*b0d29bc4SBrooks Davis 47*b0d29bc4SBrooks Davis 48*b0d29bc4SBrooks Davis /// Prototype class for the implementation of subcommands of a program. 49*b0d29bc4SBrooks Davis /// 50*b0d29bc4SBrooks Davis /// Use the subclasses of command_proto defined in this module instead of 51*b0d29bc4SBrooks Davis /// command_proto itself as base classes for your application-specific 52*b0d29bc4SBrooks Davis /// commands. 53*b0d29bc4SBrooks Davis class command_proto : noncopyable { 54*b0d29bc4SBrooks Davis /// The user-visible name of the command. 55*b0d29bc4SBrooks Davis const std::string _name; 56*b0d29bc4SBrooks Davis 57*b0d29bc4SBrooks Davis /// Textual description of the command arguments. 58*b0d29bc4SBrooks Davis const std::string _arg_list; 59*b0d29bc4SBrooks Davis 60*b0d29bc4SBrooks Davis /// The minimum number of required arguments. 61*b0d29bc4SBrooks Davis const int _min_args; 62*b0d29bc4SBrooks Davis 63*b0d29bc4SBrooks Davis /// The maximum number of allowed arguments; -1 for infinity. 64*b0d29bc4SBrooks Davis const int _max_args; 65*b0d29bc4SBrooks Davis 66*b0d29bc4SBrooks Davis /// A textual description of the command. 67*b0d29bc4SBrooks Davis const std::string _short_description; 68*b0d29bc4SBrooks Davis 69*b0d29bc4SBrooks Davis /// Collection of command-specific options. 70*b0d29bc4SBrooks Davis options_vector _options; 71*b0d29bc4SBrooks Davis 72*b0d29bc4SBrooks Davis void add_option_ptr(const base_option*); 73*b0d29bc4SBrooks Davis 74*b0d29bc4SBrooks Davis protected: 75*b0d29bc4SBrooks Davis template< typename Option > void add_option(const Option&); 76*b0d29bc4SBrooks Davis parsed_cmdline parse_cmdline(const args_vector&) const; 77*b0d29bc4SBrooks Davis 78*b0d29bc4SBrooks Davis public: 79*b0d29bc4SBrooks Davis command_proto(const std::string&, const std::string&, const int, const int, 80*b0d29bc4SBrooks Davis const std::string&); 81*b0d29bc4SBrooks Davis virtual ~command_proto(void); 82*b0d29bc4SBrooks Davis 83*b0d29bc4SBrooks Davis const std::string& name(void) const; 84*b0d29bc4SBrooks Davis const std::string& arg_list(void) const; 85*b0d29bc4SBrooks Davis const std::string& short_description(void) const; 86*b0d29bc4SBrooks Davis const options_vector& options(void) const; 87*b0d29bc4SBrooks Davis }; 88*b0d29bc4SBrooks Davis 89*b0d29bc4SBrooks Davis 90*b0d29bc4SBrooks Davis /// Unparametrized base subcommand for a program. 91*b0d29bc4SBrooks Davis /// 92*b0d29bc4SBrooks Davis /// Use this class to define subcommands for your program that do not need any 93*b0d29bc4SBrooks Davis /// information passed in from the main command-line dispatcher other than the 94*b0d29bc4SBrooks Davis /// command-line arguments. 95*b0d29bc4SBrooks Davis class base_command_no_data : public command_proto { 96*b0d29bc4SBrooks Davis /// Main code of the command. 97*b0d29bc4SBrooks Davis /// 98*b0d29bc4SBrooks Davis /// This is called from main() after the command line has been processed and 99*b0d29bc4SBrooks Davis /// validated. 100*b0d29bc4SBrooks Davis /// 101*b0d29bc4SBrooks Davis /// \param ui Object to interact with the I/O of the command. The command 102*b0d29bc4SBrooks Davis /// must always use this object to write to stdout and stderr. 103*b0d29bc4SBrooks Davis /// \param cmdline The parsed command line, containing the values of any 104*b0d29bc4SBrooks Davis /// given options and arguments. 105*b0d29bc4SBrooks Davis /// 106*b0d29bc4SBrooks Davis /// \return The exit code that the program has to return. 0 on success, 107*b0d29bc4SBrooks Davis /// some other value on error. 108*b0d29bc4SBrooks Davis /// 109*b0d29bc4SBrooks Davis /// \throw std::runtime_error Any errors detected during the execution of 110*b0d29bc4SBrooks Davis /// the command are reported by means of exceptions. 111*b0d29bc4SBrooks Davis virtual int run(ui* ui, const parsed_cmdline& cmdline) = 0; 112*b0d29bc4SBrooks Davis 113*b0d29bc4SBrooks Davis public: 114*b0d29bc4SBrooks Davis base_command_no_data(const std::string&, const std::string&, const int, 115*b0d29bc4SBrooks Davis const int, const std::string&); 116*b0d29bc4SBrooks Davis 117*b0d29bc4SBrooks Davis int main(ui*, const args_vector&); 118*b0d29bc4SBrooks Davis }; 119*b0d29bc4SBrooks Davis 120*b0d29bc4SBrooks Davis 121*b0d29bc4SBrooks Davis /// Parametrized base subcommand for a program. 122*b0d29bc4SBrooks Davis /// 123*b0d29bc4SBrooks Davis /// Use this class to define subcommands for your program that need some kind of 124*b0d29bc4SBrooks Davis /// runtime information passed in from the main command-line dispatcher. 125*b0d29bc4SBrooks Davis /// 126*b0d29bc4SBrooks Davis /// \param Data The type of the object passed to the subcommand at runtime. 127*b0d29bc4SBrooks Davis /// This is useful, for example, to pass around the runtime configuration of the 128*b0d29bc4SBrooks Davis /// program. 129*b0d29bc4SBrooks Davis template< typename Data > 130*b0d29bc4SBrooks Davis class base_command : public command_proto { 131*b0d29bc4SBrooks Davis /// Main code of the command. 132*b0d29bc4SBrooks Davis /// 133*b0d29bc4SBrooks Davis /// This is called from main() after the command line has been processed and 134*b0d29bc4SBrooks Davis /// validated. 135*b0d29bc4SBrooks Davis /// 136*b0d29bc4SBrooks Davis /// \param ui Object to interact with the I/O of the command. The command 137*b0d29bc4SBrooks Davis /// must always use this object to write to stdout and stderr. 138*b0d29bc4SBrooks Davis /// \param cmdline The parsed command line, containing the values of any 139*b0d29bc4SBrooks Davis /// given options and arguments. 140*b0d29bc4SBrooks Davis /// \param data An instance of the runtime data passed from main(). 141*b0d29bc4SBrooks Davis /// 142*b0d29bc4SBrooks Davis /// \return The exit code that the program has to return. 0 on success, 143*b0d29bc4SBrooks Davis /// some other value on error. 144*b0d29bc4SBrooks Davis /// 145*b0d29bc4SBrooks Davis /// \throw std::runtime_error Any errors detected during the execution of 146*b0d29bc4SBrooks Davis /// the command are reported by means of exceptions. 147*b0d29bc4SBrooks Davis virtual int run(ui* ui, const parsed_cmdline& cmdline, 148*b0d29bc4SBrooks Davis const Data& data) = 0; 149*b0d29bc4SBrooks Davis 150*b0d29bc4SBrooks Davis public: 151*b0d29bc4SBrooks Davis base_command(const std::string&, const std::string&, const int, const int, 152*b0d29bc4SBrooks Davis const std::string&); 153*b0d29bc4SBrooks Davis 154*b0d29bc4SBrooks Davis int main(ui*, const args_vector&, const Data&); 155*b0d29bc4SBrooks Davis }; 156*b0d29bc4SBrooks Davis 157*b0d29bc4SBrooks Davis 158*b0d29bc4SBrooks Davis } // namespace cmdline 159*b0d29bc4SBrooks Davis } // namespace utils 160*b0d29bc4SBrooks Davis 161*b0d29bc4SBrooks Davis 162*b0d29bc4SBrooks Davis #endif // !defined(UTILS_CMDLINE_BASE_COMMAND_HPP) 163