Commit 304edf3a authored by Kučera Petr RNDr. Ph.D.'s avatar Kučera Petr RNDr. Ph.D.
Browse files

PrgOptions, with config file reading

parent da22b804
......@@ -52,8 +52,8 @@ parse_arguments(int argc, char *argv[])
try
{
std::string process;
auto visible = SUBool::prg_common_opts("Allowed options", conf.verbosity);
visible.add_options()("process,p",
/*auto visible = SUBool::prg_common_opts("Allowed options",
conf.verbosity); visible.add_options()("process,p",
po::value<std::string>(&process)->default_value(
process_names.at(static_cast<unsigned>(conf.process))),
kProcessOptDesc);
......@@ -71,10 +71,23 @@ parse_arguments(int argc, char *argv[])
if (!SUBool::prg_process_common_opts(
std::cout, visible, "[input [output]]", vm))
{
return {};
}*/
SUBool::PrgOptions opts(true);
opts.InputOutput(conf.input_file, conf.output_file);
po::options_description desc("Configuration options");
desc.add_options()("process,p",
po::value<std::string>(&process)->default_value(
process_names.at(static_cast<unsigned>(conf.process))),
kProcessOptDesc);
opts.Add(desc);
if (!opts.Parse(argc, argv))
{
return {};
}
conf.process = SUBool::enum_of_name<PROCESS>(process, process_names);
conf.verbosity = opts.Verbosity();
}
catch (std::exception &e)
{
......
......@@ -93,3 +93,133 @@ SUBool::prg_input_output_opts(std::string &input_file, std::string &output_file)
return {opts, positional};
}
void
SUBool::PrgOptions::InitCommon()
{
mCommon.add_options()("help,h", "Print help")(
"version,v", "Print version info");
boost::shared_ptr<po::option_description> opt_verbosity(
new po::option_description("verbosity,b",
po::value<unsigned>(&mVerbosity)->default_value(mVerbosity),
"Verbosity level of the log which is written to standard output."
" 0 means no log is written, however, messages of glucose can still "
"appear."));
mCommon.add(opt_verbosity);
if (mUseConfigFile)
{
mConfigFile = mPrgName + kCfgSuffix;
mCommon.add_options()("config,c",
po::value<std::string>(&mConfigFile)->default_value(mConfigFile),
"Config file")("no-config", "Do not attempt to read a config file.");
mConfigOnly.add(std::move(opt_verbosity));
}
}
void
SUBool::PrgOptions::InputOutput(
std::string &input_file, std::string &output_file)
{
mHidden.add_options()("input", po::value<std::string>(&input_file))(
"output", po::value<std::string>(&output_file));
mPositional.add("input", 1);
mPositional.add("output", 1);
}
bool
SUBool::PrgOptions::ProcessCommon() const
{
if (mVM.count("help"))
{
Help();
return false;
}
if (mVM.count("version"))
{
Version();
return false;
}
return true;
}
void
SUBool::PrgOptions::Help() const
{
Info();
if (!mPrgHelpDesc.empty())
{
std::cout << '\n' << mPrgHelpDesc << '\n';
}
std::cout << "\nUsage:\n\t" << mPrgName << " [OPTION...] " << mPositionalHelp
<< "\n\n";
std::cout << mVisible << std::endl;
}
void
SUBool::PrgOptions::Version() const
{
print_version(std::cout, mPrgName, mPrgDesc);
}
bool
SUBool::PrgOptions::Parse(int argc, char *argv[])
{
/*auto visible = SUBool::prg_common_opts("Allowed options",
conf.verbosity); visible.add_options()("process,p",
po::value<std::string>(&process)->default_value(
process_names.at(static_cast<unsigned>(conf.process))),
kProcessOptDesc);
auto [hidden, positional] =
SUBool::prg_input_output_opts(conf.input_file, conf.output_file);
po::options_description desc;
desc.add(visible).add(hidden);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv)
.options(desc)
.positional(positional)
.run(),
vm);
po::notify(vm);
if (!SUBool::prg_process_common_opts(
std::cout, visible, "[input [output]]", vm))
{
return {};
}*/
po::options_description cmdline_opts;
mVisible.add(mCommon).add(mConfig);
cmdline_opts.add(mCommon).add(mConfig).add(mHidden);
po::store(po::command_line_parser(argc, argv)
.options(cmdline_opts)
.positional(mPositional)
.run(),
mVM);
po::notify(mVM);
if (!ProcessCommon())
{
return false;
}
if (!mUseConfigFile || mVM.count("no-config") || mConfigFile.empty())
{
return true;
}
std::cout << "Reading config file: " << mConfigFile << std::endl;
std::ifstream ifs(mConfigFile);
if (!ifs)
{
if (mVM["config"].defaulted())
{
std::cout << "Failed to open the default config file (" << mConfigFile
<< ")" << std::endl;
return true;
}
std::cerr << "Failed to open config file: " << mConfigFile << std::endl;
return false;
}
po::options_description config_file_options;
config_file_options.add(mConfigOnly).add(mConfig).add(mHidden);
po::store(po::parse_config_file(ifs, config_file_options), mVM);
po::notify(mVM);
return true;
// TODO: parse config file
}
......@@ -197,4 +197,106 @@ namespace SUBool
const po::options_description &visible,
const std::string &positional_help, const po::variables_map &vm);
class PrgOptions
{
std::string mPrgName{};
std::string mPrgDesc{};
std::string mPrgHelpDesc{};
std::string mPositionalHelp{};
bool mUseConfigFile{false};
unsigned mVerbosity{3};
std::string mConfigFile{};
po::options_description mCommon{};
po::options_description mConfig{};
po::options_description mConfigOnly{};
po::options_description mHidden{};
po::options_description mVisible{};
po::positional_options_description mPositional{};
po::variables_map mVM{};
void Help() const;
void Version() const;
void Info() const;
bool ProcessCommon() const;
void InitCommon();
public:
inline static const std::string kCommonOptsLabel = "Common options";
inline static const std::string kInputOutputHelp = "[input [output]]";
inline static const std::string kCfgSuffix = ".cfg";
static const unsigned kDefaultVerbosity = 3;
PrgOptions(bool use_config_file = false,
const std::string &common_opts_label = kCommonOptsLabel,
unsigned default_verbosity = kDefaultVerbosity)
: mPrgName(kPrgName), mPrgDesc(kPrgDesc),
mUseConfigFile(use_config_file), mVerbosity(default_verbosity),
mCommon(common_opts_label)
{
InitCommon();
}
// Empty prg_help_desc means using mPrgDesc
void SetPrgName(std::string prg_name);
void SetPrgDesc(std::string prg_desc);
void SetHelpDesc(std::string prg_help_desc);
void InputOutput(std::string &input_file, std::string &output_file);
bool Parse(int argc, char *argv[]);
unsigned Verbosity() const;
const po::variables_map &VarMap() const;
po::options_description_easy_init AddOptions();
void Add(const po::options_description &desc);
};
} // namespace SUBool
inline unsigned
SUBool::PrgOptions::Verbosity() const
{
return mVerbosity;
}
inline void
SUBool::PrgOptions::SetHelpDesc(std::string prg_help_desc)
{
mPrgHelpDesc = std::move(prg_help_desc);
}
inline void
SUBool::PrgOptions::SetPrgName(std::string prg_name)
{
mPrgName = std::move(prg_name);
}
inline void
SUBool::PrgOptions::SetPrgDesc(std::string prg_desc)
{
mPrgDesc = std::move(prg_desc);
}
inline po::options_description_easy_init
SUBool::PrgOptions::AddOptions()
{
return mConfig.add_options();
}
inline void
SUBool::PrgOptions::Add(const po::options_description &desc)
{
mConfig.add(desc);
}
inline const po::variables_map &
SUBool::PrgOptions::VarMap() const
{
return mVM;
}
inline void
SUBool::PrgOptions::Info() const
{
std::cout << mPrgName << " - " << mPrgDesc << std::endl;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment