MaCh3  2.8.0
Reference Guide
Public Member Functions | Private Attributes | 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::string & description () const
 Get the description 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...
 
std::vector< std::string > get_completions (const std::string &prefix, const std::vector< std::string > &context) const
 Complete subcommands or long options at any depth without parsing incomplete arguments. More...
 
void set_default_subcommand (const std::string &name)
 Set a subcommand to use when none of the registered subcommands is given. More...
 
void insert_default_subcommands (std::vector< std::string > &args, std::size_t pos=0) const
 Insert default subcommands into args where no explicit subcommand was given. More...
 

Private Attributes

std::string m_default_subcommand
 

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

◆ description()

const std::string& M3::MaCh3ArgumentParser::description ( ) const
inline

Get the description of this parser/subcommand.

Returns
The description string

Definition at line 30 of file m3argparse.hpp.

30  {
31  return this->m_description;
32  }

◆ get_completions()

std::vector<std::string> M3::MaCh3ArgumentParser::get_completions ( const std::string &  prefix,
const std::vector< std::string > &  context 
) const
inline

Complete subcommands or long options at any depth without parsing incomplete arguments.

Returns
Matching names, or an empty list to allow shell file completion

Definition at line 75 of file m3argparse.hpp.

76  {
77  const MaCh3ArgumentParser* parser = this;
78  bool positional = false;
79  for (std::size_t i = 0; i < context.size(); ++i) {
80  const auto& word = context[i];
81  if (word == "--") return {};
82 
83  if (!positional) {
84  auto sub = parser->m_subparser_map.find(word);
85  if (sub != parser->m_subparser_map.end()) {
86  parser = &static_cast<const MaCh3ArgumentParser&>(sub->second->get());
87  continue;
88  }
89  auto fallback = parser->m_subparser_map.find(parser->m_default_subcommand);
90  if (fallback != parser->m_subparser_map.end())
91  parser = &static_cast<const MaCh3ArgumentParser&>(fallback->second->get());
92  }
93 
94  if (!word.empty() && word[0] == '-') {
95  const auto equals = word.find('=');
96  if (equals == std::string::npos && parser->option_takes_value(word)) {
97  if (++i == context.size()) return {};
98  }
99  } else {
100  // Once positional arguments start, later filenames are not subcommands.
101  positional = true;
102  }
103  }
104 
105  std::vector<std::string> names;
106  if (!prefix.empty() && prefix[0] == '-') {
107  auto fallback = parser->m_subparser_map.find(parser->m_default_subcommand);
108  if (fallback != parser->m_subparser_map.end())
109  parser = &static_cast<const MaCh3ArgumentParser&>(fallback->second->get());
110  for (const auto& name : parser->get_long_option_names()) {
111  if (name.rfind(prefix, 0) == 0) names.push_back(name);
112  }
113  } else if (!positional) {
114  for (const auto& [name, sub] : parser->m_subparser_map) {
115  const auto& child = static_cast<const MaCh3ArgumentParser&>(sub->get());
116  if (!child.m_suppress && name.rfind(prefix, 0) == 0)
117  names.push_back(name);
118  }
119  }
120  return names;
121  }
const std::string name() const
Get the name of this parser/subcommand.
Definition: m3argparse.hpp:24

◆ 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 56 of file m3argparse.hpp.

56  {
57  std::vector<std::string> names;
58  for (const auto& [name, _] : this->m_argument_map) {
59  if (name.size() >= 2 && name[0] == '-' && name[1] == '-')
60  names.push_back(name);
61  }
62  return names;
63  }

◆ 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 46 of file m3argparse.hpp.

46  {
47  for (const std::reference_wrapper<ArgumentParser>& subparser : this->m_subparsers) {
48  if (this->is_subcommand_used(subparser.get())) {
49  return static_cast<MaCh3ArgumentParser&>(subparser.get()).get_subcommand_used();
50  }
51  }
52  return (*this);
53  }
const MaCh3ArgumentParser & get_subcommand_used() const
Get the subcommand that was used in parsing.
Definition: m3argparse.hpp:46

◆ insert_default_subcommands()

void M3::MaCh3ArgumentParser::insert_default_subcommands ( std::vector< std::string > &  args,
std::size_t  pos = 0 
) const
inline

Insert default subcommands into args where no explicit subcommand was given.

Parameters
argsFull argument list
posIndex in args of this parser's own name (0 for the program itself)

Definition at line 132 of file m3argparse.hpp.

132  {
133  const std::size_t next = pos + 1;
134  if (next < args.size()) {
135  auto it = this->m_subparser_map.find(args[next]);
136  if (it != this->m_subparser_map.end()) {
137  auto& sub = static_cast<MaCh3ArgumentParser&>(it->second->get());
138  // argparse only prefixes the immediate parent's name, so rebuild the full path
139  sub.m_parser_path = this->m_parser_path + " " + sub.m_program_name;
140  sub.insert_default_subcommands(args, next);
141  return;
142  }
143  }
144  if (!m_default_subcommand.empty()) {
145  auto it = this->m_subparser_map.find(m_default_subcommand);
146  if (it != this->m_subparser_map.end()) {
147  // hide the default subcommand name in its usage/help line
148  static_cast<MaCh3ArgumentParser&>(it->second->get()).m_parser_path = this->m_parser_path;
149  }
150  args.insert(args.begin() + static_cast<std::ptrdiff_t>(next), m_default_subcommand);
151  }
152  }
std::string m_default_subcommand
Definition: m3argparse.hpp:155

◆ 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 66 of file m3argparse.hpp.

66  {
67  auto it = this->m_argument_map.find(name);
68  if (it == this->m_argument_map.end()) return false;
69  // get_usage_full() appends a metavar when max nargs > 0
70  return it->second->get_usage_full().find(' ') != std::string::npos;
71  }

◆ set_default_subcommand()

void M3::MaCh3ArgumentParser::set_default_subcommand ( const std::string &  name)
inline

Set a subcommand to use when none of the registered subcommands is given.

Parameters
nameName of the default subcommand

Definition at line 125 of file m3argparse.hpp.

125  {
127  }

◆ 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 36 of file m3argparse.hpp.

36  {
37  return this->m_subparsers;
38  }

Member Data Documentation

◆ m_default_subcommand

std::string M3::MaCh3ArgumentParser::m_default_subcommand
private

Definition at line 155 of file m3argparse.hpp.


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