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