| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |  * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-15 13:21:23 +02:00
										 |  |  | #include <AK/Format.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-17 15:35:30 +02:00
										 |  |  | #include <AK/StringBuilder.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/ArgsParser.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  | #include <getopt.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-01 17:24:53 +01:00
										 |  |  | #include <limits.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-08 12:05:14 +01:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 15:57:22 -05:00
										 |  |  | static constexpr bool isnan(double __x) { return __builtin_isnan(__x); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Optional<double> convert_to_double(const char* s) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     char* p; | 
					
						
							|  |  |  |     double v = strtod(s, &p); | 
					
						
							|  |  |  |     if (isnan(v) || p == s) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     return v; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ArgsParser::ArgsParser() | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     add_option(m_show_help, "Display this message", "help", 0); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  | bool ArgsParser::parse(int argc, char** argv, bool exit_on_failure) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  |     auto print_usage_and_exit = [this, argv, exit_on_failure] { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         print_usage(stderr, argv[0]); | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  |         if (exit_on_failure) | 
					
						
							|  |  |  |             exit(1); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     Vector<option> long_options; | 
					
						
							|  |  |  |     StringBuilder short_options_builder; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int index_of_found_long_option = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-30 14:39:59 +03:00
										 |  |  |     // Tell getopt() to reset its internal state, and start scanning from optind = 1.
 | 
					
						
							|  |  |  |     // We could also set optreset = 1, but the host platform may not support that.
 | 
					
						
							| 
									
										
										
										
											2020-05-16 01:47:10 +04:30
										 |  |  |     optind = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |     for (size_t i = 0; i < m_options.size(); i++) { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         auto& opt = m_options[i]; | 
					
						
							|  |  |  |         if (opt.long_name) { | 
					
						
							|  |  |  |             option long_opt { | 
					
						
							|  |  |  |                 opt.long_name, | 
					
						
							|  |  |  |                 opt.requires_argument ? required_argument : no_argument, | 
					
						
							|  |  |  |                 &index_of_found_long_option, | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |                 static_cast<int>(i) | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             }; | 
					
						
							|  |  |  |             long_options.append(long_opt); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (opt.short_name) { | 
					
						
							|  |  |  |             short_options_builder.append(opt.short_name); | 
					
						
							|  |  |  |             if (opt.requires_argument) | 
					
						
							|  |  |  |                 short_options_builder.append(':'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     long_options.append({ 0, 0, 0, 0 }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     String short_options = short_options_builder.build(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (true) { | 
					
						
							|  |  |  |         int c = getopt_long(argc, argv, short_options.characters(), long_options.data(), nullptr); | 
					
						
							|  |  |  |         if (c == -1) { | 
					
						
							|  |  |  |             // We have reached the end.
 | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } else if (c == '?') { | 
					
						
							|  |  |  |             // There was an error, and getopt() has already
 | 
					
						
							|  |  |  |             // printed its error message.
 | 
					
						
							|  |  |  |             print_usage_and_exit(); | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         // Let's see what option we just found.
 | 
					
						
							|  |  |  |         Option* found_option = nullptr; | 
					
						
							|  |  |  |         if (c == 0) { | 
					
						
							|  |  |  |             // It was a long option.
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY(index_of_found_long_option >= 0); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             found_option = &m_options[index_of_found_long_option]; | 
					
						
							|  |  |  |             index_of_found_long_option = -1; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             // It was a short option, look it up.
 | 
					
						
							| 
									
										
										
										
											2020-12-23 11:35:20 -07:00
										 |  |  |             auto it = m_options.find_if([c](auto& opt) { return c == opt.short_name; }); | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY(!it.is_end()); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             found_option = &*it; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(found_option); | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         const char* arg = found_option->requires_argument ? optarg : nullptr; | 
					
						
							|  |  |  |         if (!found_option->accept_value(arg)) { | 
					
						
							| 
									
										
										
										
											2020-10-15 13:21:23 +02:00
										 |  |  |             warnln("\033[31mInvalid value for option \033[1m{}\033[22m, dude\033[0m", found_option->name_for_display()); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             print_usage_and_exit(); | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     // We're done processing options, now let's parse positional arguments.
 | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     int values_left = argc - optind; | 
					
						
							| 
									
										
										
										
											2021-05-14 23:42:13 -06:00
										 |  |  |     Vector<int, 16> num_values_for_arg; | 
					
						
							|  |  |  |     num_values_for_arg.resize(m_positional_args.size(), true); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     int total_values_required = 0; | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |     for (size_t i = 0; i < m_positional_args.size(); i++) { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         auto& arg = m_positional_args[i]; | 
					
						
							|  |  |  |         num_values_for_arg[i] = arg.min_values; | 
					
						
							|  |  |  |         total_values_required += arg.min_values; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  |     if (total_values_required > values_left) { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         print_usage_and_exit(); | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     int extra_values_to_distribute = values_left - total_values_required; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |     for (size_t i = 0; i < m_positional_args.size(); i++) { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         auto& arg = m_positional_args[i]; | 
					
						
							|  |  |  |         int extra_values_to_this_arg = min(arg.max_values - arg.min_values, extra_values_to_distribute); | 
					
						
							|  |  |  |         num_values_for_arg[i] += extra_values_to_this_arg; | 
					
						
							|  |  |  |         extra_values_to_distribute -= extra_values_to_this_arg; | 
					
						
							|  |  |  |         if (extra_values_to_distribute == 0) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     if (extra_values_to_distribute > 0) { | 
					
						
							|  |  |  |         // We still have too many values :(
 | 
					
						
							|  |  |  |         print_usage_and_exit(); | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |     for (size_t i = 0; i < m_positional_args.size(); i++) { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         auto& arg = m_positional_args[i]; | 
					
						
							|  |  |  |         for (int j = 0; j < num_values_for_arg[i]; j++) { | 
					
						
							|  |  |  |             const char* value = argv[optind++]; | 
					
						
							|  |  |  |             if (!arg.accept_value(value)) { | 
					
						
							| 
									
										
										
										
											2020-10-15 13:21:23 +02:00
										 |  |  |                 warnln("Invalid value for argument {}", arg.name); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |                 print_usage_and_exit(); | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  |                 return false; | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     // We're done parsing! :)
 | 
					
						
							|  |  |  |     // Now let's show help if requested.
 | 
					
						
							|  |  |  |     if (m_show_help) { | 
					
						
							|  |  |  |         print_usage(stdout, argv[0]); | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  |         if (exit_on_failure) | 
					
						
							|  |  |  |             exit(0); | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | void ArgsParser::print_usage(FILE* file, const char* argv0) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |     out(file, "Usage:\n\t\033[1m{}\033[0m", argv0); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     for (auto& opt : m_options) { | 
					
						
							|  |  |  |         if (opt.long_name && !strcmp(opt.long_name, "help")) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         if (opt.requires_argument) | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, " [{} {}]", opt.name_for_display(), opt.value_name); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, " [{}]", opt.name_for_display()); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     } | 
					
						
							|  |  |  |     for (auto& arg : m_positional_args) { | 
					
						
							|  |  |  |         bool required = arg.min_values > 0; | 
					
						
							|  |  |  |         bool repeated = arg.max_values > 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (required && repeated) | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, " <{}...>", arg.name); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         else if (required && !repeated) | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, " <{}>", arg.name); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         else if (!required && repeated) | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, " [{}...]", arg.name); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         else if (!required && !repeated) | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, " [{}]", arg.name); | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-15 13:21:23 +02:00
										 |  |  |     outln(file); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:59:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-05 15:54:27 +01:00
										 |  |  |     if (m_general_help != nullptr && m_general_help[0] != '\0') { | 
					
						
							|  |  |  |         outln(file, "\nDescription:"); | 
					
						
							|  |  |  |         outln(file, m_general_help); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     if (!m_options.is_empty()) | 
					
						
							| 
									
										
										
										
											2020-10-15 13:21:23 +02:00
										 |  |  |         outln(file, "\nOptions:"); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     for (auto& opt : m_options) { | 
					
						
							|  |  |  |         auto print_argument = [&]() { | 
					
						
							|  |  |  |             if (opt.value_name) { | 
					
						
							|  |  |  |                 if (opt.requires_argument) | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |                     out(file, " {}", opt.value_name); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |                 else | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |                     out(file, " [{}]", opt.value_name); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             } | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |         out(file, "\t"); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         if (opt.short_name) { | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, "\033[1m-{}\033[0m", opt.short_name); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             print_argument(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (opt.short_name && opt.long_name) | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, ", "); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         if (opt.long_name) { | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, "\033[1m--{}\033[0m", opt.long_name); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             print_argument(); | 
					
						
							| 
									
										
										
										
											2019-05-17 15:17:43 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         if (opt.help_string) | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, "\t{}", opt.help_string); | 
					
						
							| 
									
										
										
										
											2020-10-15 13:21:23 +02:00
										 |  |  |         outln(file); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:59:37 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     if (!m_positional_args.is_empty()) | 
					
						
							| 
									
										
										
										
											2020-10-15 13:21:23 +02:00
										 |  |  |         outln(file, "\nArguments:"); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     for (auto& arg : m_positional_args) { | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |         out(file, "\t\033[1m{}\033[0m", arg.name); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         if (arg.help_string) | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, "\t{}", arg.help_string); | 
					
						
							| 
									
										
										
										
											2020-10-15 13:21:23 +02:00
										 |  |  |         outln(file); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | void ArgsParser::add_option(Option&& option) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     m_options.append(move(option)); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:51:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | void ArgsParser::add_option(bool& value, const char* help_string, const char* long_name, char short_name) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:51:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     Option option { | 
					
						
							|  |  |  |         false, | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         nullptr, | 
					
						
							|  |  |  |         [&value](const char* s) { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY(s == nullptr); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             value = true; | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:51:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | void ArgsParser::add_option(const char*& value, const char* help_string, const char* long_name, char short_name, const char* value_name) | 
					
						
							| 
									
										
										
										
											2021-04-17 20:39:32 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     Option option { | 
					
						
							|  |  |  |         true, | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							|  |  |  |         [&value](const char* s) { | 
					
						
							|  |  |  |             value = s; | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ArgsParser::add_option(String& value, const char* help_string, const char* long_name, char short_name, const char* value_name) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:51:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     Option option { | 
					
						
							|  |  |  |         true, | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							|  |  |  |         [&value](const char* s) { | 
					
						
							|  |  |  |             value = s; | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | void ArgsParser::add_option(int& value, const char* help_string, const char* long_name, char short_name, const char* value_name) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:59:37 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     Option option { | 
					
						
							|  |  |  |         true, | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							|  |  |  |         [&value](const char* s) { | 
					
						
							| 
									
										
										
										
											2020-06-12 21:07:52 +02:00
										 |  |  |             auto opt = StringView(s).to_int(); | 
					
						
							|  |  |  |             value = opt.value_or(0); | 
					
						
							|  |  |  |             return opt.has_value(); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:59:37 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 15:57:22 -05:00
										 |  |  | void ArgsParser::add_option(double& value, const char* help_string, const char* long_name, char short_name, const char* value_name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Option option { | 
					
						
							|  |  |  |         true, | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							|  |  |  |         [&value](const char* s) { | 
					
						
							|  |  |  |             auto opt = convert_to_double(s); | 
					
						
							|  |  |  |             value = opt.value_or(0.0); | 
					
						
							|  |  |  |             return opt.has_value(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | void ArgsParser::add_positional_argument(Arg&& arg) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:59:37 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     m_positional_args.append(move(arg)); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:59:37 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | void ArgsParser::add_positional_argument(const char*& value, const char* help_string, const char* name, Required required) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         1, | 
					
						
							|  |  |  |         [&value](const char* s) { | 
					
						
							|  |  |  |             value = s; | 
					
						
							|  |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  |     add_positional_argument(move(arg)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-05 21:38:06 +02:00
										 |  |  | void ArgsParser::add_positional_argument(String& value, const char* help_string, const char* name, Required required) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         1, | 
					
						
							|  |  |  |         [&value](const char* s) { | 
					
						
							|  |  |  |             value = s; | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_positional_argument(move(arg)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | void ArgsParser::add_positional_argument(int& value, const char* help_string, const char* name, Required required) | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         1, | 
					
						
							|  |  |  |         [&value](const char* s) { | 
					
						
							| 
									
										
										
										
											2020-06-12 21:07:52 +02:00
										 |  |  |             auto opt = StringView(s).to_int(); | 
					
						
							|  |  |  |             value = opt.value_or(0); | 
					
						
							|  |  |  |             return opt.has_value(); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  |     add_positional_argument(move(arg)); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-25 20:23:05 +03:00
										 |  |  | void ArgsParser::add_positional_argument(double& value, const char* help_string, const char* name, Required required) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         1, | 
					
						
							|  |  |  |         [&value](const char* s) { | 
					
						
							| 
									
										
										
										
											2020-11-05 15:57:22 -05:00
										 |  |  |             auto opt = convert_to_double(s); | 
					
						
							|  |  |  |             value = opt.value_or(0.0); | 
					
						
							|  |  |  |             return opt.has_value(); | 
					
						
							| 
									
										
										
										
											2020-07-25 20:23:05 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_positional_argument(move(arg)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | void ArgsParser::add_positional_argument(Vector<const char*>& values, const char* help_string, const char* name, Required required) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         INT_MAX, | 
					
						
							|  |  |  |         [&values](const char* s) { | 
					
						
							|  |  |  |             values.append(s); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_positional_argument(move(arg)); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 11:10:06 +02:00
										 |  |  | void ArgsParser::add_positional_argument(Vector<String>& values, const char* help_string, const char* name, Required required) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         INT_MAX, | 
					
						
							|  |  |  |         [&values](const char* s) { | 
					
						
							|  |  |  |             values.append(s); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_positional_argument(move(arg)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | } |