| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |  * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-01-16 18:06:07 -07:00
										 |  |  |  * Copyright (c) 2022, Matthias Zimmerman <matthias291999@gmail.com> | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  | #include "AK/JsonObject.h"
 | 
					
						
							| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2021-08-29 11:51:42 +02:00
										 |  |  | #include <LibCore/Version.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  | #include <getopt.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-01 17:24:53 +01:00
										 |  |  | #include <limits.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-14 18:33:26 -04:00
										 |  |  | #include <math.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | static Optional<double> convert_to_double(char const* s) | 
					
						
							| 
									
										
										
										
											2020-11-05 15:57:22 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |     add_option(m_show_help, "Display help message and exit", "help", 0, OptionHideMode::Markdown); | 
					
						
							|  |  |  |     add_option(m_show_version, "Print version", "version", 0, OptionHideMode::Markdown); | 
					
						
							|  |  |  |     add_option(m_perform_autocomplete, "Perform autocompletion", "complete", 0, OptionHideMode::CommandLineAndMarkdown); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-13 05:31:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-06 01:11:56 +02:00
										 |  |  | bool ArgsParser::parse(int argc, char* const* argv, FailureBehavior failure_behavior) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-06 01:11:56 +02:00
										 |  |  |     auto fail = [this, argv, failure_behavior] { | 
					
						
							|  |  |  |         if (failure_behavior == FailureBehavior::PrintUsage || failure_behavior == FailureBehavior::PrintUsageAndExit) | 
					
						
							|  |  |  |             print_usage(stderr, argv[0]); | 
					
						
							|  |  |  |         if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit) | 
					
						
							| 
									
										
										
										
											2020-05-16 01:50:46 +04:30
										 |  |  |             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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 21:20:35 +02:00
										 |  |  |     if (m_stop_on_first_non_option) | 
					
						
							|  |  |  |         short_options_builder.append('+'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     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, | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |                 opt.argument_mode == OptionArgumentMode::Required ? required_argument : (opt.argument_mode == OptionArgumentMode::Optional ? optional_argument : no_argument), | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |                 &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); | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |             if (opt.argument_mode != OptionArgumentMode::None) | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |                 short_options_builder.append(':'); | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |             // Note: This is a GNU extension.
 | 
					
						
							|  |  |  |             if (opt.argument_mode == OptionArgumentMode::Optional) | 
					
						
							|  |  |  |                 short_options_builder.append(':'); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     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.
 | 
					
						
							| 
									
										
										
										
											2021-06-06 01:11:56 +02:00
										 |  |  |             fail(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         char const* arg = found_option->argument_mode != OptionArgumentMode::None ? optarg : nullptr; | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         if (!found_option->accept_value(arg)) { | 
					
						
							| 
									
										
										
										
											2021-07-08 00:49:22 +02:00
										 |  |  |             warnln("\033[31mInvalid value for option \033[1m{}\033[22m\033[0m", found_option->name_for_display()); | 
					
						
							| 
									
										
										
										
											2021-06-06 01:11:56 +02:00
										 |  |  |             fail(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 00:53:14 +08:00
										 |  |  |     // We're done processing options.
 | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |     // Now let's show version or help if requested, or perform autocompletion if needed.
 | 
					
						
							| 
									
										
										
										
											2021-08-21 00:53:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_show_version) { | 
					
						
							|  |  |  |         print_version(stdout); | 
					
						
							|  |  |  |         if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit) | 
					
						
							|  |  |  |             exit(0); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 00:53:14 +08:00
										 |  |  |     if (m_show_help) { | 
					
						
							|  |  |  |         print_usage(stdout, argv[0]); | 
					
						
							|  |  |  |         if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit) | 
					
						
							|  |  |  |             exit(0); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |     if (m_perform_autocomplete) { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |         autocomplete(stdout, { argv[0], strlen(argv[0]) }, Span<char const* const> { argv + optind, static_cast<size_t>(argc - optind) }); | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |         if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit) | 
					
						
							|  |  |  |             exit(0); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 00:53:14 +08:00
										 |  |  |     // 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) { | 
					
						
							| 
									
										
										
										
											2021-06-06 01:11:56 +02:00
										 |  |  |         fail(); | 
					
						
							| 
									
										
										
										
											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 :(
 | 
					
						
							| 
									
										
										
										
											2021-06-06 01:11:56 +02:00
										 |  |  |         fail(); | 
					
						
							| 
									
										
										
										
											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++) { | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |             char const* value = argv[optind++]; | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             if (!arg.accept_value(value)) { | 
					
						
							| 
									
										
										
										
											2020-10-15 13:21:23 +02:00
										 |  |  |                 warnln("Invalid value for argument {}", arg.name); | 
					
						
							| 
									
										
										
										
											2021-06-06 01:11:56 +02:00
										 |  |  |                 fail(); | 
					
						
							| 
									
										
										
										
											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-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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | void ArgsParser::print_usage(FILE* file, char const* argv0) | 
					
						
							| 
									
										
										
										
											2021-10-23 13:22:06 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     char const* env_preference = getenv("ARGSPARSER_EMIT_MARKDOWN"); | 
					
						
							|  |  |  |     if (env_preference != nullptr && env_preference[0] == '1' && env_preference[1] == 0) { | 
					
						
							|  |  |  |         print_usage_markdown(file, argv0); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         print_usage_terminal(file, argv0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | void ArgsParser::print_usage_terminal(FILE* file, char const* 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) { | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         if (opt.hide_mode != OptionHideMode::None) | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         if (opt.argument_mode == OptionArgumentMode::Required) | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |             out(file, " [{} {}]", opt.name_for_display(), opt.value_name); | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |         else if (opt.argument_mode == OptionArgumentMode::Optional) | 
					
						
							|  |  |  |             out(file, " [{}[{}{}]]", opt.name_for_display(), opt.long_name ? "="sv : ""sv, 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:"); | 
					
						
							| 
									
										
										
										
											2021-09-09 17:06:15 +02:00
										 |  |  |         outln(file, "{}", m_general_help); | 
					
						
							| 
									
										
										
										
											2020-12-05 15:54:27 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         if (opt.hide_mode == OptionHideMode::CommandLineAndMarkdown) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |         auto print_argument = [&](StringView value_delimiter) { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             if (opt.value_name) { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |                 if (opt.argument_mode == OptionArgumentMode::Required) | 
					
						
							| 
									
										
										
										
											2020-11-09 10:47:19 +01:00
										 |  |  |                     out(file, " {}", opt.value_name); | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |                 if (opt.argument_mode == OptionArgumentMode::Optional) | 
					
						
							|  |  |  |                     out(file, "[{}{}]", value_delimiter, 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); | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |             print_argument(""sv); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |         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); | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |             print_argument("="sv); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | void ArgsParser::print_usage_markdown(FILE* file, char const* argv0) | 
					
						
							| 
									
										
										
										
											2021-10-23 13:22:06 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     outln(file, "## Name\n\n{}", argv0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     out(file, "\n## Synopsis\n\n```sh\n$ {}", argv0); | 
					
						
							|  |  |  |     for (auto& opt : m_options) { | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         if (opt.hide_mode != OptionHideMode::None) | 
					
						
							| 
									
										
										
										
											2021-10-23 13:22:06 +02:00
										 |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2022-07-11 19:59:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // FIXME: We allow opt.value_name to be empty even if the option
 | 
					
						
							|  |  |  |         //        requires an argument. This should be disallowed as it will
 | 
					
						
							|  |  |  |         //        currently display a blank name after the option.
 | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         if (opt.argument_mode == OptionArgumentMode::Required) | 
					
						
							| 
									
										
										
										
											2022-07-11 19:59:54 +00:00
										 |  |  |             out(file, " [{} {}]", opt.name_for_display(), opt.value_name ?: ""); | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |         else if (opt.argument_mode == OptionArgumentMode::Optional) | 
					
						
							|  |  |  |             out(file, " [{}[{}{}]]", opt.name_for_display(), opt.long_name ? "="sv : ""sv, opt.value_name); | 
					
						
							| 
									
										
										
										
											2021-10-23 13:22:06 +02:00
										 |  |  |         else | 
					
						
							|  |  |  |             out(file, " [{}]", opt.name_for_display()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     for (auto& arg : m_positional_args) { | 
					
						
							|  |  |  |         bool required = arg.min_values > 0; | 
					
						
							|  |  |  |         bool repeated = arg.max_values > 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (required && repeated) | 
					
						
							|  |  |  |             out(file, " <{}...>", arg.name); | 
					
						
							|  |  |  |         else if (required && !repeated) | 
					
						
							|  |  |  |             out(file, " <{}>", arg.name); | 
					
						
							|  |  |  |         else if (!required && repeated) | 
					
						
							|  |  |  |             out(file, " [{}...]", arg.name); | 
					
						
							|  |  |  |         else if (!required && !repeated) | 
					
						
							|  |  |  |             out(file, " [{}]", arg.name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     outln(file, "\n```"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_general_help != nullptr && m_general_help[0] != '\0') { | 
					
						
							|  |  |  |         outln(file, "\n## Description\n\n{}", m_general_help); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 23:18:23 +00:00
										 |  |  |     auto should_display_option = [](Option& opt) { | 
					
						
							|  |  |  |         return !(opt.hide_mode == OptionHideMode::Markdown || opt.hide_mode == OptionHideMode::CommandLineAndMarkdown); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t options_to_display = 0; | 
					
						
							|  |  |  |     for (auto& opt : m_options) { | 
					
						
							|  |  |  |         if (!should_display_option(opt)) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         options_to_display++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (options_to_display > 0) | 
					
						
							| 
									
										
										
										
											2021-10-23 13:22:06 +02:00
										 |  |  |         outln(file, "\n## Options:\n"); | 
					
						
							|  |  |  |     for (auto& opt : m_options) { | 
					
						
							| 
									
										
										
										
											2022-04-03 23:18:23 +00:00
										 |  |  |         if (!should_display_option(opt)) | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |             continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |         auto print_argument = [&](StringView value_delimiter) { | 
					
						
							| 
									
										
										
										
											2021-10-23 13:22:06 +02:00
										 |  |  |             if (opt.value_name != nullptr) { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |                 if (opt.argument_mode == OptionArgumentMode::Required) | 
					
						
							| 
									
										
										
										
											2021-10-23 13:22:06 +02:00
										 |  |  |                     out(file, " {}", opt.value_name); | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |                 if (opt.argument_mode == OptionArgumentMode::Optional) | 
					
						
							|  |  |  |                     out(file, "[{}{}]", value_delimiter, opt.value_name); | 
					
						
							| 
									
										
										
										
											2021-10-23 13:22:06 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         out(file, "* "); | 
					
						
							|  |  |  |         if (opt.short_name != '\0') { | 
					
						
							|  |  |  |             out(file, "`-{}", opt.short_name); | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |             print_argument(""sv); | 
					
						
							| 
									
										
										
										
											2021-10-23 13:22:06 +02:00
										 |  |  |             out(file, "`"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (opt.short_name != '\0' && opt.long_name != nullptr) | 
					
						
							|  |  |  |             out(file, ", "); | 
					
						
							|  |  |  |         if (opt.long_name != nullptr) { | 
					
						
							|  |  |  |             out(file, "`--{}", opt.long_name); | 
					
						
							| 
									
										
										
										
											2022-07-12 23:05:06 +02:00
										 |  |  |             print_argument("="sv); | 
					
						
							| 
									
										
										
										
											2021-10-23 13:22:06 +02:00
										 |  |  |             out(file, "`"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (opt.help_string != nullptr) | 
					
						
							|  |  |  |             out(file, ": {}", opt.help_string); | 
					
						
							|  |  |  |         outln(file); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!m_positional_args.is_empty()) | 
					
						
							|  |  |  |         outln(file, "\n## Arguments:\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (auto& arg : m_positional_args) { | 
					
						
							|  |  |  |         out(file, "* `{}`", arg.name); | 
					
						
							|  |  |  |         if (arg.help_string != nullptr) | 
					
						
							|  |  |  |             out(file, ": {}", arg.help_string); | 
					
						
							|  |  |  |         outln(file); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-14 18:50:52 -04:00
										 |  |  | void ArgsParser::print_version(FILE* file) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-30 12:49:08 +02:00
										 |  |  |     outln(file, Core::Version::read_long_version_string()); | 
					
						
							| 
									
										
										
										
											2021-08-14 18:50:52 -04: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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  | void ArgsParser::add_ignored(char const* long_name, char short_name, OptionHideMode hide_mode) | 
					
						
							| 
									
										
										
										
											2021-10-23 13:44:04 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     Option option { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         OptionArgumentMode::None, | 
					
						
							| 
									
										
										
										
											2021-10-23 13:44:04 +02:00
										 |  |  |         "Ignored", | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         nullptr, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [](char const*) { | 
					
						
							| 
									
										
										
										
											2021-10-23 13:44:04 +02:00
										 |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         hide_mode, | 
					
						
							| 
									
										
										
										
											2021-10-23 13:44:04 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  | void ArgsParser::add_option(bool& value, char const* help_string, char const* long_name, char short_name, OptionHideMode hide_mode) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:51:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     Option option { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         OptionArgumentMode::None, | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         nullptr, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY(s == nullptr); | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             value = true; | 
					
						
							|  |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         hide_mode, | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:51:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  | void ArgsParser::add_option(char const*& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode) | 
					
						
							| 
									
										
										
										
											2021-04-17 20:39:32 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     Option option { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         OptionArgumentMode::Required, | 
					
						
							| 
									
										
										
										
											2021-04-17 20:39:32 +02:00
										 |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2021-04-17 20:39:32 +02:00
										 |  |  |             value = s; | 
					
						
							|  |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         hide_mode, | 
					
						
							| 
									
										
										
										
											2021-04-17 20:39:32 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  | void ArgsParser::add_option(String& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:51:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     Option option { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         OptionArgumentMode::Required, | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             value = s; | 
					
						
							|  |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         hide_mode, | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:46:51 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  | void ArgsParser::add_option(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode) | 
					
						
							| 
									
										
										
										
											2021-06-01 09:01:31 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     Option option { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         OptionArgumentMode::Required, | 
					
						
							| 
									
										
										
										
											2021-06-01 09:01:31 +02:00
										 |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |             value = { s, strlen(s) }; | 
					
						
							| 
									
										
										
										
											2021-06-01 09:01:31 +02:00
										 |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         hide_mode, | 
					
						
							| 
									
										
										
										
											2021-06-01 09:01:31 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 11:05:19 +02:00
										 |  |  | template<typename Integral> | 
					
						
							|  |  |  | void ArgsParser::add_option(Integral& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode) requires(IsIntegral<Integral>) | 
					
						
							| 
									
										
										
										
											2019-05-17 12:59:37 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     Option option { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         OptionArgumentMode::Required, | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2022-07-21 11:05:19 +02:00
										 |  |  |             auto view = StringView { s, strlen(s) }; | 
					
						
							|  |  |  |             Optional<Integral> opt; | 
					
						
							|  |  |  |             if constexpr (IsSigned<Integral>) | 
					
						
							|  |  |  |                 opt = view.to_int<Integral>(); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 opt = view.to_uint<Integral>(); | 
					
						
							| 
									
										
										
										
											2020-06-12 21:07:52 +02:00
										 |  |  |             value = opt.value_or(0); | 
					
						
							|  |  |  |             return opt.has_value(); | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         hide_mode, | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							| 
									
										
										
										
											2019-05-17 12:59:37 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 11:05:19 +02:00
										 |  |  | template void ArgsParser::add_option(int&, char const*, char const*, char, char const*, OptionHideMode); | 
					
						
							| 
									
										
										
										
											2022-07-21 11:18:46 +02:00
										 |  |  | template void ArgsParser::add_option(long&, char const*, char const*, char, char const*, OptionHideMode); | 
					
						
							|  |  |  | template void ArgsParser::add_option(long long&, char const*, char const*, char, char const*, OptionHideMode); | 
					
						
							| 
									
										
										
										
											2022-07-21 11:05:19 +02:00
										 |  |  | template void ArgsParser::add_option(unsigned&, char const*, char const*, char, char const*, OptionHideMode); | 
					
						
							| 
									
										
										
										
											2022-07-21 11:18:46 +02:00
										 |  |  | template void ArgsParser::add_option(unsigned long&, char const*, char const*, char, char const*, OptionHideMode); | 
					
						
							|  |  |  | template void ArgsParser::add_option(unsigned long long&, char const*, char const*, char, char const*, OptionHideMode); | 
					
						
							| 
									
										
										
										
											2021-07-06 18:18:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  | void ArgsParser::add_option(double& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode) | 
					
						
							| 
									
										
										
										
											2020-11-05 15:57:22 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     Option option { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         OptionArgumentMode::Required, | 
					
						
							| 
									
										
										
										
											2020-11-05 15:57:22 -05:00
										 |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* 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(); | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         hide_mode, | 
					
						
							| 
									
										
										
										
											2020-11-05 15:57:22 -05:00
										 |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  | void ArgsParser::add_option(Optional<double>& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode) | 
					
						
							| 
									
										
										
										
											2022-01-16 18:06:07 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     Option option { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         OptionArgumentMode::Required, | 
					
						
							| 
									
										
										
										
											2022-01-16 18:06:07 -07:00
										 |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2022-01-16 18:06:07 -07:00
										 |  |  |             value = convert_to_double(s); | 
					
						
							|  |  |  |             return value.has_value(); | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         hide_mode, | 
					
						
							| 
									
										
										
										
											2022-01-16 18:06:07 -07:00
										 |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  | void ArgsParser::add_option(Optional<size_t>& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode) | 
					
						
							| 
									
										
										
										
											2022-02-17 06:05:16 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     Option option { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         OptionArgumentMode::Required, | 
					
						
							| 
									
										
										
										
											2022-02-17 06:05:16 -05:00
										 |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |             value = AK::StringUtils::convert_to_uint<size_t>({ s, strlen(s) }); | 
					
						
							| 
									
										
										
										
											2022-02-17 06:05:16 -05:00
										 |  |  |             return value.has_value(); | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         hide_mode, | 
					
						
							| 
									
										
										
										
											2022-02-17 06:05:16 -05:00
										 |  |  |     }; | 
					
						
							|  |  |  |     add_option(move(option)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  | void ArgsParser::add_option(Vector<size_t>& values, char const* help_string, char const* long_name, char short_name, char const* value_name, char separator, OptionHideMode hide_mode) | 
					
						
							| 
									
										
										
										
											2022-03-25 22:11:05 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     Option option { | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         OptionArgumentMode::Required, | 
					
						
							| 
									
										
										
										
											2022-03-25 22:11:05 +01:00
										 |  |  |         help_string, | 
					
						
							|  |  |  |         long_name, | 
					
						
							|  |  |  |         short_name, | 
					
						
							|  |  |  |         value_name, | 
					
						
							|  |  |  |         [&values, separator](char const* s) { | 
					
						
							|  |  |  |             bool parsed_all_values = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |             StringView { s, strlen(s) }.for_each_split_view(separator, false, [&](auto value) { | 
					
						
							| 
									
										
										
										
											2022-03-25 22:11:05 +01:00
										 |  |  |                 if (auto maybe_value = AK::StringUtils::convert_to_uint<size_t>(value); maybe_value.has_value()) | 
					
						
							|  |  |  |                     values.append(*maybe_value); | 
					
						
							|  |  |  |                 else | 
					
						
							|  |  |  |                     parsed_all_values = false; | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return parsed_all_values; | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |         hide_mode | 
					
						
							| 
									
										
										
										
											2022-03-25 22:11:05 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | void ArgsParser::add_positional_argument(char const*& value, char const* help_string, char const* 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, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | void ArgsParser::add_positional_argument(String& value, char const* help_string, char const* name, Required required) | 
					
						
							| 
									
										
										
										
											2021-03-05 21:38:06 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         1, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2021-03-05 21:38:06 +02:00
										 |  |  |             value = s; | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_positional_argument(move(arg)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-01 09:01:31 +02:00
										 |  |  | void ArgsParser::add_positional_argument(StringView& value, char const* help_string, char const* name, Required required) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         1, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |             value = { s, strlen(s) }; | 
					
						
							| 
									
										
										
										
											2021-06-01 09:01:31 +02:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_positional_argument(move(arg)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | void ArgsParser::add_positional_argument(int& value, char const* help_string, char const* name, Required required) | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         1, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |             auto opt = StringView { s, strlen(s) }.to_int(); | 
					
						
							| 
									
										
										
										
											2020-06-12 21:07:52 +02:00
										 |  |  |             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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | void ArgsParser::add_positional_argument(unsigned& value, char const* help_string, char const* name, Required required) | 
					
						
							| 
									
										
										
										
											2021-07-06 18:18:10 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         1, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* s) { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |             auto opt = StringView { s, strlen(s) }.to_uint(); | 
					
						
							| 
									
										
										
										
											2021-07-06 18:18:10 +02:00
										 |  |  |             value = opt.value_or(0); | 
					
						
							|  |  |  |             return opt.has_value(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_positional_argument(move(arg)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | void ArgsParser::add_positional_argument(double& value, char const* help_string, char const* name, Required required) | 
					
						
							| 
									
										
										
										
											2020-07-25 20:23:05 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         1, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&value](char const* 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)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | void ArgsParser::add_positional_argument(Vector<char const*>& values, char const* help_string, char const* 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, | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         [&values](char const* s) { | 
					
						
							| 
									
										
										
										
											2020-01-27 20:19:39 +03:00
										 |  |  |             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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 15:32:51 +01:00
										 |  |  | void ArgsParser::add_positional_argument(Vector<String>& values, char const* help_string, char const* name, Required required) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         INT_MAX, | 
					
						
							|  |  |  |         [&values](char const* s) { | 
					
						
							|  |  |  |             values.append(s); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_positional_argument(move(arg)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-26 22:09:09 +01:00
										 |  |  | void ArgsParser::add_positional_argument(Vector<StringView>& values, char const* help_string, char const* name, Required required) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Arg arg { | 
					
						
							|  |  |  |         help_string, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         required == Required::Yes ? 1 : 0, | 
					
						
							|  |  |  |         INT_MAX, | 
					
						
							|  |  |  |         [&values](char const* s) { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |             values.append({ s, strlen(s) }); | 
					
						
							| 
									
										
										
										
											2021-11-26 22:09:09 +01:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     add_positional_argument(move(arg)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  | void ArgsParser::autocomplete(FILE* file, StringView program_name, Span<char const* const> remaining_arguments) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // We expect the full invocation of the program to be available as positional args,
 | 
					
						
							|  |  |  |     // e.g. `foo --bar arg -b` (program invoked as `foo --complete -- foo --bar arg -b`)
 | 
					
						
							|  |  |  |     auto first = true; | 
					
						
							|  |  |  |     auto seen_all_options = false; | 
					
						
							|  |  |  |     auto skip_next = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     StringView argument_to_complete; | 
					
						
							|  |  |  |     StringView option_to_complete; | 
					
						
							|  |  |  |     auto completing_option = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (auto& arg : remaining_arguments) { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |         StringView argument { arg, strlen(arg) }; | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |         completing_option = false; | 
					
						
							|  |  |  |         if (skip_next) { | 
					
						
							|  |  |  |             argument_to_complete = argument; | 
					
						
							|  |  |  |             skip_next = false; | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Skip over the program name.
 | 
					
						
							|  |  |  |         if (first && program_name == argument) { | 
					
						
							|  |  |  |             first = false; | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (seen_all_options) { | 
					
						
							|  |  |  |             argument_to_complete = argument; | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         if (argument.starts_with("--"sv)) { | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |             option_to_complete = argument; | 
					
						
							|  |  |  |             completing_option = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (argument == "--") { | 
					
						
							|  |  |  |                 seen_all_options = true; | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Look for a long option
 | 
					
						
							|  |  |  |             auto option_pattern = argument.substring_view(2); | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |             auto it = m_options.find_if([&](auto& option) { return option.hide_mode != OptionHideMode::None && StringView { option.long_name, strlen(option.long_name) } == option_pattern; }); | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |             if (it.is_end()) | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |             if (it->argument_mode == OptionArgumentMode::Required) | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |                 skip_next = true; | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 20:10:18 +00:00
										 |  |  |         if (argument.starts_with('-')) { | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |             option_to_complete = argument; | 
					
						
							|  |  |  |             completing_option = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (argument == "-") { | 
					
						
							|  |  |  |                 option_to_complete = argument; | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Look for a short option
 | 
					
						
							|  |  |  |             auto option_pattern = argument[argument.length() - 1]; | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |             auto it = m_options.find_if([&](auto& option) { return option.hide_mode != OptionHideMode::None && option.short_name == option_pattern; }); | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |             if (it.is_end()) | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |             if (it->argument_mode == OptionArgumentMode::Required) | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |                 skip_next = true; | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // We don't know how to complete arguments quite yet.
 | 
					
						
							|  |  |  |     if (!completing_option) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 21:53:12 +04:30
										 |  |  |     auto write_completion = [&](auto format, auto& option, auto has_invariant, auto... args) { | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |         JsonObject object; | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |         object.set("completion", String::formatted(StringView { format, strlen(format) }, args...)); | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |         object.set("static_offset", 0); | 
					
						
							| 
									
										
										
										
											2022-03-28 21:53:12 +04:30
										 |  |  |         object.set("invariant_offset", has_invariant ? option_to_complete.length() : 0u); | 
					
						
							| 
									
										
										
										
											2022-03-23 17:57:54 +04:30
										 |  |  |         object.set("display_trivia", option.help_string); | 
					
						
							| 
									
										
										
										
											2022-07-12 22:13:38 +02:00
										 |  |  |         object.set("trailing_trivia", option.argument_mode == OptionArgumentMode::Required ? " " : ""); | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |         outln(file, "{}", object.to_string()); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     if (option_to_complete.starts_with("--"sv)) { | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |         // Complete a long option.
 | 
					
						
							|  |  |  |         auto option_pattern = option_to_complete.substring_view(2); | 
					
						
							|  |  |  |         for (auto& option : m_options) { | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |             if (option.hide_mode != OptionHideMode::None) | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  |                 continue; | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |             StringView option_string { option.long_name, strlen(option.long_name) }; | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |             if (option_string.starts_with(option_pattern)) { | 
					
						
							| 
									
										
										
										
											2022-03-28 21:53:12 +04:30
										 |  |  |                 write_completion("--{}", option, true, option_string); | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         // Complete a short option, note that we're not going to attempt to 'match' anything here.
 | 
					
						
							|  |  |  |         for (auto& option : m_options) { | 
					
						
							| 
									
										
										
										
											2022-04-03 22:33:09 +00:00
										 |  |  |             if (option.hide_mode != OptionHideMode::None) | 
					
						
							| 
									
										
										
										
											2022-03-23 23:24:36 +04:30
										 |  |  |                 continue; | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |             if (option.short_name == 0) | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 21:53:12 +04:30
										 |  |  |             auto has_invariant = option_to_complete == "-"; | 
					
						
							|  |  |  |             write_completion("{}{}", option, has_invariant, has_invariant ? "-" : "", option.short_name); | 
					
						
							| 
									
										
										
										
											2022-03-23 13:16:43 +04:30
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | } |