Improve the process for GNU tools
[minix3.git] / external / bsd / kyua-cli / dist / utils / cmdline / base_command.hpp
blob073225ca414683ffb91b0ea3ef5aefa2953cefa1
1 // Copyright 2010 Google Inc.
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.
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
35 #include <string>
37 #include "utils/cmdline/options.hpp"
38 #include "utils/cmdline/parser.hpp"
39 #include "utils/noncopyable.hpp"
41 namespace utils {
42 namespace cmdline {
45 class ui;
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;
57 /// Textual description of the command arguments.
58 const std::string _arg_list;
60 /// The minimum number of required arguments.
61 const int _min_args;
63 /// The maximum number of allowed arguments; -1 for infinity.
64 const int _max_args;
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*);
74 protected:
75 template< typename Option > void add_option(const Option&);
76 parsed_cmdline parse_cmdline(const args_vector&) const;
78 public:
79 command_proto(const std::string&, const std::string&, const int, const int,
80 const std::string&);
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.
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.
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;
113 public:
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
128 /// program.
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
134 /// validated.
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;
150 public:
151 base_command(const std::string&, const std::string&, const int, const int,
152 const std::string&);
154 int main(ui*, const args_vector&, const Data&);
158 } // namespace cmdline
159 } // namespace utils
162 #endif // !defined(UTILS_CMDLINE_BASE_COMMAND_HPP)