18#include "argument_group.h"
19#include "cli_context.h"
20#include "flag_argument.h"
21#include "option_argument.h"
22#include "positional_argument.h"
29namespace cli::commands
37 friend std::ostream &operator<<(std::ostream &out,
const Command &cmd);
47 Command(std::string_view
id, std::string_view short_desc, std::string_view long_desc,
48 std::unique_ptr<std::function<
void(
const CliContext &)>> actionPtr)
49 : identifier(id), shortDescription(short_desc), longDescription(long_desc),
50 executePtr(std::move(actionPtr))
59 Command(std::string_view
id, std::string_view short_desc, std::string_view long_desc,
60 std::function<
void(
const CliContext &)> action)
61 : identifier(id), shortDescription(short_desc), longDescription(long_desc),
62 executePtr(std::make_unique<std::function<void(const
CliContext &)>>(std::move(action)))
69 : identifier(id), shortDescription(
""), longDescription(
""), executePtr(nullptr)
83#pragma region Accessors
87 [[nodiscard]]
constexpr std::string_view
getIdentifier() const noexcept {
return identifier; }
93 return shortDescription;
100 return longDescription;
109 [[nodiscard]]
const std::vector<std::shared_ptr<PositionalArgumentBase>> &
112 return positionalArguments;
122 return optionArguments;
132 return flagArguments;
141 return argumentGroups;
176#pragma endregion Accessor
182#pragma region ChainingMethods
199 template <
typename T>
202 safeAddToArgGroup(arg);
203 positionalArguments.push_back(arg);
236 safeAddToArgGroup(arg);
237 optionArguments.push_back(arg);
305 template <
typename... Args>
306 requires((
sizeof...(Args) > 0) &&
307 (std::derived_from<std::remove_cvref_t<Args>,
ArgumentBase> && ...))
314 template <
typename... Args>
315 requires((
sizeof...(Args) > 0) &&
316 (std::derived_from<std::remove_cvref_t<Args>,
ArgumentBase> && ...))
318#pragma endregion ChainingMethods
321 size_t indexForNewArgGroup{0};
322 void safeAddToArgGroup(
const std::shared_ptr<ArgumentBase> &arg);
323 void addArgGroup(
const ArgumentGroup &argGroup);
325 std::string identifier;
326 std::string shortDescription;
327 std::string longDescription;
328 std::unique_ptr<std::function<void(
const CliContext &)>> executePtr;
331 std::vector<std::shared_ptr<PositionalArgumentBase>> positionalArguments;
332 std::vector<std::shared_ptr<OptionArgumentBase>> optionArguments;
333 std::vector<std::shared_ptr<FlagArgument>> flagArguments;
334 std::vector<std::unique_ptr<ArgumentGroup>> argumentGroups;
336 std::string docStringShort;
337 std::string docStringLong;
339 std::map<std::string, std::unique_ptr<Command>, std::less<>> subCommands;
349 : std::runtime_error(buildMessage(cmd, msg)), malformedCmd(&cmd)
353 const Command &command()
const noexcept {
return *malformedCmd; }
358 static std::string buildMessage(
const Command &cmd,
const std::string &msg);
361template <
typename... Args>
362 requires((
sizeof...(Args) > 0) &&
363 (std::derived_from<std::remove_cvref_t<Args>,
ArgumentBase> && ...))
366 argumentGroups.emplace_back(std::make_unique<ExclusiveGroup>(std::forward<Args>(args)...));
367 addArgGroup(*argumentGroups.back());
368 indexForNewArgGroup++;
372template <
typename... Args>
373 requires((
sizeof...(Args) > 0) &&
374 (std::derived_from<std::remove_cvref_t<Args>,
ArgumentBase> && ...))
377 argumentGroups.emplace_back(std::make_unique<InclusiveGroup>(std::forward<Args>(args)...));
378 addArgGroup(*argumentGroups.back());
379 indexForNewArgGroup++;
Represents the context of a command-line interface (CLI) invocation and as such contains the parsed v...
Definition cli_context.h:39
Base class for command-line arguments.
Definition argument.h:41
Represents a command in the CLI application.
Definition command.h:36
Command & withSubCommand(std::unique_ptr< Command > subCommandPtr)
Add a sub-command to this command.
Definition command.cpp:104
const std::vector< std::shared_ptr< FlagArgument > > & getFlagArguments() const noexcept
Get the flag arguments for the command.
Definition command.h:129
std::string_view getDocStringLong() const
Get the long documentation string for the command.
Definition command.cpp:39
Command(std::string_view id, std::string_view short_desc, std::string_view long_desc, std::unique_ptr< std::function< void(const CliContext &)> > actionPtr)
Construct a new Command object.
Definition command.h:47
Command & withPositionalArgument(PositionalArgument< T > &&arg)
Add a positional argument to the command.
Definition command.h:213
Command & withOptionArgument(std::shared_ptr< OptionArgument< T > > arg)
Add an option argument to the command.
Definition command.h:234
bool hasExecutionFunction() const noexcept
Check if the command has an execution function.
Definition command.h:105
Command(std::string_view id, std::string_view short_desc, std::string_view long_desc, std::function< void(const CliContext &)> action)
Construct a new Command object.
Definition command.h:59
const std::vector< std::shared_ptr< PositionalArgumentBase > > & getPositionalArguments() const noexcept
Get the positional arguments for the command.
Definition command.h:110
Command & withShortDescription(std::string_view desc)
Set the short description for the command.
Definition command.cpp:62
Command & withOptionArgument(OptionArgument< T > &&arg)
Add an option argument to the command.
Definition command.h:247
constexpr std::string_view getIdentifier() const noexcept
Get the unique identifier for the command.
Definition command.h:87
Command(std::string_view id)
Construct a new Command object.
Definition command.h:68
Command & withExclusiveGroup(Args &&...args)
Add a sub-command to this command.
Definition command.h:364
Command & withOptionArgument(OptionArgument< T > &arg)
Add an option argument to the command.
Definition command.h:258
constexpr std::string_view getShortDescription() const noexcept
Get the short description of the command.
Definition command.h:91
Command & withExecutionFunc(std::unique_ptr< std::function< void(const CliContext &)> > actionPtr)
Set the execution function for the command.
Definition command.cpp:91
Command & withPositionalArgument(std::shared_ptr< PositionalArgument< T > > arg)
Add a positional argument to the command.
Definition command.h:200
constexpr std::string_view getLongDescription() const noexcept
Get the long description of the command.
Definition command.h:98
Command & withLongDescription(std::string_view desc)
Set the long description for the command.
Definition command.cpp:68
auto & getSubCommands()
Get all sub-commands of the command.
Definition command.h:170
const std::vector< std::shared_ptr< OptionArgumentBase > > & getOptionArguments() const noexcept
Get the option arguments for the command.
Definition command.h:119
Command & withInclusiveGroup(Args &&...args)
Add an inclusive argument group to this command.
Definition command.h:375
Command * getSubCommand(std::string_view id)
Get a sub-command by its identifier.
Definition command.cpp:145
Command & withPositionalArgument(PositionalArgument< T > &arg)
Add a positional argument to the command.
Definition command.h:224
Command & withFlagArgument(std::shared_ptr< FlagArgument > arg)
Add a flag argument to the command.
Definition command.cpp:74
std::string_view getDocStringShort() const
Get the short documentation string for the command.
Definition command.cpp:29
const std::vector< std::unique_ptr< ArgumentGroup > > & getArgumentGroups() const noexcept
Get the argument groups for the command.
Definition command.h:138
void execute(const CliContext &context) const
Execute the command.
Definition command.cpp:49
auto const & getSubCommands() const
Get all sub-commands of the command.
Definition command.h:174
Represents a flag argument in the CLI.
Definition flag_argument.h:31
Represents option arguments in the CLI.
Definition option_argument.h:64
Represents positional arguments in the CLI.
Definition positional_argument.h:57
Documentation writer for CLI commands. Consists of formatters for commands and arguments.
Definition docwriting.h:34