Commit 1adc0599 authored by aligungr's avatar aligungr

Options parser improvement

parent 69648564
......@@ -18,6 +18,14 @@
static constexpr const size_t MAX_WIDTH = 90;
static char **ConvertArgs(const std::vector<std::string> &vec)
{
char **res = static_cast<char **>(malloc(sizeof(char *) * vec.size()));
for (size_t i = 0; i < vec.size(); i++)
res[i] = strdup(vec[i].c_str());
return res;
}
static std::string ItemNameDisplay(const opt::OptionItem &item)
{
std::stringstream ss{};
......@@ -35,7 +43,18 @@ static std::string ItemNameDisplay(const opt::OptionItem &item)
return ss.str();
}
opt::OptionsResult::OptionsResult(int argc, char **argv, const opt::OptionsDescription &desc) : m_description{desc}
opt::OptionsResult::OptionsResult(const std::vector<std::string> &args, const opt::OptionsDescription &desc)
: OptionsResult(static_cast<int>(args.size()), ConvertArgs(args), desc, true)
{
}
opt::OptionsResult::OptionsResult(int argc, char **argv, const opt::OptionsDescription &desc)
: OptionsResult(argc, argv, desc, false)
{
}
opt::OptionsResult::OptionsResult(int argc, char **argv, const opt::OptionsDescription &desc, bool freeArgv)
: m_description{desc}
{
argc--;
argv++;
......@@ -139,6 +158,13 @@ opt::OptionsResult::OptionsResult(int argc, char **argv, const opt::OptionsDescr
}
}
}
if (freeArgv)
{
for (int i = 0; i < argc; i++)
free(argv[i]);
free(argv);
}
}
bool opt::OptionsResult::hasFlag(const opt::OptionItem &item) const
......
......@@ -87,8 +87,12 @@ class OptionsResult
std::unordered_map<std::string, std::string> m_options{};
OptionsDescription m_description;
private:
OptionsResult(int argc, char **argv, const OptionsDescription &desc, bool freeArgv);
public:
OptionsResult(int argc, char **argv, const OptionsDescription &desc);
OptionsResult(const std::vector<std::string> &args, const OptionsDescription &desc);
public:
bool hasFlag(const OptionItem &item) const;
......
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