Path: blob/main/contrib/kyua/utils/cmdline/base_command.hpp
48180 views
// Copyright 2010 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file utils/cmdline/base_command.hpp29/// Provides the utils::cmdline::base_command class.3031#if !defined(UTILS_CMDLINE_BASE_COMMAND_HPP)32#define UTILS_CMDLINE_BASE_COMMAND_HPP3334#include "utils/cmdline/base_command_fwd.hpp"3536#include <string>3738#include "utils/cmdline/options_fwd.hpp"39#include "utils/cmdline/parser_fwd.hpp"40#include "utils/cmdline/ui_fwd.hpp"41#include "utils/noncopyable.hpp"4243namespace utils {44namespace cmdline {454647/// Prototype class for the implementation of subcommands of a program.48///49/// Use the subclasses of command_proto defined in this module instead of50/// command_proto itself as base classes for your application-specific51/// commands.52class command_proto : noncopyable {53/// The user-visible name of the command.54const std::string _name;5556/// Textual description of the command arguments.57const std::string _arg_list;5859/// The minimum number of required arguments.60const int _min_args;6162/// The maximum number of allowed arguments; -1 for infinity.63const int _max_args;6465/// A textual description of the command.66const std::string _short_description;6768/// Collection of command-specific options.69options_vector _options;7071void add_option_ptr(const base_option*);7273protected:74template< typename Option > void add_option(const Option&);75parsed_cmdline parse_cmdline(const args_vector&) const;7677public:78command_proto(const std::string&, const std::string&, const int, const int,79const std::string&);80virtual ~command_proto(void);8182const std::string& name(void) const;83const std::string& arg_list(void) const;84const std::string& short_description(void) const;85const options_vector& options(void) const;86};878889/// Unparametrized base subcommand for a program.90///91/// Use this class to define subcommands for your program that do not need any92/// information passed in from the main command-line dispatcher other than the93/// command-line arguments.94class base_command_no_data : public command_proto {95/// Main code of the command.96///97/// This is called from main() after the command line has been processed and98/// validated.99///100/// \param ui Object to interact with the I/O of the command. The command101/// must always use this object to write to stdout and stderr.102/// \param cmdline The parsed command line, containing the values of any103/// given options and arguments.104///105/// \return The exit code that the program has to return. 0 on success,106/// some other value on error.107///108/// \throw std::runtime_error Any errors detected during the execution of109/// the command are reported by means of exceptions.110virtual int run(ui* ui, const parsed_cmdline& cmdline) = 0;111112public:113base_command_no_data(const std::string&, const std::string&, const int,114const int, const std::string&);115116int main(ui*, const args_vector&);117};118119120/// Parametrized base subcommand for a program.121///122/// Use this class to define subcommands for your program that need some kind of123/// runtime information passed in from the main command-line dispatcher.124///125/// \param Data The type of the object passed to the subcommand at runtime.126/// This is useful, for example, to pass around the runtime configuration of the127/// program.128template< typename Data >129class base_command : public command_proto {130/// Main code of the command.131///132/// This is called from main() after the command line has been processed and133/// validated.134///135/// \param ui Object to interact with the I/O of the command. The command136/// must always use this object to write to stdout and stderr.137/// \param cmdline The parsed command line, containing the values of any138/// given options and arguments.139/// \param data An instance of the runtime data passed from main().140///141/// \return The exit code that the program has to return. 0 on success,142/// some other value on error.143///144/// \throw std::runtime_error Any errors detected during the execution of145/// the command are reported by means of exceptions.146virtual int run(ui* ui, const parsed_cmdline& cmdline,147const Data& data) = 0;148149public:150base_command(const std::string&, const std::string&, const int, const int,151const std::string&);152153int main(ui*, const args_vector&, const Data&);154};155156157} // namespace cmdline158} // namespace utils159160161#endif // !defined(UTILS_CMDLINE_BASE_COMMAND_HPP)162163164