Path: blob/main/contrib/kyua/utils/cmdline/base_command.cpp
48178 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#include "utils/cmdline/base_command.hpp"2930#include "utils/cmdline/exceptions.hpp"31#include "utils/cmdline/options.hpp"32#include "utils/cmdline/parser.ipp"33#include "utils/sanity.hpp"3435namespace cmdline = utils::cmdline;363738/// Creates a new command.39///40/// \param name_ The name of the command. Must be unique within the context of41/// a program and have no spaces.42/// \param arg_list_ A textual description of the arguments received by the43/// command. May be empty.44/// \param min_args_ The minimum number of arguments required by the command.45/// \param max_args_ The maximum number of arguments required by the command.46/// -1 means infinity.47/// \param short_description_ A description of the purpose of the command.48cmdline::command_proto::command_proto(const std::string& name_,49const std::string& arg_list_,50const int min_args_,51const int max_args_,52const std::string& short_description_) :53_name(name_),54_arg_list(arg_list_),55_min_args(min_args_),56_max_args(max_args_),57_short_description(short_description_)58{59PRE(name_.find(' ') == std::string::npos);60PRE(max_args_ == -1 || min_args_ <= max_args_);61}626364/// Destructor for a command.65cmdline::command_proto::~command_proto(void)66{67for (options_vector::const_iterator iter = _options.begin();68iter != _options.end(); iter++)69delete *iter;70}717273/// Internal method to register a dynamically-allocated option.74///75/// Always use add_option() from subclasses to add options.76///77/// \param option_ The option to add. Must have been dynamically allocated.78/// This grabs ownership of the pointer, which is released when the command79/// is destroyed.80void81cmdline::command_proto::add_option_ptr(const cmdline::base_option* option_)82{83try {84_options.push_back(option_);85} catch (...) {86delete option_;87throw;88}89}909192/// Processes the command line based on the command description.93///94/// \param args The raw command line to be processed.95///96/// \return An object containing the list of options and free arguments found in97/// args.98///99/// \throw cmdline::usage_error If there is a problem processing the command100/// line. This error is caused by invalid input from the user.101cmdline::parsed_cmdline102cmdline::command_proto::parse_cmdline(const cmdline::args_vector& args) const103{104PRE(name() == args[0]);105const parsed_cmdline cmdline = cmdline::parse(args, options());106107const int argc = cmdline.arguments().size();108if (argc < _min_args)109throw usage_error("Not enough arguments");110if (_max_args != -1 && argc > _max_args)111throw usage_error("Too many arguments");112113return cmdline;114}115116117/// Gets the name of the command.118///119/// \return The command name.120const std::string&121cmdline::command_proto::name(void) const122{123return _name;124}125126127/// Gets the textual representation of the arguments list.128///129/// \return The description of the arguments list.130const std::string&131cmdline::command_proto::arg_list(void) const132{133return _arg_list;134}135136137/// Gets the description of the purpose of the command.138///139/// \return The description of the command.140const std::string&141cmdline::command_proto::short_description(void) const142{143return _short_description;144}145146147/// Gets the definition of the options accepted by the command.148///149/// \return The list of options.150const cmdline::options_vector&151cmdline::command_proto::options(void) const152{153return _options;154}155156157/// Creates a new command.158///159/// \param name_ The name of the command. Must be unique within the context of160/// a program and have no spaces.161/// \param arg_list_ A textual description of the arguments received by the162/// command. May be empty.163/// \param min_args_ The minimum number of arguments required by the command.164/// \param max_args_ The maximum number of arguments required by the command.165/// -1 means infinity.166/// \param short_description_ A description of the purpose of the command.167cmdline::base_command_no_data::base_command_no_data(168const std::string& name_,169const std::string& arg_list_,170const int min_args_,171const int max_args_,172const std::string& short_description_) :173command_proto(name_, arg_list_, min_args_, max_args_, short_description_)174{175}176177178/// Entry point for the command.179///180/// This delegates execution to the run() abstract function after the command181/// line provided in args has been parsed.182///183/// If this function returns, the command is assumed to have been executed184/// successfully. Any error must be reported by means of exceptions.185///186/// \param ui Object to interact with the I/O of the command. The command must187/// always use this object to write to stdout and stderr.188/// \param args The command line passed to the command broken by word, which189/// includes options and arguments.190///191/// \return The exit code that the program has to return. 0 on success, some192/// other value on error.193/// \throw usage_error If args is invalid (i.e. if the options are mispecified194/// or if the arguments are invalid).195int196cmdline::base_command_no_data::main(cmdline::ui* ui,197const cmdline::args_vector& args)198{199return run(ui, parse_cmdline(args));200}201202203