mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 09:31:45 +00:00
FairMQProgOptions fixes and cleanup
- Remove singular key names from JSON schema. - Align the property tree created by `FairMQSuboptParser` with the format required by the main parser (plural names). - Fix `--print-options` to print all options (not only those that have their value set). - remove XML parser (outdated and unused). - various code cleanup.
This commit is contained in:
committed by
Mohammad Al-Turany
parent
4e2a195289
commit
f8d4fe01d0
@@ -19,15 +19,15 @@
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
using namespace fair::mq;
|
||||
|
||||
FairProgOptions::FairProgOptions()
|
||||
: fVarMap()
|
||||
, fGeneralDesc("General options")
|
||||
, fCmdLineOptions("Command line options")
|
||||
, fVisibleOptions("Visible options")
|
||||
, fGeneralOptions("General options")
|
||||
, fAllOptions("Command line options")
|
||||
, fConfigMutex()
|
||||
{
|
||||
fGeneralDesc.add_options()
|
||||
fGeneralOptions.add_options()
|
||||
("help,h", "produce help")
|
||||
("version,v", "print version")
|
||||
("severity", po::value<string>()->default_value("debug"), "Log severity level: trace, debug, info, state, warn, error, fatal, nolog")
|
||||
@@ -35,6 +35,8 @@ FairProgOptions::FairProgOptions()
|
||||
("color", po::value<bool>()->default_value(true), "Log color (true/false)")
|
||||
("log-to-file", po::value<string>()->default_value(""), "Log output to a file.")
|
||||
("print-options", po::value<bool>()->implicit_value(true), "print options in machine-readable format (<option>:<computed-value>:<type>:<description>)");
|
||||
|
||||
fAllOptions.add(fGeneralOptions);
|
||||
}
|
||||
|
||||
FairProgOptions::~FairProgOptions()
|
||||
@@ -42,54 +44,45 @@ FairProgOptions::~FairProgOptions()
|
||||
}
|
||||
|
||||
/// Add option descriptions
|
||||
int FairProgOptions::AddToCmdLineOptions(const po::options_description optDesc, bool visible)
|
||||
int FairProgOptions::AddToCmdLineOptions(const po::options_description optDesc, bool /* visible */)
|
||||
{
|
||||
fCmdLineOptions.add(optDesc);
|
||||
if (visible)
|
||||
{
|
||||
fVisibleOptions.add(optDesc);
|
||||
}
|
||||
fAllOptions.add(optDesc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
po::options_description& FairProgOptions::GetCmdLineOptions()
|
||||
{
|
||||
return fCmdLineOptions;
|
||||
return fAllOptions;
|
||||
}
|
||||
|
||||
int FairProgOptions::ParseCmdLine(const int argc, char const* const* argv, const po::options_description& desc, po::variables_map& varmap, bool allowUnregistered)
|
||||
int FairProgOptions::ParseCmdLine(const int argc, char const* const* argv, bool allowUnregistered)
|
||||
{
|
||||
// get options from cmd line and store in variable map
|
||||
// here we use command_line_parser instead of parse_command_line, to allow unregistered and positional options
|
||||
if (allowUnregistered)
|
||||
{
|
||||
po::command_line_parser parser{argc, argv};
|
||||
parser.options(desc).allow_unregistered();
|
||||
parser.options(fAllOptions).allow_unregistered();
|
||||
po::parsed_options parsedOptions = parser.run();
|
||||
po::store(parsedOptions, varmap);
|
||||
po::store(parsedOptions, fVarMap);
|
||||
}
|
||||
else
|
||||
{
|
||||
po::store(po::parse_command_line(argc, argv, desc), varmap);
|
||||
po::store(po::parse_command_line(argc, argv, fAllOptions), fVarMap);
|
||||
}
|
||||
|
||||
// Handles options like "--help" or "--version"
|
||||
// return 1 if switch options found in varmap
|
||||
// return 1 if switch options found in fVarMap
|
||||
if (ImmediateOptions())
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
po::notify(varmap);
|
||||
po::notify(fVarMap);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FairProgOptions::ParseCmdLine(const int argc, char const* const* argv, const po::options_description& desc, bool allowUnregistered)
|
||||
{
|
||||
return ParseCmdLine(argc, argv, desc, fVarMap, allowUnregistered);
|
||||
}
|
||||
|
||||
void FairProgOptions::ParseDefaults(const po::options_description& desc)
|
||||
void FairProgOptions::ParseDefaults()
|
||||
{
|
||||
vector<string> emptyArgs;
|
||||
emptyArgs.push_back("dummy");
|
||||
@@ -101,148 +94,85 @@ void FairProgOptions::ParseDefaults(const po::options_description& desc)
|
||||
return str.c_str();
|
||||
});
|
||||
|
||||
po::store(po::parse_command_line(argv.size(), const_cast<char**>(argv.data()), desc), fVarMap);
|
||||
po::store(po::parse_command_line(argv.size(), const_cast<char**>(argv.data()), fAllOptions), fVarMap);
|
||||
}
|
||||
|
||||
int FairProgOptions::PrintOptions()
|
||||
{
|
||||
// Method to overload.
|
||||
// -> loop over variable map and print its content
|
||||
// -> In this example the following types are supported:
|
||||
// string, int, float, double, short, boost::filesystem::path
|
||||
// vector<string>, vector<int>, vector<float>, vector<double>, vector<short>
|
||||
|
||||
MapVarValInfo_t mapinfo;
|
||||
map<string, VarValInfo> mapinfo;
|
||||
|
||||
// get string length for formatting and convert varmap values into string
|
||||
int maxLength1st = 0;
|
||||
int maxLength2nd = 0;
|
||||
int maxLengthTypeInfo = 0;
|
||||
int maxLengthDefault = 0;
|
||||
int maxLengthEmpty = 0;
|
||||
int totalLength = 0;
|
||||
int maxLen1st = 0;
|
||||
int maxLen2nd = 0;
|
||||
int maxLenTypeInfo = 0;
|
||||
int maxLenDefault = 0;
|
||||
int maxLenEmpty = 0;
|
||||
for (const auto& m : fVarMap)
|
||||
{
|
||||
Max(maxLength1st, m.first.length());
|
||||
Max(maxLen1st, m.first.length());
|
||||
|
||||
VarValInfo_t valinfo = GetVariableValueInfo(m.second);
|
||||
VarValInfo valinfo = GetVariableValueInfo(m.second);
|
||||
mapinfo[m.first] = valinfo;
|
||||
string valueStr;
|
||||
string typeInfoStr;
|
||||
string defaultStr;
|
||||
string emptyStr;
|
||||
tie(valueStr, typeInfoStr, defaultStr, emptyStr) = valinfo;
|
||||
|
||||
Max(maxLength2nd, valueStr.length());
|
||||
Max(maxLengthTypeInfo, typeInfoStr.length());
|
||||
Max(maxLengthDefault, defaultStr.length());
|
||||
Max(maxLengthEmpty, emptyStr.length());
|
||||
Max(maxLen2nd, valinfo.value.length());
|
||||
Max(maxLenTypeInfo, valinfo.type.length());
|
||||
Max(maxLenDefault, valinfo.defaulted.length());
|
||||
Max(maxLenEmpty, valinfo.empty.length());
|
||||
}
|
||||
|
||||
// TODO : limit the value length field in a better way
|
||||
if (maxLength2nd > 100)
|
||||
// TODO : limit the value len field in a better way
|
||||
if (maxLen2nd > 100)
|
||||
{
|
||||
maxLength2nd = 100;
|
||||
maxLen2nd = 100;
|
||||
}
|
||||
totalLength = maxLength1st + maxLength2nd + maxLengthTypeInfo + maxLengthDefault + maxLengthEmpty;
|
||||
|
||||
// maxLength2nd = 200;
|
||||
|
||||
// formatting and printing
|
||||
|
||||
stringstream ss;
|
||||
ss << "\n";
|
||||
|
||||
ss << setfill('*') << setw(totalLength + 3) << "*" << "\n"; // +3 because of string " = "
|
||||
string title = " Configuration ";
|
||||
|
||||
int leftSpaceLength = 0;
|
||||
int rightSpaceLength = 0;
|
||||
int leftTitleShiftLength = 0;
|
||||
int rightTitleShiftLength = 0;
|
||||
|
||||
leftTitleShiftLength = title.length() / 2;
|
||||
|
||||
if ((title.length()) % 2)
|
||||
{
|
||||
rightTitleShiftLength = leftTitleShiftLength + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
rightTitleShiftLength = leftTitleShiftLength;
|
||||
}
|
||||
|
||||
leftSpaceLength = (totalLength + 3) / 2 - leftTitleShiftLength;
|
||||
if ((totalLength + 3) % 2)
|
||||
{
|
||||
rightSpaceLength = (totalLength + 3) / 2 - rightTitleShiftLength + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
rightSpaceLength = (totalLength + 3) / 2 - rightTitleShiftLength;
|
||||
}
|
||||
|
||||
ss << setfill ('*') << setw(leftSpaceLength) << "*"
|
||||
<< setw(title.length()) << title
|
||||
<< setfill ('*') << setw(rightSpaceLength) << "*" << "\n";
|
||||
|
||||
ss << setfill ('*') << setw (totalLength+3) << "*" << "\n";
|
||||
ss << "Configuration: \n";
|
||||
|
||||
for (const auto& p : mapinfo)
|
||||
{
|
||||
string keyStr;
|
||||
string valueStr;
|
||||
string typeInfoStr;
|
||||
string defaultStr;
|
||||
string emptyStr;
|
||||
keyStr = p.first;
|
||||
tie(valueStr, typeInfoStr, defaultStr, emptyStr) = p.second;
|
||||
ss << setfill(' ')
|
||||
<< setw(maxLength1st) << left
|
||||
<< p.first << " = "
|
||||
<< setw(maxLength2nd)
|
||||
<< valueStr
|
||||
<< setw(maxLengthTypeInfo)
|
||||
<< typeInfoStr
|
||||
<< setw(maxLengthDefault)
|
||||
<< defaultStr
|
||||
<< setw(maxLengthEmpty)
|
||||
<< emptyStr
|
||||
<< setw(maxLen1st) << left << p.first << " = "
|
||||
<< setw(maxLen2nd) << p.second.value
|
||||
<< setw(maxLenTypeInfo) << p.second.type
|
||||
<< setw(maxLenDefault) << p.second.defaulted
|
||||
<< setw(maxLenEmpty) << p.second.empty
|
||||
<< "\n";
|
||||
}
|
||||
ss << setfill ('*') << setw(totalLength + 3) << "*";// +3 for " = "
|
||||
|
||||
LOG(debug) << ss.str();
|
||||
LOG(info) << ss.str();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FairProgOptions::PrintOptionsRaw()
|
||||
{
|
||||
MapVarValInfo_t mapInfo;
|
||||
const std::vector<boost::shared_ptr<po::option_description>>& options = fAllOptions.options();
|
||||
|
||||
for (const auto& m : fVarMap)
|
||||
for (const auto& o : options)
|
||||
{
|
||||
mapInfo[m.first] = GetVariableValueInfo(m.second);
|
||||
}
|
||||
VarValInfo value;
|
||||
if (fVarMap.count(o->canonical_display_name()))
|
||||
{
|
||||
value = GetVariableValueInfo(fVarMap[o->canonical_display_name()]);
|
||||
}
|
||||
|
||||
for (const auto& p : mapInfo)
|
||||
{
|
||||
string keyStr;
|
||||
string valueStr;
|
||||
string typeInfoStr;
|
||||
string defaultStr;
|
||||
string emptyStr;
|
||||
keyStr = p.first;
|
||||
tie(valueStr, typeInfoStr, defaultStr, emptyStr) = p.second;
|
||||
auto option = fCmdLineOptions.find_nothrow(keyStr, false);
|
||||
cout << keyStr << ":" << valueStr << ":" << typeInfoStr << ":" << (option ? option->description() : "<not found>") << endl;
|
||||
string description = o->description();
|
||||
|
||||
replace(description.begin(), description.end(), '\n', ' ');
|
||||
|
||||
cout << o->long_name() << ":" << value.value << ":" << (value.type == "" ? "<unknown>" : value.type) << ":" << description << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
FairProgOptions::VarValInfo_t FairProgOptions::GetVariableValueInfo(const po::variable_value& varValue)
|
||||
VarValInfo FairProgOptions::GetVariableValueInfo(const po::variable_value& varValue)
|
||||
{
|
||||
return FairMQ::ConvertVariableValue<FairMQ::ToVarInfo>().Run(varValue);
|
||||
return ConvertVariableValue<ToVarValInfo>()(varValue);
|
||||
}
|
||||
|
Reference in New Issue
Block a user