Blame utils/cmdline/base_command.ipp

Packit 209faa
// Copyright 2010 The Kyua Authors.
Packit 209faa
// All rights reserved.
Packit 209faa
//
Packit 209faa
// Redistribution and use in source and binary forms, with or without
Packit 209faa
// modification, are permitted provided that the following conditions are
Packit 209faa
// met:
Packit 209faa
//
Packit 209faa
// * Redistributions of source code must retain the above copyright
Packit 209faa
//   notice, this list of conditions and the following disclaimer.
Packit 209faa
// * Redistributions in binary form must reproduce the above copyright
Packit 209faa
//   notice, this list of conditions and the following disclaimer in the
Packit 209faa
//   documentation and/or other materials provided with the distribution.
Packit 209faa
// * Neither the name of Google Inc. nor the names of its contributors
Packit 209faa
//   may be used to endorse or promote products derived from this software
Packit 209faa
//   without specific prior written permission.
Packit 209faa
//
Packit 209faa
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 209faa
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 209faa
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 209faa
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 209faa
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 209faa
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 209faa
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 209faa
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 209faa
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 209faa
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 209faa
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 209faa
Packit 209faa
#if !defined(UTILS_CMDLINE_BASE_COMMAND_IPP)
Packit 209faa
#define UTILS_CMDLINE_BASE_COMMAND_IPP
Packit 209faa
Packit 209faa
#include "utils/cmdline/base_command.hpp"
Packit 209faa
Packit 209faa
Packit 209faa
namespace utils {
Packit 209faa
namespace cmdline {
Packit 209faa
Packit 209faa
Packit 209faa
/// Adds an option to the command.
Packit 209faa
///
Packit 209faa
/// This is to be called from the constructor of the subclass that implements
Packit 209faa
/// the command.
Packit 209faa
///
Packit 209faa
/// \param option_ The option to add.
Packit 209faa
template< typename Option >
Packit 209faa
void
Packit 209faa
command_proto::add_option(const Option& option_)
Packit 209faa
{
Packit 209faa
    add_option_ptr(new Option(option_));
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Creates a new command.
Packit 209faa
///
Packit 209faa
/// \param name_ The name of the command.  Must be unique within the context of
Packit 209faa
///     a program and have no spaces.
Packit 209faa
/// \param arg_list_ A textual description of the arguments received by the
Packit 209faa
///     command.  May be empty.
Packit 209faa
/// \param min_args_ The minimum number of arguments required by the command.
Packit 209faa
/// \param max_args_ The maximum number of arguments required by the command.
Packit 209faa
///     -1 means infinity.
Packit 209faa
/// \param short_description_ A description of the purpose of the command.
Packit 209faa
template< typename Data >
Packit 209faa
base_command< Data >::base_command(const std::string& name_,
Packit 209faa
                                   const std::string& arg_list_,
Packit 209faa
                                   const int min_args_,
Packit 209faa
                                   const int max_args_,
Packit 209faa
                                   const std::string& short_description_) :
Packit 209faa
    command_proto(name_, arg_list_, min_args_, max_args_, short_description_)
Packit 209faa
{
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Entry point for the command.
Packit 209faa
///
Packit 209faa
/// This delegates execution to the run() abstract function after the command
Packit 209faa
/// line provided in args has been parsed.
Packit 209faa
///
Packit 209faa
/// If this function returns, the command is assumed to have been executed
Packit 209faa
/// successfully.  Any error must be reported by means of exceptions.
Packit 209faa
///
Packit 209faa
/// \param ui Object to interact with the I/O of the command.  The command must
Packit 209faa
///     always use this object to write to stdout and stderr.
Packit 209faa
/// \param args The command line passed to the command broken by word, which
Packit 209faa
///     includes options and arguments.
Packit 209faa
/// \param data An opaque data structure to pass to the run method.
Packit 209faa
///
Packit 209faa
/// \return The exit code that the program has to return.  0 on success, some
Packit 209faa
///     other value on error.
Packit 209faa
/// \throw usage_error If args is invalid (i.e. if the options are mispecified
Packit 209faa
///     or if the arguments are invalid).
Packit 209faa
template< typename Data >
Packit 209faa
int
Packit 209faa
base_command< Data >::main(ui* ui, const args_vector& args, const Data& data)
Packit 209faa
{
Packit 209faa
    return run(ui, parse_cmdline(args), data);
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
}  // namespace cli
Packit 209faa
}  // namespace utils
Packit 209faa
Packit 209faa
Packit 209faa
#endif  // !defined(UTILS_CMDLINE_BASE_COMMAND_IPP)