 |
Darwin
1.0
Event loop based prototype framework
|
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:
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>
|
| Options (const std::string &, int=none, const char *commit=DARWIN_GIT_COMMIT, const char *example=DARWIN_EXAMPLE) |
|
| Options ()=delete |
|
Options & | input (const char *, std::filesystem::path *, const char *, const std::vector< std::string > &={".root", ".xml"}) |
|
Options & | inputs (const char *, std::vector< std::filesystem::path > *, const char *, const std::vector< std::string > &={".root", ".xml"}) |
|
Options & | output (const char *, std::filesystem::path *, const char *, const std::vector< std::string > &={".root", ".xml"}) |
|
template<typename T > |
Options & | arg (const char *name, const char *configpath, const char *desc) |
|
Options & | args (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 |
|
|
void | parse_helper (int, const char *const []) |
|
void | parse_common (int, const char *const []) |
|
void | parse_custom (int, const char *const []) |
|
Options & | set (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 std::filesystem::path | prefix |
|
◆ Stage
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)
|
◆ Options() [1/2]
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
-
tuto | description, to be displayed in the helper |
pars | flags for explicit, generic command options (see enum) |
commit | git commit for --git |
example | path to example |
108 (
"prefix,p", po::value<fs::path>(&
prefix),
"Prefix command, `parallel` or `submit` (for history only)");
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)");
118 (
"example,e",
"Print config example");
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");
128 "Configuration file in INFO, JSON, or XML format");
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");
133 (
"fill,f", po::bool_switch()->default_value(
false),
"Enable `TTree::Fill()`");
135 (
"Friend,F", po::bool_switch()->default_value(
false),
136 "Use `TTree` friends instead of cloning the whole input `TTree`");
138 (
"syst,s", po::bool_switch()->default_value(
false),
139 "Enable systematic variations");
◆ Options() [2/2]
No default constructor is allowed.
◆ allow_unregistered()
bool allow_unregistered |
( |
| ) |
const |
|
inlineprivate |
◆ 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
-
name | name of the option, will be shown in helper |
configpath | path in config |
desc | description, shown in helper too |
225 if (
stage > Stage::Arg)
226 BOOST_THROW_EXCEPTION(std::runtime_error(
227 "Not possible to add another argument at this stage"));
230 names.push_back(name);
232 const boost::program_options::value_semantic * s =
233 boost::program_options::value<T>()->notifier(put<T>(configpath));
234 return set(name, s, desc);
◆ 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
-
name | generic name for unregistered options |
configpath | path in config. |
desc | description, shown in helper too |
519 if (
stage >= Stage::Args)
520 BOOST_THROW_EXCEPTION(runtime_error(
"Only one garbage collector is possible"));
523 synopsis +=
" [" + string(name) +
"...]";
527 const po::value_semantic * s =
528 po::value<vector<string>>()->multitoken()->zero_tokens();
529 custom.add_options()(name, s, desc);
◆ 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
-
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)));
46 BOOST_THROW_EXCEPTION(fs::filesystem_error(
"Input exists but cannot be read",
47 input, make_error_code(errc::permission_denied)));
◆ 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
-
52 fs::path outputHist =
output;
53 if (!
prefix.empty() && !fs::is_directory(
output)) outputHist.remove_filename();
54 full_cmd +=
' ' + outputHist.string();
56 if (!fs::exists(
output))
return;
58 if ((fs::status(
output).permissions() & fs::perms::owner_write)
61 BOOST_THROW_EXCEPTION(fs::filesystem_error(
62 "Output already exists and cannot be overwritten",
63 output, make_error_code(errc::permission_denied)));
66 if (fs::is_directory(
output) && fs::equivalent(
output,
".")) {
68 BOOST_THROW_EXCEPTION(fs::filesystem_error(
"Not a valid output",
69 output, make_error_code(errc::invalid_argument)));
71 else if (
output !=
"/dev/null")
72 cerr <<
orange <<
"Warning: you are overwriting " <<
output <<
def <<
'\n';
◆ commit()
◆ exec()
std::string exec |
( |
const std::string & |
cmd | ) |
|
|
private |
- Returns
- output from command given to shell
- Parameters
-
cmd | cmd to run in the shell |
447 FILE * pipe = popen(cmd.c_str(),
"r");
449 if (fgets(buffer, 128, pipe) != NULL)
◆ 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
-
name | name of the option, will be shown in helper |
file | path pointer to file |
desc | description, shown in helper too |
432 if (
stage > Stage::Input)
433 BOOST_THROW_EXCEPTION(runtime_error(
434 "Not possible to add another input at this stage"));
437 const po::value_semantic * s =
438 po::value<fs::path>(file)->notifier(
check_input)->required();
439 return set(name, s, desc);
◆ 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
-
name | name of the options, will be shown in helper |
files | paths to files |
desc | description, shown in helper too |
460 if (
stage > Stage::Input)
461 BOOST_THROW_EXCEPTION(runtime_error(
462 "Not possible to add another input at this stage"));
464 auto store = [files,
this](
const string& pathRegex) {
466 const string& ls =
exec(
"ls -1d " + pathRegex);
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;
475 if (files->empty()) {
476 fs::path p = pathRegex;
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)));
483 auto inputs = accumulate(files->begin(), files->end(),
string(),
485 return inputs + fs::canonical(input).string() +
' '; });
487 if (files->size() > 1)
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());
◆ 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.
409 catch (
const po::error& e) {
410 BOOST_THROW_EXCEPTION(po::error(e.what()));
◆ 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
-
name | name of the option, will be shown in helper |
file | path pointer to file |
desc | description, shown in helper too |
503 if (
stage > Stage::Output) {
505 BOOST_THROW_EXCEPTION(runtime_error(
506 "Not possible to add another output at this stage"));
508 stage = Stage::Output;
511 const po::value_semantic * s =
512 po::value<fs::path>(file)->notifier(
check_output)->required();
514 return set(name, s, desc);
◆ 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.
267 po::options_description cmd_line;
271 po::command_line_parser parser{argc, argv};
272 parser.options(cmd_line)
273 .allow_unregistered();
275 po::variables_map vm;
276 po::store(parser.run(), vm);
279 if (vm.count(
"config")) {
283 else if (ext ==
".xml" ) {
286 pt_conf = userinfo.get_child(
"userinfo");
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)));
◆ 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)
375 for (
auto& it: tree) {
376 string subkey = it.first;
378 pt::ptree& child = it.second;
379 auto value = tree.get_optional<
string>(subkey);
380 string newkey = key + (key.empty() ?
"" :
".") + subkey;
383 tree.put<
string>(subkey, *value);
◆ 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.
301 po::options_description cmd_line;
306 po::command_line_parser parser{argc, argv};
307 parser.options(cmd_line)
310 po::variables_map vm;
311 po::parsed_options parsed = parser.run();
312 po::store(parsed, vm);
316 for (
auto& name:
names) {
317 if (vm.count(name))
continue;
318 throw po::required_option(name);
330 auto arg =
pt_conf.get_optional<
string>(configpath);
332 al::erase_all(*
arg,
"\n");
333 al::erase_all(*
arg,
" ");
334 al::erase_all(*
arg,
"\"");
335 if (
arg->empty())
continue;
342 if (!
pt_conf.count(configpath))
343 pt_conf.add<
string>(configpath,
"");
344 auto& subtree =
pt_conf.get_child(configpath);
346 for (
auto& item: subtree) {
347 string key = item.first,
348 value = item.second.get_value<
string>();
349 if (key.empty() || key ==
"item")
351 else if (value.empty())
355 vector<string> items = po::collect_unrecognized(parsed.options,
356 po::include_positional);
359 items.erase(items.begin(), items.begin() + *
registered);
361 for (
auto& item: items) {
362 if (subtree.count(item))
continue;
363 subtree.add<
string>(item,
"");
◆ parse_env_var() [1/3]
static const char* parse_env_var |
( |
const char * |
p | ) |
|
|
inlinestatic |
Parse environment variable in C-style string.
◆ parse_env_var() [2/3]
static std::filesystem::path parse_env_var |
( |
const std::filesystem::path & |
p | ) |
|
|
inlinestatic |
Parse environment variable in a path.
◆ 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
77 static const regex env_re{R
"--(\$\{([^}]+)\})--"};
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());
84 BOOST_THROW_EXCEPTION(runtime_error(
85 "Environment variable ${" + name +
"} does not exist."));
86 s.replace(from.first, from.second, value);
◆ 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.
184 po::command_line_parser parser{argc, argv};
186 .allow_unregistered();
189 po::variables_map vm;
190 po::store(parser.run(), vm);
193 if (vm.count(
"help") || argc == 1) {
194 fs::path executable = argv[0];
197 for (
const auto& option:
custom.options())
198 cout <<
'\t' << option->long_name()
199 <<
" = " << option->description() <<
'\n';
202 if (vm.count(
"help")) {
203 po::options_description cmd_line;
207 cout << cmd_line << endl;
210 if (vm.count(
"tutorial"))
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;
227 if (vm.count(
"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)));
234 pt::ptree reduced_config;
236 auto value =
pt_conf.get<
string>(key);
237 reduced_config.add<
string>(key, value);
241 write_info(ss, reduced_config);
242 string str = ss.str();
243 al::erase_all(str,
"\"\"");
244 cout << str << flush;
247 auto space_separate = [](
string a,
string b) {
return move(a) +
' ' + move(b); };
249 if (vm.count(
"input_ext"))
250 for (
const vector<string>& ext:
inputExt)
252 cout << accumulate(next(begin(ext)), end(ext),
253 ext.front(), space_separate) << endl;
255 if (vm.count(
"output_ext"))
256 for (
const vector<string>& ext:
outputExt)
258 cout << accumulate(next(begin(ext)), end(ext),
259 ext.front(), space_separate) << endl;
261 if (!vm.empty() || argc == 1)
◆ 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
-
configpath | path in config file |
130 return [configpath,
this](T value) {
pt_conf.put<T>(configpath, value); };
◆ 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
-
420 BOOST_THROW_EXCEPTION(invalid_argument(
421 "Once `Options::args()` has been called, "
422 "it is no longer possible to add any further options."));
425 custom.add_options()(name, s, desc);
◆ slice()
std::pair<unsigned, unsigned> slice |
( |
| ) |
const |
|
inline |
Compactify slice information into a pair.
291 BOOST_THROW_EXCEPTION(invalid_argument(
"No splitting of the input file."));
292 return make_pair(
j,
k);
◆ steering()
Steering information for running of executable.
◆ 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 |
◆ full_cmd
extended version of the command for reproducibility
◆ helper
boost::program_options::options_description helper |
|
private |
◆ 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
◆ m_commit
const std::string m_commit |
|
private |
◆ 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
input parameters to interpret explicit options
◆ pos_hide
boost::program_options::positional_options_description pos_hide |
|
private |
parser for positional arguments
◆ prefix
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
◆ steer
output parameters for code executation
◆ synopsis
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:
static const char * def
Definition: colours.h:11
static const char * red
Definition: colours.h:5
static const char * orange
Definition: colours.h:6
string wrap_paragraph(const string &text)
Definition: Options.cc:148
void set_mute(bool flag)
Function used by Boost::PO to disable standard error.
Definition: Options.cc:93
static const char * bold
Definition: colours.h:7