parse positional arguments
This commit is contained in:
parent
e85c2aa282
commit
8701a5d7ca
@ -116,6 +116,39 @@ Options::checked_parse_arg
|
||||
parse_option(value, name, argv[argPos]);
|
||||
}
|
||||
|
||||
void
|
||||
Options::add_to_option(const std::string& option, const std::string& arg)
|
||||
{
|
||||
auto iter = m_options.find(option);
|
||||
|
||||
if (iter == m_options.end())
|
||||
{
|
||||
throw option_not_exists_exception(option);
|
||||
}
|
||||
|
||||
parse_option(iter->second, option, arg);
|
||||
}
|
||||
|
||||
bool
|
||||
Options::consume_positional(std::string a)
|
||||
{
|
||||
if (m_positional.size() > 0)
|
||||
{
|
||||
add_to_option(m_positional, a);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Options::parse_positional(std::string option)
|
||||
{
|
||||
m_positional = std::move(option);
|
||||
}
|
||||
|
||||
void
|
||||
Options::parse(int& argc, char**& argv)
|
||||
{
|
||||
@ -130,11 +163,16 @@ Options::parse(int& argc, char**& argv)
|
||||
|
||||
if (result.empty())
|
||||
{
|
||||
//handle empty
|
||||
|
||||
//for now, throw an exception
|
||||
throw option_not_exists_exception(argv[current]);
|
||||
//not a flag
|
||||
|
||||
//if true is returned here then it was consumed, otherwise it is
|
||||
//ignored
|
||||
if (consume_positional(argv[current]))
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
//if we return from here then it was parsed successfully, so continue
|
||||
}
|
||||
else
|
||||
|
||||
@ -312,9 +312,19 @@ namespace cxxopts
|
||||
return *iter->second;
|
||||
}
|
||||
|
||||
//parse positional arguments into the given option
|
||||
void
|
||||
parse_positional(std::string option);
|
||||
|
||||
private:
|
||||
friend class OptionAdder;
|
||||
|
||||
bool
|
||||
consume_positional(std::string a);
|
||||
|
||||
void
|
||||
add_to_option(const std::string& option, const std::string& arg);
|
||||
|
||||
void
|
||||
parse_option
|
||||
(
|
||||
@ -335,6 +345,7 @@ namespace cxxopts
|
||||
|
||||
|
||||
std::map<std::string, std::shared_ptr<OptionDetails>> m_options;
|
||||
std::string m_positional;
|
||||
};
|
||||
|
||||
class OptionAdder
|
||||
|
||||
@ -37,8 +37,11 @@ int main(int argc, char* argv[])
|
||||
("a,apple", "an apple")
|
||||
("b,bob", "Bob")
|
||||
("f,file", "File", cxxopts::value<std::string>())
|
||||
("positional", "Positional arguments", cxxopts::value<std::string>())
|
||||
;
|
||||
|
||||
options.parse_positional("positional");
|
||||
|
||||
options.parse(argc, argv);
|
||||
|
||||
if (options.count("a"))
|
||||
@ -62,6 +65,12 @@ int main(int argc, char* argv[])
|
||||
//std::cout << options.print_help();
|
||||
}
|
||||
|
||||
if (options.count("positional"))
|
||||
{
|
||||
std::cout << "Positional = " << options["positional"].as<std::string>()
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
} catch (const cxxopts::OptionException& e)
|
||||
{
|
||||
std::cout << "error parsing options: " << e.what() << std::endl;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user