ready to test
This commit is contained in:
parent
b89dc24eb1
commit
6f51b4f218
@ -59,6 +59,37 @@ OptionAdder::operator()
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Options::parse_option
|
||||||
|
(
|
||||||
|
std::shared_ptr<OptionDetails> value,
|
||||||
|
const std::string& name,
|
||||||
|
const std::string& arg
|
||||||
|
)
|
||||||
|
{
|
||||||
|
auto& v = m_parsed[name];
|
||||||
|
value->parse("", v.value);
|
||||||
|
++v.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Options::checked_parse_arg
|
||||||
|
(
|
||||||
|
int argc,
|
||||||
|
char* argv[],
|
||||||
|
int argPos,
|
||||||
|
std::shared_ptr<OptionDetails> value,
|
||||||
|
const std::string& name
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (argPos >= argc)
|
||||||
|
{
|
||||||
|
throw missing_argument_exception(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_option(value, name, argv[argPos]);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Options::parse(int& argc, char**& argv)
|
Options::parse(int& argc, char**& argv)
|
||||||
{
|
{
|
||||||
@ -75,6 +106,9 @@ Options::parse(int& argc, char**& argv)
|
|||||||
{
|
{
|
||||||
//handle empty
|
//handle empty
|
||||||
|
|
||||||
|
//for now, throw an exception
|
||||||
|
throw option_not_exists_exception(argv[current]);
|
||||||
|
|
||||||
//if we return from here then it was parsed successfully, so continue
|
//if we return from here then it was parsed successfully, so continue
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -92,7 +126,6 @@ Options::parse(int& argc, char**& argv)
|
|||||||
if (iter == m_short.end())
|
if (iter == m_short.end())
|
||||||
{
|
{
|
||||||
throw option_not_exists_exception(name);
|
throw option_not_exists_exception(name);
|
||||||
//argument not found
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto value = iter->second;
|
auto value = iter->second;
|
||||||
@ -100,15 +133,14 @@ Options::parse(int& argc, char**& argv)
|
|||||||
//if no argument then just add it
|
//if no argument then just add it
|
||||||
if (!value->has_arg())
|
if (!value->has_arg())
|
||||||
{
|
{
|
||||||
auto& v = m_parsed[name];
|
parse_option(value, name);
|
||||||
value->parse("", v.value);
|
|
||||||
++v.count;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//it must be the last argument
|
//it must be the last argument
|
||||||
if (i + 1 == s.size())
|
if (i + 1 == s.size())
|
||||||
{
|
{
|
||||||
|
checked_parse_arg(argc, argv, current+1, value, name);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -135,16 +167,26 @@ Options::parse(int& argc, char**& argv)
|
|||||||
if (result[3].length() != 0)
|
if (result[3].length() != 0)
|
||||||
{
|
{
|
||||||
//parse the option given
|
//parse the option given
|
||||||
|
|
||||||
|
//but if it doesn't take an argument, this is an error
|
||||||
|
if (!opt->has_arg())
|
||||||
|
{
|
||||||
|
throw option_not_has_argument_exception(name, result[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_option(opt, name, result[3]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (opt->has_arg())
|
if (opt->has_arg())
|
||||||
{
|
{
|
||||||
//parse the next argument
|
//parse the next argument
|
||||||
|
checked_parse_arg(argc, argv, current + 1, opt, name);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//parse with empty argument
|
//parse with empty argument
|
||||||
|
parse_option(opt, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,6 +26,7 @@ namespace cxxopts
|
|||||||
void
|
void
|
||||||
parse(const std::string& text, any& result) const
|
parse(const std::string& text, any& result) const
|
||||||
{
|
{
|
||||||
|
result = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -107,6 +108,15 @@ namespace cxxopts
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class missing_argument_exception : public OptionParseException
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
missing_argument_exception(const std::string& option)
|
||||||
|
: OptionParseException(u8"Option ‘" + option + u8"’ is missing an argument")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class option_requires_argument_exception : public OptionParseException
|
class option_requires_argument_exception : public OptionParseException
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -116,6 +126,21 @@ namespace cxxopts
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class option_not_has_argument_exception : public OptionParseException
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
option_not_has_argument_exception
|
||||||
|
(
|
||||||
|
const std::string& option,
|
||||||
|
const std::string& arg
|
||||||
|
)
|
||||||
|
: OptionParseException(
|
||||||
|
u8"Option ‘" + option + u8"’ does not take an argument, but argument‘"
|
||||||
|
+ arg + "’ given")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class option_not_present_exception : public OptionParseException
|
class option_not_present_exception : public OptionParseException
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -213,6 +238,25 @@ namespace cxxopts
|
|||||||
private:
|
private:
|
||||||
friend class OptionAdder;
|
friend class OptionAdder;
|
||||||
|
|
||||||
|
void
|
||||||
|
parse_option
|
||||||
|
(
|
||||||
|
std::shared_ptr<OptionDetails> value,
|
||||||
|
const std::string& name,
|
||||||
|
const std::string& arg = ""
|
||||||
|
);
|
||||||
|
|
||||||
|
void
|
||||||
|
checked_parse_arg
|
||||||
|
(
|
||||||
|
int argc,
|
||||||
|
char* argv[],
|
||||||
|
int argPos,
|
||||||
|
std::shared_ptr<OptionDetails> value,
|
||||||
|
const std::string& name
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
std::map<std::string, std::shared_ptr<OptionDetails>> m_short;
|
std::map<std::string, std::shared_ptr<OptionDetails>> m_short;
|
||||||
std::map<std::string, std::shared_ptr<OptionDetails>> m_long;
|
std::map<std::string, std::shared_ptr<OptionDetails>> m_long;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user