CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
cxxopts::ParseResult Class Reference

#include <cxxopts.h>

Public Member Functions

const std::vector< KeyValue > & arguments () const
 
size_t count (const std::string &o) const
 
const OptionValueoperator[] (const std::string &option) const
 
 ParseResult (const std::shared_ptr< std::unordered_map< std::string, std::shared_ptr< OptionDetails >>>, std::vector< std::string >, bool allow_unrecognised, int &, char **&)
 

Private Member Functions

void add_to_option (const std::string &option, const std::string &arg)
 
void checked_parse_arg (int argc, char *argv[], int &current, std::shared_ptr< OptionDetails > value, const std::string &name)
 
bool consume_positional (std::string a)
 
void parse (int &argc, char **&argv)
 
void parse_default (std::shared_ptr< OptionDetails > details)
 
void parse_option (std::shared_ptr< OptionDetails > value, const std::string &name, const std::string &arg="")
 

Private Attributes

bool m_allow_unrecognised
 
std::vector< std::string >::iterator m_next_positional
 
const std::shared_ptr< std::unordered_map< std::string, std::shared_ptr< OptionDetails > > > m_options
 
std::vector< std::string > m_positional
 
std::unordered_set< std::string > m_positional_set
 
std::unordered_map< std::shared_ptr< OptionDetails >, OptionValuem_results
 
std::vector< KeyValuem_sequential
 

Detailed Description

Definition at line 695 of file cxxopts.h.

Constructor & Destructor Documentation

◆ ParseResult()

cxxopts::ParseResult::ParseResult ( const std::shared_ptr< std::unordered_map< std::string, std::shared_ptr< OptionDetails >>>  options,
std::vector< std::string >  positional,
bool  allow_unrecognised,
int &  argc,
char **&  argv 
)
inline

Definition at line 943 of file cxxopts.h.

References dir2webdir::argc, GCPpyPlots::argv, and parse().

949  : m_options(options),
950  m_positional(std::move(positional)),
952  m_allow_unrecognised(allow_unrecognised) {
953  parse(argc, argv);
954  }
std::vector< std::string >::iterator m_next_positional
Definition: cxxopts.h:744
void parse(int &argc, char **&argv)
Definition: cxxopts.h:1078
bool m_allow_unrecognised
Definition: cxxopts.h:748
const std::shared_ptr< std::unordered_map< std::string, std::shared_ptr< OptionDetails > > > m_options
Definition: cxxopts.h:742
def move(src, dest)
Definition: eostools.py:511
std::vector< std::string > m_positional
Definition: cxxopts.h:743

Member Function Documentation

◆ add_to_option()

void cxxopts::ParseResult::add_to_option ( const std::string &  option,
const std::string &  arg 
)
inlineprivate

Definition at line 1023 of file cxxopts.h.

References m_options, fileinputsource_cfi::option, and parse_option().

Referenced by consume_positional().

1023  {
1024  auto iter = m_options->find(option);
1025 
1026  if (iter == m_options->end()) {
1027  throw Option_not_exists_exception(option);
1028  }
1029 
1030  parse_option(iter->second, option, arg);
1031  }
A arg
Definition: Factorize.h:31
const std::shared_ptr< std::unordered_map< std::string, std::shared_ptr< OptionDetails > > > m_options
Definition: cxxopts.h:742
void parse_option(std::shared_ptr< OptionDetails > value, const std::string &name, const std::string &arg="")
Definition: cxxopts.h:996

◆ arguments()

const std::vector<KeyValue>& cxxopts::ParseResult::arguments ( ) const
inline

Definition at line 726 of file cxxopts.h.

References m_sequential.

726 { return m_sequential; }
std::vector< KeyValue > m_sequential
Definition: cxxopts.h:750

◆ checked_parse_arg()

void cxxopts::ParseResult::checked_parse_arg ( int  argc,
char *  argv[],
int &  current,
std::shared_ptr< OptionDetails value,
const std::string &  name 
)
inlineprivate

Definition at line 1005 of file cxxopts.h.

References dir2webdir::argc, GCPpyPlots::argv, mergeVDriftHistosByStation::name, and parse_option().

Referenced by parse().

1006  {
1007  if (current + 1 >= argc) {
1008  if (value->value().has_implicit()) {
1009  parse_option(value, name, value->value().get_implicit_value());
1010  } else {
1011  throw Missing_argument_exception(name);
1012  }
1013  } else {
1014  if (value->value().has_implicit()) {
1015  parse_option(value, name, value->value().get_implicit_value());
1016  } else {
1017  parse_option(value, name, argv[current + 1]);
1018  ++current;
1019  }
1020  }
1021  }
Definition: value.py:1
void parse_option(std::shared_ptr< OptionDetails > value, const std::string &name, const std::string &arg="")
Definition: cxxopts.h:996

◆ consume_positional()

bool cxxopts::ParseResult::consume_positional ( std::string  a)
inlineprivate

Definition at line 1033 of file cxxopts.h.

References a, add_to_option(), m_next_positional, m_options, m_positional, m_results, and mps_fire::result.

Referenced by parse().

1033  {
1034  while (m_next_positional != m_positional.end()) {
1035  auto iter = m_options->find(*m_next_positional);
1036  if (iter != m_options->end()) {
1037  auto& result = m_results[iter->second];
1038  if (!iter->second->value().is_container()) {
1039  if (result.count() == 0) {
1042  return true;
1043  } else {
1045  continue;
1046  }
1047  } else {
1049  return true;
1050  }
1051  }
1053  }
1054 
1055  return false;
1056  }
std::vector< std::string >::iterator m_next_positional
Definition: cxxopts.h:744
void add_to_option(const std::string &option, const std::string &arg)
Definition: cxxopts.h:1023
std::unordered_map< std::shared_ptr< OptionDetails >, OptionValue > m_results
Definition: cxxopts.h:746
const std::shared_ptr< std::unordered_map< std::string, std::shared_ptr< OptionDetails > > > m_options
Definition: cxxopts.h:742
double a
Definition: hdecay.h:121
std::vector< std::string > m_positional
Definition: cxxopts.h:743

◆ count()

size_t cxxopts::ParseResult::count ( const std::string &  o) const
inline

Definition at line 703 of file cxxopts.h.

References m_options, m_results, and EcalTangentSkim_cfg::o.

703  {
704  auto iter = m_options->find(o);
705  if (iter == m_options->end()) {
706  return 0;
707  }
708 
709  auto riter = m_results.find(iter->second);
710 
711  return riter->second.count();
712  }
std::unordered_map< std::shared_ptr< OptionDetails >, OptionValue > m_results
Definition: cxxopts.h:746
const std::shared_ptr< std::unordered_map< std::string, std::shared_ptr< OptionDetails > > > m_options
Definition: cxxopts.h:742

◆ operator[]()

const OptionValue& cxxopts::ParseResult::operator[] ( const std::string &  option) const
inline

Definition at line 714 of file cxxopts.h.

References m_options, m_results, and fileinputsource_cfi::option.

714  {
715  auto iter = m_options->find(option);
716 
717  if (iter == m_options->end()) {
718  throw Option_not_present_exception(option);
719  }
720 
721  auto riter = m_results.find(iter->second);
722 
723  return riter->second;
724  }
std::unordered_map< std::shared_ptr< OptionDetails >, OptionValue > m_results
Definition: cxxopts.h:746
const std::shared_ptr< std::unordered_map< std::string, std::shared_ptr< OptionDetails > > > m_options
Definition: cxxopts.h:742

◆ parse()

void cxxopts::ParseResult::parse ( int &  argc,
char **&  argv 
)
inlineprivate

Definition at line 1078 of file cxxopts.h.

References dir2webdir::argc, GCPpyPlots::argv, checked_parse_arg(), consume_positional(), createIOVlist::detail, mps_fire::i, m_allow_unrecognised, m_options, m_results, mergeVDriftHistosByStation::name, das-up-to-nevents::opt, parse_default(), parse_option(), mps_fire::result, alignCSCRings::s, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by ParseResult().

1078  {
1079  int current = 1;
1080 
1081  int nextKeep = 1;
1082 
1083  bool consume_remaining = false;
1084 
1085  while (current != argc) {
1086  if (strcmp(argv[current], "--") == 0) {
1087  consume_remaining = true;
1088  ++current;
1089  break;
1090  }
1091 
1092  std::match_results<const char*> result;
1093  std::regex_match(argv[current], result, option_matcher);
1094 
1095  if (result.empty()) {
1096  //not a flag
1097 
1098  // but if it starts with a `-`, then it's an error
1099  if (argv[current][0] == '-' && argv[current][1] != '\0') {
1100  throw Option_syntax_exception(argv[current]);
1101  }
1102 
1103  //if true is returned here then it was consumed, otherwise it is
1104  //ignored
1105  if (consume_positional(argv[current])) {
1106  } else {
1107  argv[nextKeep] = argv[current];
1108  ++nextKeep;
1109  }
1110  //if we return from here then it was parsed successfully, so continue
1111  } else {
1112  //short or long option?
1113  if (result[4].length() != 0) {
1114  const std::string& s = result[4];
1115 
1116  for (std::size_t i = 0; i != s.size(); ++i) {
1117  std::string name(1, s[i]);
1118  auto iter = m_options->find(name);
1119 
1120  if (iter == m_options->end()) {
1121  if (m_allow_unrecognised) {
1122  continue;
1123  } else {
1124  //error
1125  throw Option_not_exists_exception(name);
1126  }
1127  }
1128 
1129  auto value = iter->second;
1130 
1131  if (i + 1 == s.size()) {
1132  //it must be the last argument
1133  checked_parse_arg(argc, argv, current, value, name);
1134  } else if (value->value().has_implicit()) {
1135  parse_option(value, name, value->value().get_implicit_value());
1136  } else {
1137  //error
1138  throw Option_requires_argument_exception(name);
1139  }
1140  }
1141  } else if (result[1].length() != 0) {
1142  const std::string& name = result[1];
1143 
1144  auto iter = m_options->find(name);
1145 
1146  if (iter == m_options->end()) {
1147  if (m_allow_unrecognised) {
1148  // keep unrecognised options in argument list, skip to next argument
1149  argv[nextKeep] = argv[current];
1150  ++nextKeep;
1151  ++current;
1152  continue;
1153  } else {
1154  //error
1155  throw Option_not_exists_exception(name);
1156  }
1157  }
1158 
1159  auto opt = iter->second;
1160 
1161  //equals provided for long option?
1162  if (result[2].length() != 0) {
1163  //parse the option given
1164 
1165  parse_option(opt, name, result[3]);
1166  } else {
1167  //parse the next argument
1168  checked_parse_arg(argc, argv, current, opt, name);
1169  }
1170  }
1171  }
1172 
1173  ++current;
1174  }
1175 
1176  for (auto& opt : *m_options) {
1177  auto& detail = opt.second;
1178  auto& value = detail->value();
1179 
1180  auto& store = m_results[detail];
1181 
1182  if (!store.count() && value.has_default()) {
1184  }
1185  }
1186 
1187  if (consume_remaining) {
1188  while (current < argc) {
1189  if (!consume_positional(argv[current])) {
1190  break;
1191  }
1192  ++current;
1193  }
1194 
1195  //adjust argv for any that couldn't be swallowed
1196  while (current != argc) {
1197  argv[nextKeep] = argv[current];
1198  ++nextKeep;
1199  ++current;
1200  }
1201  }
1202 
1203  argc = nextKeep;
1204  }
void checked_parse_arg(int argc, char *argv[], int &current, std::shared_ptr< OptionDetails > value, const std::string &name)
Definition: cxxopts.h:1005
bool consume_positional(std::string a)
Definition: cxxopts.h:1033
void parse_default(std::shared_ptr< OptionDetails > details)
Definition: cxxopts.h:992
std::unordered_map< std::shared_ptr< OptionDetails >, OptionValue > m_results
Definition: cxxopts.h:746
bool m_allow_unrecognised
Definition: cxxopts.h:748
Definition: value.py:1
const std::shared_ptr< std::unordered_map< std::string, std::shared_ptr< OptionDetails > > > m_options
Definition: cxxopts.h:742
void parse_option(std::shared_ptr< OptionDetails > value, const std::string &name, const std::string &arg="")
Definition: cxxopts.h:996

◆ parse_default()

void cxxopts::ParseResult::parse_default ( std::shared_ptr< OptionDetails details)
inlineprivate

Definition at line 992 of file cxxopts.h.

References m_results.

Referenced by parse().

992  {
993  m_results[details].parse_default(details);
994  }
std::unordered_map< std::shared_ptr< OptionDetails >, OptionValue > m_results
Definition: cxxopts.h:746
Definition: helper.h:56

◆ parse_option()

void cxxopts::ParseResult::parse_option ( std::shared_ptr< OptionDetails value,
const std::string &  name,
const std::string &  arg = "" 
)
inlineprivate

Definition at line 996 of file cxxopts.h.

References m_results, m_sequential, mps_fire::result, and cxxopts::value().

Referenced by add_to_option(), checked_parse_arg(), and parse().

998  {
999  auto& result = m_results[value];
1000  result.parse(value, arg);
1001 
1002  m_sequential.emplace_back(value->long_name(), arg);
1003  }
std::unordered_map< std::shared_ptr< OptionDetails >, OptionValue > m_results
Definition: cxxopts.h:746
A arg
Definition: Factorize.h:31
std::vector< KeyValue > m_sequential
Definition: cxxopts.h:750
std::shared_ptr< Value > value()
Definition: cxxopts.h:574
Definition: value.py:1

Member Data Documentation

◆ m_allow_unrecognised

bool cxxopts::ParseResult::m_allow_unrecognised
private

Definition at line 748 of file cxxopts.h.

Referenced by parse().

◆ m_next_positional

std::vector<std::string>::iterator cxxopts::ParseResult::m_next_positional
private

Definition at line 744 of file cxxopts.h.

Referenced by consume_positional().

◆ m_options

const std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<OptionDetails> > > cxxopts::ParseResult::m_options
private

Definition at line 742 of file cxxopts.h.

Referenced by add_to_option(), consume_positional(), count(), operator[](), and parse().

◆ m_positional

std::vector<std::string> cxxopts::ParseResult::m_positional
private

Definition at line 743 of file cxxopts.h.

Referenced by consume_positional().

◆ m_positional_set

std::unordered_set<std::string> cxxopts::ParseResult::m_positional_set
private

Definition at line 745 of file cxxopts.h.

◆ m_results

std::unordered_map<std::shared_ptr<OptionDetails>, OptionValue> cxxopts::ParseResult::m_results
private

Definition at line 746 of file cxxopts.h.

Referenced by consume_positional(), count(), operator[](), parse(), parse_default(), and parse_option().

◆ m_sequential

std::vector<KeyValue> cxxopts::ParseResult::m_sequential
private

Definition at line 750 of file cxxopts.h.

Referenced by arguments(), and parse_option().