5 #include <argparse/argparse.hpp>
7 using argparse::ArgumentParser;
19 using ArgumentParser::ArgumentParser;
24 const std::string
name()
const{
25 return this->m_program_name;
31 return this->m_description;
36 const std::list<std::reference_wrapper<ArgumentParser>>&
subparsers()
const{
37 return this->m_subparsers;
47 for (
const std::reference_wrapper<ArgumentParser>& subparser : this->m_subparsers) {
48 if (this->is_subcommand_used(subparser.get())) {
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);
67 auto it = this->m_argument_map.find(
name);
68 if (it == this->m_argument_map.end())
return false;
70 return it->second->get_usage_full().find(
' ') != std::string::npos;
76 const std::vector<std::string>& context)
const {
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 {};
84 auto sub = parser->m_subparser_map.find(word);
85 if (sub != parser->m_subparser_map.end()) {
90 if (fallback != parser->m_subparser_map.end())
94 if (!word.empty() && word[0] ==
'-') {
95 const auto equals = word.find(
'=');
97 if (++i == context.size())
return {};
105 std::vector<std::string> names;
106 if (!prefix.empty() && prefix[0] ==
'-') {
108 if (fallback != parser->m_subparser_map.end())
111 if (
name.rfind(prefix, 0) == 0) names.push_back(
name);
113 }
else if (!positional) {
114 for (
const auto& [
name, sub] : parser->m_subparser_map) {
116 if (!child.m_suppress &&
name.rfind(prefix, 0) == 0)
117 names.push_back(
name);
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()) {
139 sub.m_parser_path = this->m_parser_path +
" " + sub.m_program_name;
146 if (it != this->m_subparser_map.end()) {
Extended ArgumentParser with MaCh3-specific functionality.
const std::string name() const
Get the name of this parser/subcommand.
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.
const std::string & description() const
Get the description of this parser/subcommand.
std::string m_default_subcommand
virtual ~MaCh3ArgumentParser()=default
std::vector< std::string > get_long_option_names() const
Return all long option names (–xxx) registered with this parser.
const std::list< std::reference_wrapper< ArgumentParser > > & subparsers() const
Get the list of registered subparsers.
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.
bool option_takes_value(const std::string &name) const
Return true if the named option expects a value (not a flag)
const MaCh3ArgumentParser & get_subcommand_used() const
Get the subcommand that was used in parsing.
void set_default_subcommand(const std::string &name)
Set a subcommand to use when none of the registered subcommands is given.
Main namespace for MaCh3 software.