Darwin  1.0
Event loop based prototype framework
Options

Description

Common class to interpret the command line, based on Boost Program Options.

Options may also be partly provided via a configuration file. Additional options to split the input file are also possible. Minimal working example:

namespace DT = Darwin::Tools;
DT::Options options("Global description.", DT::config | DT::split);
options.input ("input" , &input , "input ROOT file" );
options.output("output", &output, "output ROOT file");
options.arg<int>("myArg", "correction.here", "my description");
options.args("allOtherArgs", "correction.there.list", "garbage collector");
auto const& config = options(argc, argv);

where the config is a Boost Property Tree. A one-liner is even possible:

auto const& config = DT::Options("Global description.", DT::config | DT::split).input("input", &input, "input ROOT file").output("output", &output, "output ROOT file").arg<int>("myArg", "correction.here", "my description").args("allOtherArgs", "correction.there.list", "garbage collector")(argc, argv);

The order matters only for the positional options, where input(s) should be given first, then output(s), then additional positional options. The position of explicit options (e.g. -v) does not matter. Options::args may be used as a garbage collector for extra positional options.

#include <Options.h>

+ Collaboration diagram for Options:

Public Member Functions

 Options (const std::string &, int=none, const char *commit=DARWIN_GIT_COMMIT, const char *example=DARWIN_EXAMPLE)
 
 Options ()=delete
 
Optionsinput (const char *, std::filesystem::path *, const char *, const std::vector< std::string > &={".root", ".xml"})
 
Optionsinputs (const char *, std::vector< std::filesystem::path > *, const char *, const std::vector< std::string > &={".root", ".xml"})
 
Optionsoutput (const char *, std::filesystem::path *, const char *, const std::vector< std::string > &={".root", ".xml"})
 
template<typename T >
Optionsarg (const char *name, const char *configpath, const char *desc)
 
Optionsargs (const char *name, const char *configpath, const char *desc)
 
const boost::property_tree::ptree & operator() (int, const char *const [])
 
auto commit () const
 
int steering () const
 
std::pair< unsigned, unsigned > slice () const
 

Static Public Member Functions

static std::string parse_env_var (std::string)
 
static const char * parse_env_var (const char *p)
 
static std::filesystem::path parse_env_var (const std::filesystem::path &p)
 
static void parse_config (boost::property_tree::ptree &, std::string="")
 

Static Public Attributes

static std::string full_cmd
 

Private Types

enum  Stage { Input, Output, Arg, Args }
 

Private Member Functions

void parse_helper (int, const char *const [])
 
void parse_common (int, const char *const [])
 
void parse_custom (int, const char *const [])
 
Optionsset (const char *, const boost::program_options::value_semantic *, const char *)
 
template<typename T >
std::function< void(T)> put (const char *configpath)
 
bool allow_unregistered () const
 
std::string exec (const std::string &)
 

Static Private Member Functions

static void check_input (const std::filesystem::path &)
 
static void check_output (const std::filesystem::path &)
 

Private Attributes

boost::program_options::options_description hidden
 
boost::program_options::options_description helper
 
boost::program_options::options_description common
 
boost::program_options::options_description custom
 
boost::program_options::positional_options_description pos_hide
 
boost::property_tree::ptree pt_conf
 
const std::filesystem::path example
 
std::filesystem::path config_file
 
const std::string tutorial
 
std::string synopsis
 
std::vector< std::string > names
 
std::vector< std::string > configpaths
 
std::optional< unsigned > registered
 
Stage stage
 
const int params
 
const std::string m_commit
 
int steer
 
unsigned j
 
unsigned k
 
std::vector< std::vector< std::string > > inputExt
 
std::vector< std::vector< std::string > > outputExt
 

Static Private Attributes

static std::filesystem::path prefix
 

Member Enumeration Documentation

◆ Stage

enum Stage
private
Enumerator
Input 

first the inputs

Output 

then the outputs (inputs are no longer allowed)

Arg 

then the registered arguments (inputs and outputs no longer allowed)

Args 

finally the remaining arguments / garbage collector (must be very last)

144  {
145  Input,
146  Output,
147  Arg,
148  Args
149  };

Constructor & Destructor Documentation

◆ Options() [1/2]

Options ( const std::string &  tuto,
int  pars = none,
const char *  commit = DARWIN_GIT_COMMIT,
const char *  example = DARWIN_EXAMPLE 
)

Constructor:

  • contains a parser for the help itself
  • contains a parser for the options, like config file and verbosity
  • and contains a parser for the input, output, and any other option as positional arguments
Parameters
tutodescription, to be displayed in the helper
parsflags for explicit, generic command options (see enum)
commitgit commit for --git
examplepath to example
102  : hidden{"Hidden"}, helper{"Helper"}, common{"Options"}, custom{"Positional options"},
103  tutorial(tuto), stage(Stage::Input), params(pars),
104  m_commit(commit ? commit : DARWIN_VERSION), steer(0), j(1), k(0),
105  example(example ? example : DARWIN "/test/example.info")
106 {
107  if ((params & split) == split) hidden.add_options()
108  ("prefix,p", po::value<fs::path>(&prefix), "Prefix command, `parallel` or `submit` (for history only)");
109 
110  // first the helper
111  helper.add_options()
112  ("help,h", "Help screen (what you are seeing right now)")
113  ("tutorial,t", "Brief description of the command's purpose")
114  ("git,g", "Commit hash of this executable at compilation time")
115  ("input_ext,i", "Expected extension for input(s) (one line for each input)")
116  ("output_ext,o", "Expected extension for output(s) (one line for each output)");
117  if (params & config) helper.add_options()
118  ("example,e", "Print config example");
119 
120  // then the running options
121  common.add_options()
122  ("verbose,v", po::bool_switch()->default_value(false),
123  "Enable debug mode (typically in slow operations such as the event loop)")
124  ("mute,m", po::bool_switch()->default_value(false)->notifier(set_mute),
125  "Disable standard output stream");
126  if (params & config) common.add_options()
127  ("config,c", po::value<fs::path>(&config_file),
128  "Configuration file in INFO, JSON, or XML format");
129  if (params & split) common.add_options()
130  ("nSplit,j", po::value<unsigned>(&j)->default_value(j), "Number of slices")
131  ("nNow,k" , po::value<unsigned>(&k)->default_value(k), "Index of current slice");
132  if (params & fill) common.add_options()
133  ("fill,f", po::bool_switch()->default_value(false), "Enable `TTree::Fill()`");
134  if (params & Friend) common.add_options()
135  ("Friend,F", po::bool_switch()->default_value(false),
136  "Use `TTree` friends instead of cloning the whole input `TTree`");
137  if (params & syst) common.add_options()
138  ("syst,s", po::bool_switch()->default_value(false),
139  "Enable systematic variations");
140 
141  // positional arguments should be defined by hand by the user
142  // with the various public members
143 }

◆ Options() [2/2]

Options ( )
delete

No default constructor is allowed.

Member Function Documentation

◆ allow_unregistered()

bool allow_unregistered ( ) const
inlineprivate
Returns
true if Options::registered has been initialised.
137 { return bool(registered); }

◆ arg()

Options& arg ( const char *  name,
const char *  configpath,
const char *  desc 
)
inline

Member to add an argument. It can be called several times in a row. Each argument may be provided via the configuration file too.

Returns
the object itself, so that the arguments can be given in a row.
Parameters
namename of the option, will be shown in helper
configpathpath in config
descdescription, shown in helper too
224  {
225  if (stage > Stage::Arg)
226  BOOST_THROW_EXCEPTION(std::runtime_error(
227  "Not possible to add another argument at this stage"));
228  stage = Stage::Arg;
229 
230  names.push_back(name);
231  configpaths.push_back(configpath);
232  const boost::program_options::value_semantic * s =
233  boost::program_options::value<T>()->notifier(put<T>(configpath));
234  return set(name, s, desc);
235  }

◆ args()

Options & args ( const char *  name,
const char *  configpath,
const char *  desc 
)

Member to collect any addition arguments provided in command line. It should be called maximum once. The additional options will be collected as a list of strings.

Parameters
namegeneric name for unregistered options
configpathpath in config.
descdescription, shown in helper too
518 {
519  if (stage >= Stage::Args)
520  BOOST_THROW_EXCEPTION(runtime_error("Only one garbage collector is possible"));
521  stage = Stage::Args;
522 
523  synopsis += " [" + string(name) + "...]";
524  configpaths.push_back(configpath);
525 
526  registered = pos_hide.max_total_count();
527  const po::value_semantic * s =
528  po::value<vector<string>>()->multitoken()->zero_tokens();
529  custom.add_options()(name, s, desc);
530  pos_hide.add(name, -1);
531 
532  return *this;
533 }

◆ check_input()

void check_input ( const std::filesystem::path &  input)
staticprivate

Function used by Boost Program Options to check if the file does exist, and if yes, if it is readable and readable.

Parameters
inputpath to input
38 {
39  if (!fs::exists(input))
40  BOOST_THROW_EXCEPTION(fs::filesystem_error("Bad input", input,
41  make_error_code(errc::no_such_file_or_directory)));
42 
43  full_cmd += ' ' + fs::canonical(input).string();
44 
45  if ((fs::status(input).permissions() & fs::perms::owner_read) == fs::perms::none)
46  BOOST_THROW_EXCEPTION(fs::filesystem_error("Input exists but cannot be read",
47  input, make_error_code(errc::permission_denied)));
48 }

◆ check_output()

void check_output ( const std::filesystem::path &  output)
staticprivate

Function used by Boost Program Options to check if the file may already exist, and if yes, then if it is writable, and not a directory. (At this stage, the automatic naming of the output according to the name of the input should not happen. Such a thing should rather be done in the executable directly.)

Parameters
outputpath to output
51 {
52  fs::path outputHist = output;
53  if (!prefix.empty() && !fs::is_directory(output)) outputHist.remove_filename();
54  full_cmd += ' ' + outputHist.string();
55 
56  if (!fs::exists(output)) return;
57 
58  if ((fs::status(output).permissions() & fs::perms::owner_write)
59  == fs::perms::none) {
60  full_cmd.clear();
61  BOOST_THROW_EXCEPTION(fs::filesystem_error(
62  "Output already exists and cannot be overwritten",
63  output, make_error_code(errc::permission_denied)));
64  }
65 
66  if (fs::is_directory(output) && fs::equivalent(output,".")) {
67  full_cmd.clear();
68  BOOST_THROW_EXCEPTION(fs::filesystem_error("Not a valid output",
69  output, make_error_code(errc::invalid_argument)));
70  }
71  else if (output != "/dev/null")
72  cerr << orange << "Warning: you are overwriting " << output << def << '\n';
73 }

◆ commit()

auto commit ( ) const
inline

Git commit information.

279 { return m_commit; }

◆ exec()

std::string exec ( const std::string &  cmd)
private
Returns
output from command given to shell
Parameters
cmdcmd to run in the shell
443 {
444  string result;
445  char buffer[128];
446  cerr << red;
447  FILE * pipe = popen(cmd.c_str(), "r");
448  while (!feof(pipe))
449  if (fgets(buffer, 128, pipe) != NULL)
450  result += buffer;
451  pclose(pipe);
452  cerr << def;
453  return result;
454 }

◆ input()

Options & input ( const char *  name,
std::filesystem::path *  file,
const char *  desc,
const std::vector< std::string > &  ext = {".root", ".xml"} 
)

expected file extension

Member to add an input. It can be called several times in a row. This option should always be provided from the command line, and never via the config file.

Returns
the object itself, so that the arguments can be given in a row.
Parameters
namename of the option, will be shown in helper
filepath pointer to file
descdescription, shown in helper too
431 {
432  if (stage > Stage::Input)
433  BOOST_THROW_EXCEPTION(runtime_error(
434  "Not possible to add another input at this stage"));
435 
436  inputExt.push_back(ext);
437  const po::value_semantic * s =
438  po::value<fs::path>(file)->notifier(check_input)->required();
439  return set(name, s, desc);
440 }

◆ inputs()

Options & inputs ( const char *  name,
std::vector< std::filesystem::path > *  files,
const char *  desc,
const std::vector< std::string > &  ext = {".root", ".xml"} 
)

expected file extension

Member to add an undefined amount of input. It can be called only once. This option should always be provided from the command line, and never via the config file. There can be no garbage collector in addition.

Returns
the object itself, so that the arguments can be given in a row.
Parameters
namename of the options, will be shown in helper
filespaths to files
descdescription, shown in helper too
459 {
460  if (stage > Stage::Input)
461  BOOST_THROW_EXCEPTION(runtime_error(
462  "Not possible to add another input at this stage"));
463 
464  auto store = [files,this](const string& pathRegex) {
465 
466  const string& ls = exec("ls -1d " + pathRegex);
467 
468  vector<fs::path> paths;
469  al::split(paths, ls, al::is_any_of("\n"), al::token_compress_on);
470  for (fs::path const& p: paths) {
471  if (!fs::exists(p)) continue;
472  files->push_back(p);
473  }
474 
475  if (files->empty()) {
476  fs::path p = pathRegex;
477  full_cmd.clear();
478  BOOST_THROW_EXCEPTION(
479  fs::filesystem_error("No input file could be found", p,
480  make_error_code(errc::no_such_file_or_directory)));
481  }
482 
483  auto inputs = accumulate(files->begin(), files->end(), string(),
484  [](string inputs, fs::path input) {
485  return inputs + fs::canonical(input).string() + ' '; });
486  inputs.resize(inputs.size()-1);
487  if (files->size() > 1)
488  inputs = '"' + inputs + '"';
489  full_cmd += ' ' + inputs;
490  };
491 
492  inputExt.push_back(ext);
493  const po::value_semantic * s =
494  po::value<string>()->notifier(store)->required();
495  string full_desc = desc;
496  full_desc += " (use a regular expression, surrounded by quotation marks)";
497  return set(name, s, full_desc.c_str());
498 }

◆ operator()()

const boost::property_tree::ptree & operator() ( int  argc,
const char * const  argv[] 
)

Unique parser accessible by the user of the class. It should be directly given argc and argv from the main function.

391 {
392  pt_conf.clear();
393  full_cmd.clear();
394  prefix.clear();
395 
396  try {
397  parse_helper(argc, argv);
398  parse_common(argc, argv);
399  parse_custom(argc, argv);
401 
402  if (steer & split && j > 1) full_cmd += " -j " + to_string(j);
403  if (steer & syst ) full_cmd += " -s";
404  if (steer & fill ) full_cmd += " -f";
405  else if (steer & Friend ) full_cmd += " -F";
406 
407  pt_conf.put<string>("history", full_cmd);
408  }
409  catch (const po::error& e) {
410  BOOST_THROW_EXCEPTION(po::error(e.what()));
411  }
412 
413  return pt_conf;
414 }

◆ output()

Options & output ( const char *  name,
std::filesystem::path *  file,
const char *  desc,
const std::vector< std::string > &  ext = {".root", ".xml"} 
)

expected file extension

Member to add an output. It can be called several times in a row. This option should always be provided from the command line, and never via the config file.

Returns
the object itself, so that the arguments can be given in a row.
Parameters
namename of the option, will be shown in helper
filepath pointer to file
descdescription, shown in helper too
502 {
503  if (stage > Stage::Output) {
504  full_cmd.clear();
505  BOOST_THROW_EXCEPTION(runtime_error(
506  "Not possible to add another output at this stage"));
507  }
508  stage = Stage::Output;
509 
510  outputExt.push_back(ext);
511  const po::value_semantic * s =
512  po::value<fs::path>(file)->notifier(check_output)->required();
513 
514  return set(name, s, desc);
515 }

◆ parse_common()

void parse_common ( int  argc,
const char * const  argv[] 
)
private

Parser for generic options, such as the config (with -c) or slices. It should be directly given argc and argv from the main function.

266 {
267  po::options_description cmd_line;
268  cmd_line.add(common)
269  .add(hidden);
270 
271  po::command_line_parser parser{argc, argv};
272  parser.options(cmd_line)
273  .allow_unregistered();
274 
275  po::variables_map vm;
276  po::store(parser.run(), vm);
277  po::notify(vm);
278 
279  if (vm.count("config")) {
280  const string ext = config_file.extension();
281  if (ext == ".json") pt::read_json(config_file.c_str(), pt_conf);
282  else if (ext == ".info") pt::read_info(config_file.c_str(), pt_conf);
283  else if (ext == ".xml" ) {
284  pt::ptree userinfo;
285  pt::read_xml(config_file.c_str(), userinfo);
286  pt_conf = userinfo.get_child("userinfo");
287  }
288  else BOOST_THROW_EXCEPTION(
289  fs::filesystem_error("Extension should be .json, .xml, or .info",
290  config_file, make_error_code(errc::invalid_argument)));
291  }
292 
293  if ((params & fill ) && vm["fill" ].as<bool>()) steer |= fill ;
294  if ((params & Friend ) && vm["Friend" ].as<bool>()) steer |= Friend ;
295  if ((params & syst ) && vm["syst" ].as<bool>()) steer |= syst ;
296  if ( vm["verbose"].as<bool>()) steer |= verbose;
297 }

◆ parse_config()

void parse_config ( boost::property_tree::ptree &  ,
std::string  = "" 
)
static

Parse config if given. This method calls itself to parse the tree structure.

key in config (for recursive call)

373 {
374  // loop over the config and parse environment variables
375  for (auto& it: tree) {
376  string subkey = it.first;
377  subkey = parse_env_var(subkey);
378  pt::ptree& child = it.second;
379  auto value = tree.get_optional<string>(subkey);
380  string newkey = key + (key.empty() ? "" : ".") + subkey;
381  if (value) {
382  *value = parse_env_var(*value);
383  tree.put<string>(subkey, *value);
384  }
385  parse_config(child, newkey);
386  }
387 }

◆ parse_custom()

void parse_custom ( int  argc,
const char * const  argv[] 
)
private

Parser for options provided with args(), defined differently in each application. It should be directly given argc and argv from the main function.

300 {
301  po::options_description cmd_line;
302  cmd_line.add(common) // only here to avoid using `allow_unregistered()`
303  .add(hidden) // idem
304  .add(custom);
305 
306  po::command_line_parser parser{argc, argv};
307  parser.options(cmd_line)
308  .positional(pos_hide);
309 
310  po::variables_map vm;
311  po::parsed_options parsed = parser.run();
312  po::store(parsed, vm);
313 
314  // if no config file is given, then options are all required
315  if (!fs::exists(config_file))
316  for (auto& name: names) {
317  if (vm.count(name)) continue;
318  throw po::required_option(name);
319  }
320 
321  if (!prefix.empty())
322  full_cmd = prefix.string() + ' ';
323  full_cmd += argv[0];
324 
325  po::notify(vm); // necessary for config to be given the value from the cmd line
326  // note: `full_cmd` is modified here
327 
328  // first fetch values from config (some may be overwritten later)
329  for (auto& configpath: configpaths) {
330  auto arg = pt_conf.get_optional<string>(configpath);
331  if (!arg) continue;
332  al::erase_all(*arg, "\n");
333  al::erase_all(*arg, " ");
334  al::erase_all(*arg, "\"");
335  if (arg->empty()) continue;
336  full_cmd += ' ' + *arg;
337  }
338 
339  if (allow_unregistered()) { // garbage collector
340 
341  string configpath = configpaths.back();
342  if (!pt_conf.count(configpath))
343  pt_conf.add<string>(configpath, "");
344  auto& subtree = pt_conf.get_child(configpath);
345 
346  for (auto& item: subtree) {
347  string key = item.first,
348  value = item.second.get_value<string>();
349  if (key.empty() || key == "item")
350  full_cmd += ' ' + value;
351  else if (value.empty())
352  full_cmd += ' ' + key;
353  }
354 
355  vector<string> items = po::collect_unrecognized(parsed.options,
356  po::include_positional);
357  if (items.size() > *registered) {
358 
359  items.erase(items.begin(), items.begin() + *registered);
360 
361  for (auto& item: items) {
362  if (subtree.count(item)) continue;
363  subtree.add<string>(item, "");
364  full_cmd += ' ' + item;
365  }
366  }
367  } // end of garbage collector
368 
370 }

◆ parse_env_var() [1/3]

static const char* parse_env_var ( const char *  p)
inlinestatic

Parse environment variable in C-style string.

261  { return parse_env_var(std::string(p)).c_str(); }

◆ parse_env_var() [2/3]

static std::filesystem::path parse_env_var ( const std::filesystem::path &  p)
inlinestatic

Parse environment variable in a path.

266  { return std::filesystem::path(parse_env_var(p.string())); }

◆ parse_env_var() [3/3]

string parse_env_var ( std::string  s)
static

Parse environment variable in string. Adapted from https://codereview.stackexchange.com/questions/172644/c-environment-variable-expansion

Remarks
Curly brackets are needed (e.g. ${VAR}, not just $VAR)
76 {
77  static const regex env_re{R"--(\$\{([^}]+)\})--"};
78  smatch match;
79  while (regex_search(s, match, env_re)) {
80  auto const from = match[0];
81  auto const name = match[1].str();
82  auto const value = getenv(name.c_str());
83  if (!value)
84  BOOST_THROW_EXCEPTION(runtime_error(
85  "Environment variable ${" + name + "} does not exist."));
86  s.replace(from.first, from.second, value);
87  }
88  return s;
89 }

◆ parse_helper()

void parse_helper ( int  argc,
const char * const  argv[] 
)
private

First parser that is called. As soon as -h is given, or if the command is run without options, then the helper is shown. It should be directly given argc and argv from the main function.

183 {
184  po::command_line_parser parser{argc, argv};
185  parser.options(helper) // parse only the helper, but no other option at this stage
186  .allow_unregistered(); // ignore unregistered options
187 
188  // defines actions
189  po::variables_map vm;
190  po::store(parser.run(), vm);
191  po::notify(vm); // necessary for config to be given the value from the cmd line
192 
193  if (vm.count("help") || argc == 1) {
194  fs::path executable = argv[0];
195  cout << bold << executable.filename().string() << synopsis << def
196  << "\nwhere";
197  for (const auto& option: custom.options())
198  cout << '\t' << option->long_name()
199  << " = " << option->description() << '\n';
200  }
201 
202  if (vm.count("help")) {
203  po::options_description cmd_line;
204  cmd_line.add(helper)
205  .add(common); // only used to display the helper message,
206  // but not to parse
207  cout << cmd_line << endl;
208  }
209 
210  if (vm.count("tutorial"))
211  cout << wrap_paragraph(tutorial) << '\n'
212  << wrap_paragraph("General remarks: "
213  "unless stated otherwise, all options except the input and output files "
214  "may be given either from the command line or from such a config. Unused "
215  "options in the config are simply ignored. The `flags` should be set at "
216  "the creation of the n-tuples; the `corrections` should be added step "
217  "by step (typically a couple of corrections per executable at most). How "
218  "the config is parsed may change from executable to executable. This "
219  "example uses the Boost INFO format, but JSON and XML formats are also "
220  "possible. It is possible to extract such a config from an existing ROOT "
221  "file by using `getMetaInfo`. Arguments from command line overwrite "
222  "arguments from the config.") << endl;
223 
224  if (vm.count("git"))
225  cout << commit() << endl;
226 
227  if (vm.count("example")) {
228  if (!fs::exists(example))
229  BOOST_THROW_EXCEPTION(
230  fs::filesystem_error("The example could not be found",
231  example, make_error_code(errc::no_such_file_or_directory)));
232  pt::read_info(example, pt_conf);
233 
234  pt::ptree reduced_config;
235  for (auto const& key: configpaths) {
236  auto value = pt_conf.get<string>(key);
237  reduced_config.add<string>(key, value);
238  }
239 
240  stringstream ss;
241  write_info(ss, reduced_config);
242  string str = ss.str();
243  al::erase_all(str, "\"\"");
244  cout << str << flush;
245  }
246 
247  auto space_separate = [](string a, string b) { return move(a) + ' ' + move(b); };
248 
249  if (vm.count("input_ext"))
250  for (const vector<string>& ext: inputExt)
251  if (ext.size() > 0)
252  cout << accumulate(next(begin(ext)), end(ext),
253  ext.front(), space_separate) << endl;
254 
255  if (vm.count("output_ext"))
256  for (const vector<string>& ext: outputExt)
257  if (ext.size() > 0)
258  cout << accumulate(next(begin(ext)), end(ext),
259  ext.front(), space_separate) << endl;
260 
261  if (!vm.empty() || argc == 1)
262  exit(EXIT_SUCCESS);
263 }

◆ put()

std::function<void(T)> put ( const char *  configpath)
inlineprivate

Helper to call boost::property_tree::put in Darwin::Toos::Options::args.

Returns
true if Options::registered has been initialised.
Parameters
configpathpath in config file
129  {
130  return [configpath,this](T value) { pt_conf.put<T>(configpath, value); };
131  }

◆ set()

Options & set ( const char *  name,
const boost::program_options::value_semantic *  s,
const char *  desc 
)
private

Generic code to add options. It is called internally by Options::input(), Options::output(), and Options::args().

Returns
the object itself, so that the arguments can be given in a row.
Parameters
nameoptions name
shttps://www.boost.org/doc/libs/1_80_0/doc/html/boost/program_options/value_semantic.html
descdescription (for helper)
418 {
419  if (allow_unregistered())
420  BOOST_THROW_EXCEPTION(invalid_argument(
421  "Once `Options::args()` has been called, "
422  "it is no longer possible to add any further options."));
423  synopsis += ' '; synopsis += name;
424  pos_hide.add(name, 1);
425  custom.add_options()(name, s, desc);
426  return *this;
427 }

◆ slice()

std::pair<unsigned, unsigned> slice ( ) const
inline

Compactify slice information into a pair.

288  {
289  using namespace std;
290  if (!(params & split))
291  BOOST_THROW_EXCEPTION(invalid_argument("No splitting of the input file."));
292  return make_pair(j,k);
293  }

◆ steering()

int steering ( ) const
inline

Steering information for running of executable.

283 { return steer; }

Member Data Documentation

◆ common

boost::program_options::options_description common
private

generic + explicit options like --verbose

◆ config_file

std::filesystem::path config_file
private

path to INFO, JSON, or XML config file

◆ configpaths

std::vector<std::string> configpaths
private

path to the options in config (except for I/O)

◆ custom

boost::program_options::options_description custom
private

for positional arguments, depending on the actual command

◆ example

const std::filesystem::path example
private

path to example config

◆ full_cmd

string full_cmd
static

extended version of the command for reproducibility

◆ helper

boost::program_options::options_description helper
private

to display the helper

◆ hidden

boost::program_options::options_description hidden
private

hidden interface (not for lambda user)

◆ inputExt

std::vector<std::vector<std::string> > inputExt
private

expected extensions of input files

◆ j

unsigned j
private

slices

◆ k

unsigned k
private

slice index

◆ m_commit

const std::string m_commit
private

Commit SHA for –git.

◆ names

std::vector<std::string> names
private

names of the different options (shown in synopsis)

◆ outputExt

std::vector<std::vector<std::string> > outputExt
private

expected extensions of output files

◆ params

const int params
private

input parameters to interpret explicit options

◆ pos_hide

boost::program_options::positional_options_description pos_hide
private

parser for positional arguments

◆ prefix

fs::path prefix
staticprivate

prefix command to steer -j and -k

◆ pt_conf

boost::property_tree::ptree pt_conf
private

internal config obtained from arugments and input config

◆ registered

std::optional<unsigned> registered
private

collect the number of registered options (only if Options::args has been called before)

◆ stage

Stage stage
private

internal stage

◆ steer

int steer
private

output parameters for code executation

◆ synopsis

std::string synopsis
private

stores a clean version of the command, displayed w. -h

◆ tutorial

const std::string tutorial
private

define in constructor, shown with option -t


The documentation for this class was generated from the following files:
Darwin::Tools::Options::parse_env_var
static std::string parse_env_var(std::string)
Definition: Options.cc:75
def
static const char * def
Definition: colours.h:11
Darwin::Tools::Options::check_input
static void check_input(const std::filesystem::path &)
Definition: Options.cc:37
Darwin::Tools::Options::tutorial
const std::string tutorial
define in constructor, shown with option -t
Definition: Options.h:91
Darwin::Tools::Options::custom
boost::program_options::options_description custom
for positional arguments, depending on the actual command
Definition: Options.h:63
Darwin::Tools::Options::Arg
@ Arg
then the registered arguments (inputs and outputs no longer allowed)
Definition: Options.h:147
Darwin::Tools::Options::configpaths
std::vector< std::string > configpaths
path to the options in config (except for I/O)
Definition: Options.h:95
Darwin::Tools::Options::allow_unregistered
bool allow_unregistered() const
Definition: Options.h:137
Darwin::Tools::Options::inputs
Options & inputs(const char *, std::vector< std::filesystem::path > *, const char *, const std::vector< std::string > &={".root", ".xml"})
expected file extension
Definition: Options.cc:456
Darwin::Tools::Options::Output
@ Output
then the outputs (inputs are no longer allowed)
Definition: Options.h:146
Darwin::Tools::Options::args
Options & args(const char *name, const char *configpath, const char *desc)
Definition: Options.cc:517
Darwin::Tools::Options::check_output
static void check_output(const std::filesystem::path &)
Definition: Options.cc:50
Darwin::Tools::Options::hidden
boost::program_options::options_description hidden
hidden interface (not for lambda user)
Definition: Options.h:60
Darwin::Tools::Options::params
const int params
input parameters to interpret explicit options
Definition: Options.h:153
red
static const char * red
Definition: colours.h:5
Darwin::Tools::Options::synopsis
std::string synopsis
stores a clean version of the command, displayed w. -h
Definition: Options.h:92
Darwin::Tools::Options::pos_hide
boost::program_options::positional_options_description pos_hide
parser for positional arguments
Definition: Options.h:65
Darwin::Tools::Options::inputExt
std::vector< std::vector< std::string > > inputExt
expected extensions of input files
Definition: Options.h:161
Darwin::Tools::Options::steer
int steer
output parameters for code executation
Definition: Options.h:155
Darwin::Tools::Options::commit
auto commit() const
Git commit information.
Definition: Options.h:279
Darwin::Tools::Options::parse_common
void parse_common(int, const char *const [])
Definition: Options.cc:265
prefix
Definition: prefix.py:1
Darwin::Tools::Options::outputExt
std::vector< std::vector< std::string > > outputExt
expected extensions of output files
Definition: Options.h:162
Darwin::Tools::Options::parse_helper
void parse_helper(int, const char *const [])
Definition: Options.cc:182
Darwin::Tools::Options::parse_config
static void parse_config(boost::property_tree::ptree &, std::string="")
Parse config if given. This method calls itself to parse the tree structure.
Definition: Options.cc:372
Darwin::Tools::syst
@ syst
activate -s to systematic uncertainties
Definition: Options.h:29
Darwin::Tools::none
@ none
default (for simple executables)
Definition: Options.h:24
Darwin::Tools::Options::k
unsigned k
slice index
Definition: Options.h:158
orange
static const char * orange
Definition: colours.h:6
Darwin::Tools::Options::set
Options & set(const char *, const boost::program_options::value_semantic *, const char *)
Definition: Options.cc:416
Darwin::Tools::Options
Common class to interpret the command line, based on Boost Program Options.
Definition: Options.h:56
wrap_paragraph
string wrap_paragraph(const string &text)
Definition: Options.cc:148
Darwin::Tools::verbose
@ verbose
bit for debug mode (-v is always available)
Definition: Options.h:30
Darwin::Tools::split
@ split
activate -k and -j to define slice
Definition: Options.h:26
Darwin::Tools::Options::arg
Options & arg(const char *name, const char *configpath, const char *desc)
Definition: Options.h:220
Darwin::Tools::Options::stage
Stage stage
internal stage
Definition: Options.h:151
Darwin::Tools::fill
@ fill
activate -f to fill the tree
Definition: Options.h:27
Darwin::Tools
Classes and functions related to the framework.
Definition: forceMetaInfo.cc:28
Darwin::Tools::Options::m_commit
const std::string m_commit
Commit SHA for –git.
Definition: Options.h:154
Darwin::Tools::Options::helper
boost::program_options::options_description helper
to display the helper
Definition: Options.h:61
Darwin::Tools::Options::j
unsigned j
Definition: Options.h:157
Darwin::Tools::Options::parse_custom
void parse_custom(int, const char *const [])
Definition: Options.cc:299
Darwin::Tools::Options::config_file
std::filesystem::path config_file
path to INFO, JSON, or XML config file
Definition: Options.h:88
Darwin::Tools::Options::names
std::vector< std::string > names
names of the different options (shown in synopsis)
Definition: Options.h:94
Darwin::Tools::Options::output
Options & output(const char *, std::filesystem::path *, const char *, const std::vector< std::string > &={".root", ".xml"})
expected file extension
Definition: Options.cc:500
Darwin::Tools::config
@ config
activate -c option to provide config file
Definition: Options.h:25
Darwin::Tools::Options::input
Options & input(const char *, std::filesystem::path *, const char *, const std::vector< std::string > &={".root", ".xml"})
expected file extension
Definition: Options.cc:429
set_mute
void set_mute(bool flag)
Function used by Boost::PO to disable standard error.
Definition: Options.cc:93
Darwin::Tools::Options::exec
std::string exec(const std::string &)
Definition: Options.cc:442
Darwin::Tools::Options::Input
@ Input
first the inputs
Definition: Options.h:145
Darwin::Tools::Options::Args
@ Args
finally the remaining arguments / garbage collector (must be very last)
Definition: Options.h:148
bold
static const char * bold
Definition: colours.h:7
Darwin::Tools::Friend
@ Friend
activate -F to only fill the new branches
Definition: Options.h:28
Darwin::Tools::Options::full_cmd
static std::string full_cmd
extended version of the command for reproducibility
Definition: Options.h:275
Darwin::Tools::Options::registered
std::optional< unsigned > registered
collect the number of registered options (only if Options::args has been called before)
Definition: Options.h:133
Darwin::Tools::Options::pt_conf
boost::property_tree::ptree pt_conf
internal config obtained from arugments and input config
Definition: Options.h:67
Darwin::Tools::Options::common
boost::program_options::options_description common
generic + explicit options like --verbose
Definition: Options.h:62
Darwin::Tools::Options::example
const std::filesystem::path example
path to example config
Definition: Options.h:69