Ookii.CommandLine for C++  2.0.0
Public Types | Public Member Functions | List of all members
ookii::basic_command< CharType, Traits, Alloc > Class Template Referenceabstract

Abstract base class for all subcommands. More...

#include <ookii/subcommand.h>

Public Types

using builder_type = basic_parser_builder< CharType, Traits, Alloc >
 The concrete type of basic_parser_builder used.
 

Public Member Functions

 basic_command ()=default
 Initializes a new instance of the basic_command class.
 
 basic_command (builder_type &)
 Initializes a new instance of the basic_command class. More...
 
virtual ~basic_command ()=default
 Default destructor.
 
virtual int run ()=0
 Runs the command, after argument parsing was successful. More...
 

Detailed Description

template<typename CharType, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
class ookii::basic_command< CharType, Traits, Alloc >

Abstract base class for all subcommands.

Template Parameters
CharTypeThe character type used for arguments and other strings.
TraitsThe character traits to use for strings. Defaults to std::char_traits<CharType>.
AllocThe allocator to use for strings. Defaults to std::allocator<CharType>.

When you implement a subcommand, you must derive from the basic_command class, and implement the run() method.

In addition, you must provide a constructor that takes a reference to builder_type, which is a typedef for basic_parser_builder, which creates the arguments accepted by this command. This basic_parser_builder will have been initialized with the name and description of the command, as well as basic_parser_builder::case_sensitive() and basic_parser_builder::locale() values matching the basic_command_manager.

To specify a name or description for your command, you can either pass them to basic_command_manager::add_command(), or you can provide static methods that return them. If a name is not provided using either method, the name will match the type name of the subcommand class.

For example:

class some_command : public std::command
{
public:
some_command(builder_type &builder)
{
// ...
}
virtual int run() override
{
// ...
}
static std::string name()
{
return "some_name";
}
static std::string description()
{
return "The description.";
}
};
virtual int run()=0
Runs the command, after argument parsing was successful.
basic_parser_builder< CharType, Traits, Alloc > builder_type
The concrete type of basic_parser_builder used.
Definition: subcommand.h:72
basic_command< char > command
Typedef for basic_command using char as the character type.
Definition: subcommand.h:94

Several typedefs for common character types are provided:

Type Definition
ookii::command ookii::basic_command<char>
ookii::wcommand ookii::basic_command<wchar_t>

Constructor & Destructor Documentation

◆ basic_command()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
ookii::basic_command< CharType, Traits, Alloc >::basic_command ( builder_type )
inline

Initializes a new instance of the basic_command class.

Attention
You don't need to call this constructor when implementing a derived class, as it does nothing. It exists so New-Subcommand.ps1 can generate code that supports subcommands with a base class that does have a constructor.

Member Function Documentation

◆ run()

template<typename CharType , typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
virtual int ookii::basic_command< CharType, Traits, Alloc >::run ( )
pure virtual

Runs the command, after argument parsing was successful.

Returns
The exit code for the command. Typically, this code will be returned from the application to the OS.

The documentation for this class was generated from the following file: