ready to test

This commit is contained in:
Jarryd Beck 2014-10-01 17:36:43 +10:00
parent b89dc24eb1
commit 6f51b4f218
2 changed files with 90 additions and 4 deletions

View File

@ -59,6 +59,37 @@ OptionAdder::operator()
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
Options::parse(int& argc, char**& argv)
{
@ -75,6 +106,9 @@ Options::parse(int& argc, char**& argv)
{
//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
}
else
@ -92,7 +126,6 @@ Options::parse(int& argc, char**& argv)
if (iter == m_short.end())
{
throw option_not_exists_exception(name);
//argument not found
}
auto value = iter->second;
@ -100,15 +133,14 @@ Options::parse(int& argc, char**& argv)
//if no argument then just add it
if (!value->has_arg())
{
auto& v = m_parsed[name];
value->parse("", v.value);
++v.count;
parse_option(value, name);
}
else
{
//it must be the last argument
if (i + 1 == s.size())
{
checked_parse_arg(argc, argv, current+1, value, name);
}
else
{
@ -135,16 +167,26 @@ Options::parse(int& argc, char**& argv)
if (result[3].length() != 0)
{
//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
{
if (opt->has_arg())
{
//parse the next argument
checked_parse_arg(argc, argv, current + 1, opt, name);
}
else
{
//parse with empty argument
parse_option(opt, name);
}
}
}

View File

@ -26,6 +26,7 @@ namespace cxxopts
void
parse(const std::string& text, any& result) const
{
result = true;
}
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
{
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
{
public:
@ -213,6 +238,25 @@ namespace cxxopts
private:
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_long;