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