mirror of
https://github.com/Cameron-Reed1/Cpp-Argument-Parser.git
synced 2025-01-10 15:56:47 +00:00
Fix some weird choices I made two years ago
This includes a potential two byte memory leak
This commit is contained in:
parent
c5cc2f65d8
commit
2e7bf3b8c7
@ -8,8 +8,7 @@ namespace Arguments
|
||||
{
|
||||
|
||||
enum ERROR {
|
||||
IMPOSSIBLE = -1,
|
||||
NO_ERROR,
|
||||
NO_ERROR = 0,
|
||||
ERROR_UNKNOWN_OPTION,
|
||||
ERROR_INCORRECT_TYPE,
|
||||
ERROR_MISSING_ARGUMENT,
|
||||
@ -70,10 +69,12 @@ private:
|
||||
ERROR handle_short_option(const char option, const char* next_value);
|
||||
ERROR handle_positional_argument(const char* arg);
|
||||
ERROR get_option_data(Option* opt, const char* option_name, const char* data_str);
|
||||
ERROR get_option_data(Option* opt, const char option_name, const char* data_str);
|
||||
ERROR unknown_option(const char* option);
|
||||
ERROR unknown_option(const char option);
|
||||
ERROR incorrect_type(const char* option, const char* got);
|
||||
ERROR missing_argument(const char* option);
|
||||
ERROR missing_argument(const char option);
|
||||
ERROR missing_positional_argument(const char* arg);
|
||||
private:
|
||||
const char* m_program_name;
|
||||
|
@ -129,16 +129,11 @@ ERROR Parser::handle_long_option(const char* option, const char* next_value)
|
||||
|
||||
return unknown_option(option);
|
||||
|
||||
} else {
|
||||
}
|
||||
|
||||
matched_option->m_found = true;
|
||||
return get_option_data(matched_option, matched_option->m_name, next_value);
|
||||
|
||||
}
|
||||
|
||||
std::cout << "Congratulations! You have reached an impossible state" << std::endl << std::endl;
|
||||
std::cout << "Reality is broken :)" << std::endl;
|
||||
return IMPOSSIBLE;
|
||||
matched_option->m_found = true;
|
||||
return get_option_data(matched_option, option, next_value);
|
||||
}
|
||||
|
||||
ERROR Parser::handle_short_option(const char option, const char* next_value)
|
||||
@ -159,20 +154,11 @@ ERROR Parser::handle_short_option(const char option, const char* next_value)
|
||||
|
||||
return unknown_option(option);
|
||||
|
||||
} else {
|
||||
|
||||
matched_option->m_found = true;
|
||||
char* name = new char[2];
|
||||
name[0] = matched_option->m_short_name;
|
||||
name[1] = 0;
|
||||
return get_option_data(matched_option, name, next_value);
|
||||
delete[] name;
|
||||
|
||||
}
|
||||
|
||||
std::cout << "Congratulations! You have reached impossible state #2" << std::endl << std::endl;
|
||||
std::cout << "Reality is broken :)" << std::endl;
|
||||
return IMPOSSIBLE;
|
||||
|
||||
matched_option->m_found = true;
|
||||
return get_option_data(matched_option, option, next_value);
|
||||
}
|
||||
|
||||
ERROR Parser::handle_positional_argument(const char* arg)
|
||||
@ -218,6 +204,27 @@ ERROR Parser::get_option_data(Option* opt, const char* option_name, const char*
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
ERROR Parser::get_option_data(Option* opt, const char option_name, const char* data_str)
|
||||
{
|
||||
if (opt->m_type == FLAG)
|
||||
return NO_ERROR;
|
||||
|
||||
if (data_str == nullptr || data_str[0] == '-')
|
||||
return missing_argument(option_name);
|
||||
|
||||
if (opt->m_type == INT) {
|
||||
int* val = new int;
|
||||
if ((*val = stringToInt(data_str)) == -1)
|
||||
return incorrect_type(opt->m_name, data_str);
|
||||
opt->data = val;
|
||||
} else {
|
||||
opt->data = (void*) data_str;
|
||||
}
|
||||
|
||||
m_opt_index++;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
ERROR Parser::unknown_option(const char* option)
|
||||
{
|
||||
std::cout << m_program_name << ": invalid option '" << option << '\'' << std::endl << std::endl;
|
||||
@ -248,6 +255,13 @@ ERROR Parser::missing_argument(const char* option)
|
||||
return ERROR_MISSING_ARGUMENT;
|
||||
}
|
||||
|
||||
ERROR Parser::missing_argument(const char option)
|
||||
{
|
||||
std::cout << m_program_name << ": Missing argument for option '" << option << '\'' << std::endl;
|
||||
|
||||
return ERROR_MISSING_ARGUMENT;
|
||||
}
|
||||
|
||||
ERROR Parser::missing_positional_argument(const char* arg)
|
||||
{
|
||||
std::cout << m_program_name << ": Missing required positional argument '" << arg << "'" << std::endl << std::endl;
|
||||
|
Loading…
Reference in New Issue
Block a user