CPP-AP 3.0.1
Command-line argument parser for C++20
Loading...
Searching...
No Matches
ap::argument_group Class Reference

Represents a group of arguments. More...

#include <argument_group.hpp>

Public Member Functions

 argument_group ()=delete
 
argument_grouprequired (const bool r=true) noexcept
 Set the required attribute of the group.
 
argument_groupmutually_exclusive (const bool me=true) noexcept
 Set the mutually_exclusive attribute of the group.
 

Private Types

using arg_ptr_t = std::shared_ptr< detail::argument_base >
 The argument pointer type alias.
 
using arg_ptr_vec_t = std::vector< arg_ptr_t >
 The argument pointer list type alias.
 

Private Member Functions

 argument_group (argument_parser &parser, const std::string_view name)
 Construct a new argument group with the given name.
 
void _add_argument (arg_ptr_t arg) noexcept
 Add a new argument to this group (called internally by parser).
 

Static Private Member Functions

static std::unique_ptr< argument_groupcreate (argument_parser &parser, std::string_view name)
 Factory method to create an argument group.
 

Private Attributes

argument_parser_parser
 Pointer to the owning parser.
 
std::string _name
 Name of the group (used in help output).
 
arg_ptr_vec_t _arguments
 A list of arguments that belong to this group.
 
bool _required: 1 = false
 The required attribute value (default: false).
 
bool _mutually_exclusive: 1 = false
 The mutually exclusive attribute value (default: false).
 

Friends

class argument_parser
 

Detailed Description

Represents a group of arguments.

Groups allow arguments to be organized under a dedicated section in the parser's help message.

A group can be marked as:

  • required: at least one argument from the group must be used in the command-line
  • mutually exclusive: at most one argument from the group can be used in the command-line
Note
- This class is not intended to be constructed directly, but rather through the add_group method of ap::argument_parser.
- User defined groups may contain only optional arguments (and flags).

Example usage:

ap::argument_parser parser("myprog");
auto& out_opts = parser.add_group("Output Options").mutually_exclusive();
group.add_optional_argument(out_opts, "output", "o")
.nargs(1)
.help("Print output to the given file");
group.add_optional_argument<ap::none_type>(out_opts, "print", "p")
.help("Print output to the console");
The main argument parser class.
A type representing the absence of a value. This type is used for arguments that should not store any...
Definition types.hpp:20

Here out_opts is a mutually exclusive group, so using both arguments at the same time would cause an error.

Definition at line 41 of file argument_group.hpp.

Member Typedef Documentation

◆ arg_ptr_t

using ap::argument_group::arg_ptr_t = std::shared_ptr<detail::argument_base>
private

The argument pointer type alias.

Definition at line 78 of file argument_group.hpp.

◆ arg_ptr_vec_t

using ap::argument_group::arg_ptr_vec_t = std::vector<arg_ptr_t>
private

The argument pointer list type alias.

Definition at line 79 of file argument_group.hpp.

Constructor & Destructor Documentation

◆ argument_group() [1/2]

ap::argument_group::argument_group ( )
delete

◆ argument_group() [2/2]

ap::argument_group::argument_group ( argument_parser parser,
const std::string_view  name 
)
inlineprivate

Construct a new argument group with the given name.

Definition at line 91 of file argument_group.hpp.

Member Function Documentation

◆ _add_argument()

void ap::argument_group::_add_argument ( arg_ptr_t  arg)
inlineprivatenoexcept

Add a new argument to this group (called internally by parser).

Definition at line 95 of file argument_group.hpp.

◆ create()

static std::unique_ptr< argument_group > ap::argument_group::create ( argument_parser parser,
std::string_view  name 
)
inlinestaticprivate

Factory method to create an argument group.

Parameters
parserThe owning parser.
nameName of the group.
Examples
/home/runner/work/cpp-ap/cpp-ap/include/ap/argument_parser.hpp.

Definition at line 86 of file argument_group.hpp.

◆ mutually_exclusive()

argument_group & ap::argument_group::mutually_exclusive ( const bool  me = true)
inlinenoexcept

Set the mutually_exclusive attribute of the group.

  • If set to true, the parser will allow at most one argument from the group to be used in the command-line.
  • If more than one argument from the group is used, an exception will be thrown.
  • Argument groups are NOT mutually exclusive by default.
Parameters
meThe value to set for the attribute (default: true).
Returns
Reference to the group instance.

Definition at line 70 of file argument_group.hpp.

◆ required()

argument_group & ap::argument_group::required ( const bool  r = true)
inlinenoexcept

Set the required attribute of the group.

  • If set to true, the parser will require at least one argument from the group to be used in the command-line.
  • If no arguments from the group are used, an exception will be thrown.
  • Argument groups are NOT required by default.
Parameters
rThe value to set for the attribute (default: true).
Returns
Reference to the group instance.

Definition at line 55 of file argument_group.hpp.

Friends And Related Symbol Documentation

◆ argument_parser

friend class argument_parser
friend

Definition at line 75 of file argument_group.hpp.

Member Data Documentation

◆ _arguments

arg_ptr_vec_t ap::argument_group::_arguments
private

A list of arguments that belong to this group.

Definition at line 101 of file argument_group.hpp.

◆ _mutually_exclusive

bool ap::argument_group::_mutually_exclusive
private

The mutually exclusive attribute value (default: false).

Definition at line 104 of file argument_group.hpp.

◆ _name

std::string ap::argument_group::_name
private

Name of the group (used in help output).

Definition at line 100 of file argument_group.hpp.

◆ _parser

argument_parser* ap::argument_group::_parser
private

Pointer to the owning parser.

Definition at line 99 of file argument_group.hpp.

◆ _required

bool ap::argument_group::_required
private

The required attribute value (default: false).

Definition at line 103 of file argument_group.hpp.


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