Skip to content
Snippets Groups Projects
Commit 2bc82dca authored by Marian Stahl's avatar Marian Stahl
Browse files

Merge branch 'extraargs' into 'master'

Extraargs

This adds the ability to store extra arguments in the command line option parser.
For example if you have something like this:

`myapp -i blub -o bla   file1.txt file2.txt file3.txt ......`

file1.txt file2.txt etc will appear in the property tree in a list called `extraargs`
`extraargs` is a node which contains elements that have an empty key and the values are the strings (in the example `file1.txt` etc)
So you have to loop  over the content of `extraargs` to retrieve the options.

This can be used to pass a list of files to an application. A well known example for this pattern is `hadd`

See merge request !2
parents 422206fb 4535d7a3
Branches
No related tags found
1 merge request!2Extraargs
......@@ -31,6 +31,17 @@
namespace pt = boost::property_tree;
/**
* @fn print_ptree(const pt::ptree& configtree)
* @param configtree: reference to ptree that is printed
*/
inline void print_ptree(const pt::ptree& configtree){
for (const auto& it : configtree) {
std::cout << it.first << ": " << it.second.get_value<std::string>() << std::endl;
print_ptree(it.second);
}
}
/**
* @fn parse_options(int argc, char** argv, STRING&& available_options)
* @param argc, argv: Options to executable
......@@ -68,6 +79,7 @@ inline const pt::ptree parse_options(int argc, char** argv, STRING&& available_o
};
extern char* optarg;
extern int optind;
int ca;
while ((ca = getopt(argc, argv, options)) != -1){
switch (ca) {
......@@ -112,6 +124,21 @@ inline const pt::ptree parse_options(int argc, char** argv, STRING&& available_o
}
}
// put list of arguments which can't be parsed into the property tree
//if (optind >= argc) {
// if we reqire additional paremters give an error message
//}
std::vector<std::string> extraarguments;
while (optind < argc) {
// use a little trick to create a list of values
pt::ptree ptr1;
ptr1.put("",std::string(argv[optind++]));
parsed_options.add_child("extraargs",ptr1);
}
//now check mandatory options
if((opts.Contains("c") && !parsed_options.get_optional<std::string>("config"))
|| (opts.Contains("i") && !parsed_options.get_optional<std::string>("infilename"))
......@@ -139,6 +166,8 @@ inline const pt::ptree parse_options(int argc, char** argv, STRING&& available_o
gSystem->mkdir(parsed_options.get<std::string>("workdir").data());
}
print_ptree(parsed_options);
return parsed_options;//the ptree is moved implicitly!
}
......@@ -188,16 +217,7 @@ inline OBJTYPE* get_obj(TFile* infile, STR&& treename){
return obj;
}
/**
* @fn print_ptree(const pt::ptree& configtree)
* @param configtree: reference to ptree that is printed
*/
inline void print_ptree(const pt::ptree& configtree){
for (const auto& it : configtree) {
std::cout << it.first << ": " << it.second.get_value<std::string>() << std::endl;
print_ptree(it.second);
}
}
/**
* @fn get_ptree(STR&& config, bool print = false)
* @param config: location of config file
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment