1 #ifndef OOKII_SHELL_COMMAND_H_
5 #define OOKII_SHELL_COMMAND_H_
68 template<
typename CharType = details::default_
char_type,
typename Traits = std::
char_traits<CharType>,
typename Alloc = std::allocator<CharType>>
91 virtual int run() = 0;
104 template<
typename CharType,
typename Traits = std::
char_traits<CharType>,
typename Alloc = std::allocator<CharType>>
116 using creator = std::function<std::unique_ptr<command_type>(
builder_type &)>;
128 auto creator = [](
builder_type &builder) -> std::unique_ptr<command_type>
130 return std::make_unique<T>(builder);
141 return _creator(builder);
190 template<
typename CharType = details::default_
char_type,
typename Traits = std::
char_traits<CharType>,
typename Alloc = std::allocator<CharType>>
221 _application_name{application_name},
223 _case_sensitive{case_sensitive}
240 name = name_helper<T>::name();
242 if (description.empty())
243 description = description_helper<T>::description();
245 auto [it, success] = _commands.emplace(name, info_type::template create<T>(name, description));
247 throw std::logic_error(
"Duplicate command name");
257 return _commands | std::views::values;
266 auto it = _commands.find(name);
267 if (it == _commands.end())
288 template<
typename Iterator>
299 auto command = info->create(builder);
300 auto parser = builder.build();
301 if (!parser.parse(begin, end, options))
321 template<
typename Range>
366 template<
typename Iterator>
393 template<
typename Range>
465 template<
typename Iterator>
472 return command->run();
491 template<
typename Range>
497 return run_command(name, begin(range), end(range), options);
520 return run_command(name, args.begin(), args.end(), options);
540 template<
typename Iterator>
547 return command->run();
566 template<
typename Range>
572 return run_command(begin(range), end(range), options);
595 return run_command(args.begin(), args.end(), options);
620 return command->run();
628 auto &stream = options.output;
629 auto usage =
format::ncformat(_locale, options.command_usage_format, _application_name);
631 stream << usage << std::endl << std::endl;
632 stream << options.available_commands_header << std::endl << std::endl;
634 for (
const auto &command : _commands)
637 stream <<
format::ncformat(_locale, options.command_format, command.second.name(), command.second.description()) << std::endl;
651 string_type full_name = _application_name +
static_cast<CharType
>(
' ') + command.
name();
653 builder.locale(_locale)
654 .case_sensitive(_case_sensitive)
661 template<
typename T,
typename =
void>
666 return get_short_type_name<T, CharType, Traits, Alloc>();
671 struct name_helper<T, std::void_t<decltype(T::name())>>
679 template<
typename T,
typename =
void>
680 struct description_helper
689 struct description_helper<T, std::void_t<decltype(T::description())>>
693 return T::description();
697 std::map<string_type, info_type, string_less> _commands;
700 bool _case_sensitive;
Provides functionality to specify options and arguments to create a new basic_command_line_parser.
Definition: command_line_builder.h:36
Manages registration, creation and invocation of shell commands for an application.
Definition: shell_command.h:192
int run_command(const string_type &name, Range range, const usage_options_type &options={}) const
Creates an instance of the specified command, parsing the specified arguments, and runs the command.
Definition: shell_command.h:492
const info_type * get_command(const string_type &name) const
Gets information about a shell command by name.
Definition: shell_command.h:264
std::unique_ptr< command_type > create_command(std::initializer_list< T > args, const usage_options_type &options={}) const
Creates an instance of a command based on the specified arguments.
Definition: shell_command.h:418
int run_command(const string_type &name, Iterator begin, Iterator end, const usage_options_type &options={}) const
Creates an instance of the specified command, parsing the specified arguments, and runs the command.
Definition: shell_command.h:466
basic_shell_command_manager & add_command(string_type name={}, string_type description={})
Adds a command to the basic_shell_command_manager.
Definition: shell_command.h:237
int run_command(const string_type &name, std::initializer_list< T > args, const usage_options_type &options={}) const
Creates an instance of the specified command, parsing the specified arguments, and runs the command.
Definition: shell_command.h:518
std::unique_ptr< command_type > create_command(Range range, const usage_options_type &options={}) const
Creates an instance of a command based on the specified arguments.
Definition: shell_command.h:394
builder_type create_parser_builder(const info_type &command) const
Creates a basic_parser_builder for a specified command.
Definition: shell_command.h:648
typename info_type::string_type string_type
The concrete string type used.
Definition: shell_command.h:197
std::unique_ptr< command_type > create_command(int argc, const CharType *const argv[], const usage_options_type &options={}) const
Creates an instance of a command based on the specified arguments.
Definition: shell_command.h:437
std::unique_ptr< command_type > create_command(const string_type &name, Iterator begin, Iterator end, const usage_options_type &options={}) const
Creates an instance of the specified command, parsing the specified arguments.
Definition: shell_command.h:289
int run_command(int argc, const CharType *const argv[], const usage_options_type &options={}) const
Creates an instance of a command based onthe specified arguments, and runs the command.
Definition: shell_command.h:614
std::basic_ostream< CharType, Traits > stream_type
The concrete type of output stream used.
Definition: shell_command.h:205
static constexpr int error_return_code
The error exit code used by run_shell_command() if no command name was supplied or thesupplied comman...
Definition: shell_command.h:209
std::unique_ptr< command_type > create_command(const string_type &name, std::initializer_list< T > args, const usage_options_type &options={}) const
Creates an instance of the specified command, parsing the specified arguments.
Definition: shell_command.h:346
std::unique_ptr< command_type > create_command(const string_type &name, Range range, const usage_options_type &options={}) const
Creates an instance of the specified command, parsing the specified arguments.
Definition: shell_command.h:322
int run_command(Range range, const usage_options_type &options={}) const
Creates an instance of a command based onthe specified arguments, and runs the command.
Definition: shell_command.h:567
std::unique_ptr< command_type > create_command(Iterator begin, Iterator end, const usage_options_type &options={}) const
Creates an instance of a command based on the specified arguments.
Definition: shell_command.h:367
typename info_type::builder_type builder_type
The concrete type of basic_parser_builder used.
Definition: shell_command.h:199
void write_usage(const usage_options_type &options={}) const
Writes usage help about the available commands.
Definition: shell_command.h:626
int run_command(std::initializer_list< T > args, const usage_options_type &options={}) const
Creates an instance of a command based onthe specified arguments, and runs the command.
Definition: shell_command.h:593
int run_command(Iterator begin, Iterator end, const usage_options_type &options={}) const
Creates an instance of a command based onthe specified arguments, and runs the command.
Definition: shell_command.h:541
auto commands() const noexcept
Gets a view of all the commands.
Definition: shell_command.h:255
typename info_type::command_type command_type
The concrete type of basic_shell_command used.
Definition: shell_command.h:201
basic_shell_command_manager(string_type application_name, bool case_sensitive=false, const std::locale &locale={})
Initializes a new instance of the basic_shell_command_manager class.
Definition: shell_command.h:219
Provides options for how to format usage help for applications using shell commands.
Definition: shell_command_usage_options.h:30
Abstract base class for all shell commands.
Definition: shell_command.h:70
virtual ~basic_shell_command()=default
Default destructor.
basic_shell_command()=default
Initializes a new instance of the basic_shell_command class.
basic_parser_builder< CharType, Traits, Alloc > builder_type
The concrete type of basic_parser_builder used.
Definition: shell_command.h:73
virtual int run()=0
Runs the command, after argument parsing was successful.
basic_shell_command(builder_type &)
Initializes a new instance of the basic_shell_command class.
Definition: shell_command.h:83
Provides information about a shell command.
Definition: shell_command.h:106
typename command_type::builder_type builder_type
The concrete type of basic_parser_builder used.
Definition: shell_command.h:111
basic_shell_command< CharType, Traits, Alloc > command_type
The concrete type of basic_shell_command used.
Definition: shell_command.h:109
static shell_command_info create(string_type name, string_type description)
Creates a shell_command_info instance for the specified type.
Definition: shell_command.h:126
const string_type & description() const noexcept
Gets the description of the shell command.
Definition: shell_command.h:151
std::basic_string< CharType, Traits, Alloc > string_type
The concrete string type used.
Definition: shell_command.h:113
std::unique_ptr< command_type > create(builder_type &builder) const
Creates an instance of the shell command type.
Definition: shell_command.h:139
const string_type & name() const noexcept
Gets the name of the shell command.
Definition: shell_command.h:145
Provides the ookii::basic_parser_builder class.
Namespace containing the core Ookii.CommandLine.Cpp types.
Definition: command_line_argument.h:16
std::basic_ostream< CharType, Traits > & reset_indent(std::basic_ostream< CharType, Traits > &stream)
IO manipulator that lets the next line start at the beginning of the line, without indenting it.
Definition: line_wrapping_stream.h:515
details::set_indent_helper set_indent(size_t indent)
IO manipulator that changes the number of spaces that each line is indented with for a line wrapping ...
Definition: line_wrapping_stream.h:493
Provides the ookii::shell_command_usage_options class.
A version of the std::less predicate for strings that supports case insensitive comparison.
Definition: string_helper.h:23