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 #if !defined(UTILS_CMDLINE_BASE_COMMAND_IPP)
30 #define UTILS_CMDLINE_BASE_COMMAND_IPP
32 #include "utils/cmdline/base_command.hpp"
39 /// Adds an option to the command.
41 /// This is to be called from the constructor of the subclass that implements
44 /// \param option_ The option to add.
45 template< typename Option >
47 command_proto::add_option(const Option& option_)
49 add_option_ptr(new Option(option_));
53 /// Creates a new command.
55 /// \param name_ The name of the command. Must be unique within the context of
56 /// a program and have no spaces.
57 /// \param arg_list_ A textual description of the arguments received by the
58 /// command. May be empty.
59 /// \param min_args_ The minimum number of arguments required by the command.
60 /// \param max_args_ The maximum number of arguments required by the command.
61 /// -1 means infinity.
62 /// \param short_description_ A description of the purpose of the command.
63 template< typename Data >
64 base_command< Data >::base_command(const std::string& name_,
65 const std::string& arg_list_,
68 const std::string& short_description_) :
69 command_proto(name_, arg_list_, min_args_, max_args_, short_description_)
74 /// Entry point for the command.
76 /// This delegates execution to the run() abstract function after the command
77 /// line provided in args has been parsed.
79 /// If this function returns, the command is assumed to have been executed
80 /// successfully. Any error must be reported by means of exceptions.
82 /// \param ui Object to interact with the I/O of the command. The command must
83 /// always use this object to write to stdout and stderr.
84 /// \param args The command line passed to the command broken by word, which
85 /// includes options and arguments.
86 /// \param data An opaque data structure to pass to the run method.
88 /// \return The exit code that the program has to return. 0 on success, some
89 /// other value on error.
90 /// \throw usage_error If args is invalid (i.e. if the options are mispecified
91 /// or if the arguments are invalid).
92 template< typename Data >
94 base_command< Data >::main(ui* ui, const args_vector& args, const Data& data)
96 return run(ui, parse_cmdline(args), data);
104 #endif // !defined(UTILS_CMDLINE_BASE_COMMAND_IPP)