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