123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- /*
- * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <AK/Format.h>
- #include <AK/StringBuilder.h>
- #include <LibCore/ArgsParser.h>
- #include <getopt.h>
- #include <limits.h>
- #include <stdio.h>
- #include <string.h>
- 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;
- }
- namespace Core {
- ArgsParser::ArgsParser()
- {
- add_option(m_show_help, "Display this message", "help", 0);
- }
- bool ArgsParser::parse(int argc, char* const* argv, FailureBehavior failure_behavior)
- {
- 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)
- exit(1);
- };
- Vector<option> long_options;
- StringBuilder short_options_builder;
- if (m_stop_on_first_non_option)
- short_options_builder.append('+');
- int index_of_found_long_option = -1;
- // 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.
- optind = 0;
- for (size_t i = 0; i < m_options.size(); i++) {
- 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,
- static_cast<int>(i)
- };
- 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.
- fail();
- return false;
- }
- // Let's see what option we just found.
- Option* found_option = nullptr;
- if (c == 0) {
- // It was a long option.
- VERIFY(index_of_found_long_option >= 0);
- found_option = &m_options[index_of_found_long_option];
- index_of_found_long_option = -1;
- } else {
- // It was a short option, look it up.
- auto it = m_options.find_if([c](auto& opt) { return c == opt.short_name; });
- VERIFY(!it.is_end());
- found_option = &*it;
- }
- VERIFY(found_option);
- const char* arg = found_option->requires_argument ? optarg : nullptr;
- if (!found_option->accept_value(arg)) {
- warnln("\033[31mInvalid value for option \033[1m{}\033[22m, dude\033[0m", found_option->name_for_display());
- fail();
- return false;
- }
- }
- // We're done processing options, now let's parse positional arguments.
- int values_left = argc - optind;
- Vector<int, 16> num_values_for_arg;
- num_values_for_arg.resize(m_positional_args.size(), true);
- int total_values_required = 0;
- for (size_t i = 0; i < m_positional_args.size(); i++) {
- auto& arg = m_positional_args[i];
- num_values_for_arg[i] = arg.min_values;
- total_values_required += arg.min_values;
- }
- if (total_values_required > values_left) {
- fail();
- return false;
- }
- int extra_values_to_distribute = values_left - total_values_required;
- for (size_t i = 0; i < m_positional_args.size(); i++) {
- 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;
- }
- if (extra_values_to_distribute > 0) {
- // We still have too many values :(
- fail();
- return false;
- }
- for (size_t i = 0; i < m_positional_args.size(); i++) {
- 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)) {
- warnln("Invalid value for argument {}", arg.name);
- fail();
- return false;
- }
- }
- }
- // We're done parsing! :)
- // Now let's show help if requested.
- if (m_show_help) {
- print_usage(stdout, argv[0]);
- if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit)
- exit(0);
- return false;
- }
- return true;
- }
- void ArgsParser::print_usage(FILE* file, const char* argv0)
- {
- out(file, "Usage:\n\t\033[1m{}\033[0m", argv0);
- for (auto& opt : m_options) {
- if (opt.long_name && !strcmp(opt.long_name, "help"))
- continue;
- if (opt.requires_argument)
- out(file, " [{} {}]", opt.name_for_display(), opt.value_name);
- 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);
- if (m_general_help != nullptr && m_general_help[0] != '\0') {
- outln(file, "\nDescription:");
- outln(file, m_general_help);
- }
- if (!m_options.is_empty())
- outln(file, "\nOptions:");
- for (auto& opt : m_options) {
- auto print_argument = [&]() {
- if (opt.value_name) {
- if (opt.requires_argument)
- out(file, " {}", opt.value_name);
- else
- out(file, " [{}]", opt.value_name);
- }
- };
- out(file, "\t");
- if (opt.short_name) {
- out(file, "\033[1m-{}\033[0m", opt.short_name);
- print_argument();
- }
- if (opt.short_name && opt.long_name)
- out(file, ", ");
- if (opt.long_name) {
- out(file, "\033[1m--{}\033[0m", opt.long_name);
- print_argument();
- }
- if (opt.help_string)
- out(file, "\t{}", opt.help_string);
- outln(file);
- }
- if (!m_positional_args.is_empty())
- outln(file, "\nArguments:");
- for (auto& arg : m_positional_args) {
- out(file, "\t\033[1m{}\033[0m", arg.name);
- if (arg.help_string)
- out(file, "\t{}", arg.help_string);
- outln(file);
- }
- }
- void ArgsParser::add_option(Option&& option)
- {
- m_options.append(move(option));
- }
- void ArgsParser::add_option(bool& value, const char* help_string, const char* long_name, char short_name)
- {
- Option option {
- false,
- help_string,
- long_name,
- short_name,
- nullptr,
- [&value](const char* s) {
- VERIFY(s == nullptr);
- value = true;
- return true;
- }
- };
- add_option(move(option));
- }
- void ArgsParser::add_option(const char*& 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) {
- 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)
- {
- 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(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name)
- {
- 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(int& 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 = StringView(s).to_int();
- value = opt.value_or(0);
- return opt.has_value();
- }
- };
- add_option(move(option));
- }
- 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));
- }
- void ArgsParser::add_positional_argument(Arg&& arg)
- {
- m_positional_args.append(move(arg));
- }
- void ArgsParser::add_positional_argument(const char*& 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));
- }
- 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));
- }
- 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,
- [&value](const char* s) {
- value = s;
- return true;
- }
- };
- add_positional_argument(move(arg));
- }
- void ArgsParser::add_positional_argument(int& 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) {
- auto opt = StringView(s).to_int();
- value = opt.value_or(0);
- return opt.has_value();
- }
- };
- add_positional_argument(move(arg));
- }
- 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) {
- auto opt = convert_to_double(s);
- value = opt.value_or(0.0);
- return opt.has_value();
- }
- };
- add_positional_argument(move(arg));
- }
- void ArgsParser::add_positional_argument(Vector<const char*>& 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));
- }
- 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));
- }
- }
|