xref: /freebsd/contrib/kyua/utils/cmdline/base_command.ipp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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#if !defined(UTILS_CMDLINE_BASE_COMMAND_IPP)
30#define UTILS_CMDLINE_BASE_COMMAND_IPP
31
32#include "utils/cmdline/base_command.hpp"
33
34
35namespace utils {
36namespace cmdline {
37
38
39/// Adds an option to the command.
40///
41/// This is to be called from the constructor of the subclass that implements
42/// the command.
43///
44/// \param option_ The option to add.
45template< typename Option >
46void
47command_proto::add_option(const Option& option_)
48{
49    add_option_ptr(new Option(option_));
50}
51
52
53/// Creates a new command.
54///
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.
63template< typename Data >
64base_command< Data >::base_command(const std::string& name_,
65                                   const std::string& arg_list_,
66                                   const int min_args_,
67                                   const int max_args_,
68                                   const std::string& short_description_) :
69    command_proto(name_, arg_list_, min_args_, max_args_, short_description_)
70{
71}
72
73
74/// Entry point for the command.
75///
76/// This delegates execution to the run() abstract function after the command
77/// line provided in args has been parsed.
78///
79/// If this function returns, the command is assumed to have been executed
80/// successfully.  Any error must be reported by means of exceptions.
81///
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.
87///
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).
92template< typename Data >
93int
94base_command< Data >::main(ui* ui, const args_vector& args, const Data& data)
95{
96    return run(ui, parse_cmdline(args), data);
97}
98
99
100}  // namespace cli
101}  // namespace utils
102
103
104#endif  // !defined(UTILS_CMDLINE_BASE_COMMAND_IPP)
105