MaCh3  2.8.0
Reference Guide
Public Member Functions | List of all members
M3::MaCh3ArgumentParser Class Reference

Extended ArgumentParser with MaCh3-specific functionality. More...

#include <CLI/API/m3argparse.hpp>

Inheritance diagram for M3::MaCh3ArgumentParser:
[legend]
Collaboration diagram for M3::MaCh3ArgumentParser:
[legend]

Public Member Functions

virtual ~MaCh3ArgumentParser ()=default
 
const std::string name () const
 Get the name of this parser/subcommand. More...
 
const std::list< std::reference_wrapper< ArgumentParser > > & subparsers () const
 Get the list of registered subparsers. More...
 
const MaCh3ArgumentParser & get_subcommand_used () const
 Get the subcommand that was used in parsing. More...
 
std::vector< std::string > get_long_option_names () const
 Return all long option names (–xxx) registered with this parser. More...
 
bool option_takes_value (const std::string &name) const
 Return true if the named option expects a value (not a flag) More...
 

Detailed Description

Extended ArgumentParser with MaCh3-specific functionality.

This class extends the standard ArgumentParser to provide additional methods for accessing parser name, subparsers, and tracking which subcommand was used.

Definition at line 17 of file m3argparse.hpp.

Constructor & Destructor Documentation

◆ ~MaCh3ArgumentParser()

virtual M3::MaCh3ArgumentParser::~MaCh3ArgumentParser ( )
virtualdefault

Member Function Documentation

◆ get_long_option_names()

std::vector<std::string> M3::MaCh3ArgumentParser::get_long_option_names ( ) const
inline

Return all long option names (–xxx) registered with this parser.

Definition at line 50 of file m3argparse.hpp.

50  {
51  std::vector<std::string> names;
52  for (const auto& [name, _] : this->m_argument_map) {
53  if (name.size() >= 2 && name[0] == '-' && name[1] == '-')
54  names.push_back(name);
55  }
56  return names;
57  }
const std::string name() const
Get the name of this parser/subcommand.
Definition: m3argparse.hpp:24

◆ get_subcommand_used()

const MaCh3ArgumentParser& M3::MaCh3ArgumentParser::get_subcommand_used ( ) const
inline

Get the subcommand that was used in parsing.

Recursively traverses the subparser hierarchy to find the deepest subcommand that was actually invoked.

Returns
Reference to the MaCh3ArgumentParser of the used subcommand

Definition at line 40 of file m3argparse.hpp.

40  {
41  for (const std::reference_wrapper<ArgumentParser>& subparser : this->m_subparsers) {
42  if (this->is_subcommand_used(subparser.get())) {
43  return static_cast<MaCh3ArgumentParser*>(&(subparser.get()))->get_subcommand_used();
44  }
45  }
46  return (*this);
47  }

◆ name()

const std::string M3::MaCh3ArgumentParser::name ( ) const
inline

Get the name of this parser/subcommand.

Returns
The program or subcommand name

Definition at line 24 of file m3argparse.hpp.

24  {
25  return this->m_program_name;
26  }

◆ option_takes_value()

bool M3::MaCh3ArgumentParser::option_takes_value ( const std::string &  name) const
inline

Return true if the named option expects a value (not a flag)

Definition at line 60 of file m3argparse.hpp.

60  {
61  auto it = this->m_argument_map.find(name);
62  if (it == this->m_argument_map.end()) return false;
63  // get_usage_full() appends a metavar when max nargs > 0
64  return it->second->get_usage_full().find(' ') != std::string::npos;
65  }

◆ subparsers()

const std::list<std::reference_wrapper<ArgumentParser> >& M3::MaCh3ArgumentParser::subparsers ( ) const
inline

Get the list of registered subparsers.

Returns
Reference to the list of subparsers

Definition at line 30 of file m3argparse.hpp.

30  {
31  return this->m_subparsers;
32  }

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