refactor(argument_parser): improve argument handling and cleanup code
- Add new template method for argument addition with type - Handle optional values more safely in get_optional - Remove commented test code from main.cpp
This commit is contained in:
@@ -150,6 +150,11 @@ namespace argument_parser {
|
|||||||
base_add_argument(short_arg, long_arg, help_text, action, required);
|
base_add_argument(short_arg, long_arg, help_text, action, required);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void add_argument(std::string const& short_arg, std::string const& long_arg, std::string const& help_text, bool required) {
|
||||||
|
base_add_argument<T>(short_arg, long_arg, help_text, required);
|
||||||
|
}
|
||||||
|
|
||||||
void add_argument(std::string const& short_arg, std::string const& long_arg, std::string const& help_text, non_parametered_action const& action, bool required) {
|
void add_argument(std::string const& short_arg, std::string const& long_arg, std::string const& help_text, non_parametered_action const& action, bool required) {
|
||||||
base_add_argument(short_arg, long_arg, help_text, action, required);
|
base_add_argument(short_arg, long_arg, help_text, action, required);
|
||||||
}
|
}
|
||||||
@@ -157,16 +162,14 @@ namespace argument_parser {
|
|||||||
void add_argument(std::string const& short_arg, std::string const& long_arg, std::string const& help_text, bool required) {
|
void add_argument(std::string const& short_arg, std::string const& long_arg, std::string const& help_text, bool required) {
|
||||||
base_add_argument<void>(short_arg, long_arg, help_text, required);
|
base_add_argument<void>(short_arg, long_arg, help_text, required);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void add_argument(std::string const& short_arg, std::string const& long_arg, std::string const& help_text, bool required) {
|
|
||||||
base_add_argument<T>(short_arg, long_arg, help_text, required);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::optional<T> get_optional(std::string const& arg) {
|
std::optional<T> get_optional(std::string const& arg) {
|
||||||
auto id = find_argument_id(arg);
|
auto id = find_argument_id(arg);
|
||||||
if (id.has_value()) return std::any_cast<T>(stored_arguments[id.value()]);
|
if (id.has_value()) {
|
||||||
|
auto value = stored_arguments[id.value()];
|
||||||
|
if (value.has_value()) return std::any_cast<T>(value);
|
||||||
|
}
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -105,10 +105,9 @@ auto make_grep_action(argument_parser::base_parser& parser) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// std::vector<std::string> fake_args = { "-g", "add", "-f", "src/main.cpp", "-ep", "1,2" };
|
|
||||||
// auto parser = argument_parser::fake_parser{"test", std::move(fake_args)};
|
|
||||||
auto parser = argument_parser::parser{};
|
auto parser = argument_parser::parser{};
|
||||||
auto [file, grep] = make_grep_action(parser);
|
auto [file, grep] = make_grep_action(parser);
|
||||||
|
|
||||||
parser.add_argument("e", "echo", "echoes given variable", echo, false);
|
parser.add_argument("e", "echo", "echoes given variable", echo, false);
|
||||||
parser.add_argument("ep", "echo-point", "echoes given point", echo_point, false);
|
parser.add_argument("ep", "echo-point", "echoes given point", echo_point, false);
|
||||||
parser.add_argument("f", "file", "File to grep, required only if using grep", file, false);
|
parser.add_argument("f", "file", "File to grep, required only if using grep", file, false);
|
||||||
|
|||||||
Reference in New Issue
Block a user