MaCh3  2.8.0
Reference Guide
m3argparse.hpp
Go to the documentation of this file.
1 
4 #pragma once
5 #include <argparse/argparse.hpp>
6 
7 using argparse::ArgumentParser;
8 
9 namespace M3 {
10 
17  class MaCh3ArgumentParser: public ArgumentParser{
18  public:
19  using ArgumentParser::ArgumentParser;
20  virtual ~MaCh3ArgumentParser() = default;
21 
24  const std::string name() const{
25  return this->m_program_name;
26  }
27 
30  const std::list<std::reference_wrapper<ArgumentParser>>& subparsers() const{
31  return this->m_subparsers;
32  }
33 
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  }
48 
50  std::vector<std::string> get_long_option_names() const {
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  }
58 
60  bool option_takes_value(const std::string& name) const {
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  }
66  };
67 }
Extended ArgumentParser with MaCh3-specific functionality.
Definition: m3argparse.hpp:17
const std::string name() const
Get the name of this parser/subcommand.
Definition: m3argparse.hpp:24
virtual ~MaCh3ArgumentParser()=default
std::vector< std::string > get_long_option_names() const
Return all long option names (–xxx) registered with this parser.
Definition: m3argparse.hpp:50
const std::list< std::reference_wrapper< ArgumentParser > > & subparsers() const
Get the list of registered subparsers.
Definition: m3argparse.hpp:30
bool option_takes_value(const std::string &name) const
Return true if the named option expects a value (not a flag)
Definition: m3argparse.hpp:60
const MaCh3ArgumentParser & get_subcommand_used() const
Get the subcommand that was used in parsing.
Definition: m3argparse.hpp:40
Main namespace for MaCh3 software.