xref: /freebsd/contrib/kyua/utils/cmdline/base_command.hpp (revision 02e9120893770924227138ba49df1edb3896112a)
1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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.
16 //
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.
28 
29 /// \file utils/cmdline/base_command.hpp
30 /// Provides the utils::cmdline::base_command class.
31 
32 #if !defined(UTILS_CMDLINE_BASE_COMMAND_HPP)
33 #define UTILS_CMDLINE_BASE_COMMAND_HPP
34 
35 #include "utils/cmdline/base_command_fwd.hpp"
36 
37 #include <string>
38 
39 #include "utils/cmdline/options_fwd.hpp"
40 #include "utils/cmdline/parser_fwd.hpp"
41 #include "utils/cmdline/ui_fwd.hpp"
42 #include "utils/noncopyable.hpp"
43 
44 namespace utils {
45 namespace cmdline {
46 
47 
48 /// Prototype class for the implementation of subcommands of a program.
49 ///
50 /// Use the subclasses of command_proto defined in this module instead of
51 /// command_proto itself as base classes for your application-specific
52 /// commands.
53 class command_proto : noncopyable {
54     /// The user-visible name of the command.
55     const std::string _name;
56 
57     /// Textual description of the command arguments.
58     const std::string _arg_list;
59 
60     /// The minimum number of required arguments.
61     const int _min_args;
62 
63     /// The maximum number of allowed arguments; -1 for infinity.
64     const int _max_args;
65 
66     /// A textual description of the command.
67     const std::string _short_description;
68 
69     /// Collection of command-specific options.
70     options_vector _options;
71 
72     void add_option_ptr(const base_option*);
73 
74 protected:
75     template< typename Option > void add_option(const Option&);
76     parsed_cmdline parse_cmdline(const args_vector&) const;
77 
78 public:
79     command_proto(const std::string&, const std::string&, const int, const int,
80                   const std::string&);
81     virtual ~command_proto(void);
82 
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;
87 };
88 
89 
90 /// Unparametrized base subcommand for a program.
91 ///
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.
97     ///
98     /// This is called from main() after the command line has been processed and
99     /// validated.
100     ///
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.
105     ///
106     /// \return The exit code that the program has to return.  0 on success,
107     ///     some other value on error.
108     ///
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;
112 
113 public:
114     base_command_no_data(const std::string&, const std::string&, const int,
115                          const int, const std::string&);
116 
117     int main(ui*, const args_vector&);
118 };
119 
120 
121 /// Parametrized base subcommand for a program.
122 ///
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.
125 ///
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
128 /// program.
129 template< typename Data >
130 class base_command : public command_proto {
131     /// Main code of the command.
132     ///
133     /// This is called from main() after the command line has been processed and
134     /// validated.
135     ///
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().
141     ///
142     /// \return The exit code that the program has to return.  0 on success,
143     ///     some other value on error.
144     ///
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;
149 
150 public:
151     base_command(const std::string&, const std::string&, const int, const int,
152                  const std::string&);
153 
154     int main(ui*, const args_vector&, const Data&);
155 };
156 
157 
158 }  // namespace cmdline
159 }  // namespace utils
160 
161 
162 #endif  // !defined(UTILS_CMDLINE_BASE_COMMAND_HPP)
163