1*6b3a42afSjmmv // Copyright 2010 Google Inc.
2*6b3a42afSjmmv // All rights reserved.
3*6b3a42afSjmmv //
4*6b3a42afSjmmv // Redistribution and use in source and binary forms, with or without
5*6b3a42afSjmmv // modification, are permitted provided that the following conditions are
6*6b3a42afSjmmv // met:
7*6b3a42afSjmmv //
8*6b3a42afSjmmv // * Redistributions of source code must retain the above copyright
9*6b3a42afSjmmv // notice, this list of conditions and the following disclaimer.
10*6b3a42afSjmmv // * Redistributions in binary form must reproduce the above copyright
11*6b3a42afSjmmv // notice, this list of conditions and the following disclaimer in the
12*6b3a42afSjmmv // documentation and/or other materials provided with the distribution.
13*6b3a42afSjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*6b3a42afSjmmv // may be used to endorse or promote products derived from this software
15*6b3a42afSjmmv // without specific prior written permission.
16*6b3a42afSjmmv //
17*6b3a42afSjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*6b3a42afSjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*6b3a42afSjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*6b3a42afSjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*6b3a42afSjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*6b3a42afSjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*6b3a42afSjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*6b3a42afSjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*6b3a42afSjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*6b3a42afSjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*6b3a42afSjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*6b3a42afSjmmv
29*6b3a42afSjmmv #include "utils/cmdline/base_command.hpp"
30*6b3a42afSjmmv
31*6b3a42afSjmmv #include "utils/cmdline/exceptions.hpp"
32*6b3a42afSjmmv #include "utils/cmdline/options.hpp"
33*6b3a42afSjmmv #include "utils/sanity.hpp"
34*6b3a42afSjmmv
35*6b3a42afSjmmv namespace cmdline = utils::cmdline;
36*6b3a42afSjmmv
37*6b3a42afSjmmv
38*6b3a42afSjmmv /// Creates a new command.
39*6b3a42afSjmmv ///
40*6b3a42afSjmmv /// \param name_ The name of the command. Must be unique within the context of
41*6b3a42afSjmmv /// a program and have no spaces.
42*6b3a42afSjmmv /// \param arg_list_ A textual description of the arguments received by the
43*6b3a42afSjmmv /// command. May be empty.
44*6b3a42afSjmmv /// \param min_args_ The minimum number of arguments required by the command.
45*6b3a42afSjmmv /// \param max_args_ The maximum number of arguments required by the command.
46*6b3a42afSjmmv /// -1 means infinity.
47*6b3a42afSjmmv /// \param short_description_ A description of the purpose of the command.
command_proto(const std::string & name_,const std::string & arg_list_,const int min_args_,const int max_args_,const std::string & short_description_)48*6b3a42afSjmmv cmdline::command_proto::command_proto(const std::string& name_,
49*6b3a42afSjmmv const std::string& arg_list_,
50*6b3a42afSjmmv const int min_args_,
51*6b3a42afSjmmv const int max_args_,
52*6b3a42afSjmmv const std::string& short_description_) :
53*6b3a42afSjmmv _name(name_),
54*6b3a42afSjmmv _arg_list(arg_list_),
55*6b3a42afSjmmv _min_args(min_args_),
56*6b3a42afSjmmv _max_args(max_args_),
57*6b3a42afSjmmv _short_description(short_description_)
58*6b3a42afSjmmv {
59*6b3a42afSjmmv PRE(name_.find(' ') == std::string::npos);
60*6b3a42afSjmmv PRE(max_args_ == -1 || min_args_ <= max_args_);
61*6b3a42afSjmmv }
62*6b3a42afSjmmv
63*6b3a42afSjmmv
64*6b3a42afSjmmv /// Destructor for a command.
~command_proto(void)65*6b3a42afSjmmv cmdline::command_proto::~command_proto(void)
66*6b3a42afSjmmv {
67*6b3a42afSjmmv for (options_vector::const_iterator iter = _options.begin();
68*6b3a42afSjmmv iter != _options.end(); iter++)
69*6b3a42afSjmmv delete *iter;
70*6b3a42afSjmmv }
71*6b3a42afSjmmv
72*6b3a42afSjmmv
73*6b3a42afSjmmv /// Internal method to register a dynamically-allocated option.
74*6b3a42afSjmmv ///
75*6b3a42afSjmmv /// Always use add_option() from subclasses to add options.
76*6b3a42afSjmmv ///
77*6b3a42afSjmmv /// \param option_ The option to add. Must have been dynamically allocated.
78*6b3a42afSjmmv /// This grabs ownership of the pointer, which is released when the command
79*6b3a42afSjmmv /// is destroyed.
80*6b3a42afSjmmv void
add_option_ptr(const cmdline::base_option * option_)81*6b3a42afSjmmv cmdline::command_proto::add_option_ptr(const cmdline::base_option* option_)
82*6b3a42afSjmmv {
83*6b3a42afSjmmv try {
84*6b3a42afSjmmv _options.push_back(option_);
85*6b3a42afSjmmv } catch (...) {
86*6b3a42afSjmmv delete option_;
87*6b3a42afSjmmv throw;
88*6b3a42afSjmmv }
89*6b3a42afSjmmv }
90*6b3a42afSjmmv
91*6b3a42afSjmmv
92*6b3a42afSjmmv /// Processes the command line based on the command description.
93*6b3a42afSjmmv ///
94*6b3a42afSjmmv /// \param args The raw command line to be processed.
95*6b3a42afSjmmv ///
96*6b3a42afSjmmv /// \return An object containing the list of options and free arguments found in
97*6b3a42afSjmmv /// args.
98*6b3a42afSjmmv ///
99*6b3a42afSjmmv /// \throw cmdline::usage_error If there is a problem processing the command
100*6b3a42afSjmmv /// line. This error is caused by invalid input from the user.
101*6b3a42afSjmmv cmdline::parsed_cmdline
parse_cmdline(const cmdline::args_vector & args) const102*6b3a42afSjmmv cmdline::command_proto::parse_cmdline(const cmdline::args_vector& args) const
103*6b3a42afSjmmv {
104*6b3a42afSjmmv PRE(name() == args[0]);
105*6b3a42afSjmmv const parsed_cmdline cmdline = cmdline::parse(args, options());
106*6b3a42afSjmmv
107*6b3a42afSjmmv const int argc = cmdline.arguments().size();
108*6b3a42afSjmmv if (argc < _min_args)
109*6b3a42afSjmmv throw usage_error("Not enough arguments");
110*6b3a42afSjmmv if (_max_args != -1 && argc > _max_args)
111*6b3a42afSjmmv throw usage_error("Too many arguments");
112*6b3a42afSjmmv
113*6b3a42afSjmmv return cmdline;
114*6b3a42afSjmmv }
115*6b3a42afSjmmv
116*6b3a42afSjmmv
117*6b3a42afSjmmv /// Gets the name of the command.
118*6b3a42afSjmmv ///
119*6b3a42afSjmmv /// \return The command name.
120*6b3a42afSjmmv const std::string&
name(void) const121*6b3a42afSjmmv cmdline::command_proto::name(void) const
122*6b3a42afSjmmv {
123*6b3a42afSjmmv return _name;
124*6b3a42afSjmmv }
125*6b3a42afSjmmv
126*6b3a42afSjmmv
127*6b3a42afSjmmv /// Gets the textual representation of the arguments list.
128*6b3a42afSjmmv ///
129*6b3a42afSjmmv /// \return The description of the arguments list.
130*6b3a42afSjmmv const std::string&
arg_list(void) const131*6b3a42afSjmmv cmdline::command_proto::arg_list(void) const
132*6b3a42afSjmmv {
133*6b3a42afSjmmv return _arg_list;
134*6b3a42afSjmmv }
135*6b3a42afSjmmv
136*6b3a42afSjmmv
137*6b3a42afSjmmv /// Gets the description of the purpose of the command.
138*6b3a42afSjmmv ///
139*6b3a42afSjmmv /// \return The description of the command.
140*6b3a42afSjmmv const std::string&
short_description(void) const141*6b3a42afSjmmv cmdline::command_proto::short_description(void) const
142*6b3a42afSjmmv {
143*6b3a42afSjmmv return _short_description;
144*6b3a42afSjmmv }
145*6b3a42afSjmmv
146*6b3a42afSjmmv
147*6b3a42afSjmmv /// Gets the definition of the options accepted by the command.
148*6b3a42afSjmmv ///
149*6b3a42afSjmmv /// \return The list of options.
150*6b3a42afSjmmv const cmdline::options_vector&
options(void) const151*6b3a42afSjmmv cmdline::command_proto::options(void) const
152*6b3a42afSjmmv {
153*6b3a42afSjmmv return _options;
154*6b3a42afSjmmv }
155*6b3a42afSjmmv
156*6b3a42afSjmmv
157*6b3a42afSjmmv /// Creates a new command.
158*6b3a42afSjmmv ///
159*6b3a42afSjmmv /// \param name_ The name of the command. Must be unique within the context of
160*6b3a42afSjmmv /// a program and have no spaces.
161*6b3a42afSjmmv /// \param arg_list_ A textual description of the arguments received by the
162*6b3a42afSjmmv /// command. May be empty.
163*6b3a42afSjmmv /// \param min_args_ The minimum number of arguments required by the command.
164*6b3a42afSjmmv /// \param max_args_ The maximum number of arguments required by the command.
165*6b3a42afSjmmv /// -1 means infinity.
166*6b3a42afSjmmv /// \param short_description_ A description of the purpose of the command.
base_command_no_data(const std::string & name_,const std::string & arg_list_,const int min_args_,const int max_args_,const std::string & short_description_)167*6b3a42afSjmmv cmdline::base_command_no_data::base_command_no_data(
168*6b3a42afSjmmv const std::string& name_,
169*6b3a42afSjmmv const std::string& arg_list_,
170*6b3a42afSjmmv const int min_args_,
171*6b3a42afSjmmv const int max_args_,
172*6b3a42afSjmmv const std::string& short_description_) :
173*6b3a42afSjmmv command_proto(name_, arg_list_, min_args_, max_args_, short_description_)
174*6b3a42afSjmmv {
175*6b3a42afSjmmv }
176*6b3a42afSjmmv
177*6b3a42afSjmmv
178*6b3a42afSjmmv /// Entry point for the command.
179*6b3a42afSjmmv ///
180*6b3a42afSjmmv /// This delegates execution to the run() abstract function after the command
181*6b3a42afSjmmv /// line provided in args has been parsed.
182*6b3a42afSjmmv ///
183*6b3a42afSjmmv /// If this function returns, the command is assumed to have been executed
184*6b3a42afSjmmv /// successfully. Any error must be reported by means of exceptions.
185*6b3a42afSjmmv ///
186*6b3a42afSjmmv /// \param ui Object to interact with the I/O of the command. The command must
187*6b3a42afSjmmv /// always use this object to write to stdout and stderr.
188*6b3a42afSjmmv /// \param args The command line passed to the command broken by word, which
189*6b3a42afSjmmv /// includes options and arguments.
190*6b3a42afSjmmv ///
191*6b3a42afSjmmv /// \return The exit code that the program has to return. 0 on success, some
192*6b3a42afSjmmv /// other value on error.
193*6b3a42afSjmmv /// \throw usage_error If args is invalid (i.e. if the options are mispecified
194*6b3a42afSjmmv /// or if the arguments are invalid).
195*6b3a42afSjmmv int
main(cmdline::ui * ui,const cmdline::args_vector & args)196*6b3a42afSjmmv cmdline::base_command_no_data::main(cmdline::ui* ui,
197*6b3a42afSjmmv const cmdline::args_vector& args)
198*6b3a42afSjmmv {
199*6b3a42afSjmmv return run(ui, parse_cmdline(args));
200*6b3a42afSjmmv }
201