2017-07-08 05:32:41 +08:00
|
|
|
// Implementation of the argparse builtin.
|
|
|
|
//
|
|
|
|
// See issue #4190 for the rationale behind the original behavior of this builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2017-07-11 11:58:19 +08:00
|
|
|
#include <errno.h>
|
2017-07-08 05:32:41 +08:00
|
|
|
#include <stddef.h>
|
2017-07-13 07:40:53 +08:00
|
|
|
#include <stdint.h>
|
2017-07-08 05:32:41 +08:00
|
|
|
#include <wchar.h>
|
|
|
|
|
2017-07-11 11:58:19 +08:00
|
|
|
#include <algorithm>
|
2017-07-08 05:32:41 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2017-08-20 00:55:06 +08:00
|
|
|
#include <unordered_map>
|
2017-07-08 05:32:41 +08:00
|
|
|
#include <utility>
|
2017-07-11 11:58:19 +08:00
|
|
|
#include <vector>
|
2017-07-08 05:32:41 +08:00
|
|
|
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "builtin_argparse.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "env.h"
|
2017-07-17 12:10:22 +08:00
|
|
|
#include "exec.h"
|
2017-07-08 05:32:41 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "io.h"
|
|
|
|
#include "wgetopt.h" // IWYU pragma: keep
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
|
|
|
|
2018-07-22 08:58:22 +08:00
|
|
|
static const wcstring var_name_prefix = L"_flag_";
|
2017-07-08 05:32:41 +08:00
|
|
|
|
|
|
|
#define BUILTIN_ERR_INVALID_OPT_SPEC _(L"%ls: Invalid option spec '%ls' at char '%lc'\n")
|
|
|
|
|
2018-07-22 08:41:05 +08:00
|
|
|
struct option_spec_t {
|
|
|
|
const wchar_t short_flag;
|
2017-07-08 05:32:41 +08:00
|
|
|
wcstring long_flag;
|
2017-07-17 12:10:22 +08:00
|
|
|
wcstring validation_command;
|
2017-07-08 05:32:41 +08:00
|
|
|
wcstring_list_t vals;
|
2018-07-22 08:41:05 +08:00
|
|
|
bool short_flag_valid{true};
|
|
|
|
int num_allowed{0};
|
|
|
|
int num_seen{0};
|
|
|
|
|
|
|
|
option_spec_t(wchar_t s) : short_flag(s) {}
|
2017-07-08 05:32:41 +08:00
|
|
|
};
|
2018-07-22 08:41:05 +08:00
|
|
|
using option_spec_ref_t = std::unique_ptr<option_spec_t>;
|
2017-07-08 05:32:41 +08:00
|
|
|
|
2018-07-22 08:41:05 +08:00
|
|
|
struct argparse_cmd_opts_t {
|
2017-07-20 02:13:59 +08:00
|
|
|
bool print_help = false;
|
2017-07-13 07:40:53 +08:00
|
|
|
bool stop_nonopt = false;
|
2017-07-12 08:15:11 +08:00
|
|
|
size_t min_args = 0;
|
2017-07-12 11:25:08 +08:00
|
|
|
size_t max_args = SIZE_MAX;
|
2017-07-17 09:27:41 +08:00
|
|
|
wchar_t implicit_int_flag = L'\0';
|
2017-07-08 05:32:41 +08:00
|
|
|
wcstring name = L"argparse";
|
2017-07-11 11:58:19 +08:00
|
|
|
wcstring_list_t raw_exclusive_flags;
|
2017-07-08 05:32:41 +08:00
|
|
|
wcstring_list_t argv;
|
2018-07-22 08:41:05 +08:00
|
|
|
std::unordered_map<wchar_t, option_spec_ref_t> options;
|
2017-08-20 07:27:24 +08:00
|
|
|
std::unordered_map<wcstring, wchar_t> long_to_short_flag;
|
2017-07-11 11:58:19 +08:00
|
|
|
std::vector<std::vector<wchar_t>> exclusive_flag_sets;
|
2017-07-08 05:32:41 +08:00
|
|
|
};
|
|
|
|
|
2018-07-22 08:58:22 +08:00
|
|
|
static const wchar_t *const short_options = L"+:hn:sx:N:X:";
|
2017-07-13 07:40:53 +08:00
|
|
|
static const struct woption long_options[] = {{L"stop-nonopt", no_argument, NULL, 's'},
|
2017-07-08 05:32:41 +08:00
|
|
|
{L"name", required_argument, NULL, 'n'},
|
2017-07-11 11:58:19 +08:00
|
|
|
{L"exclusive", required_argument, NULL, 'x'},
|
2017-07-20 02:13:59 +08:00
|
|
|
{L"help", no_argument, NULL, 'h'},
|
2017-07-12 08:15:11 +08:00
|
|
|
{L"min-args", required_argument, NULL, 'N'},
|
|
|
|
{L"max-args", required_argument, NULL, 'X'},
|
2017-07-08 05:32:41 +08:00
|
|
|
{NULL, 0, NULL, 0}};
|
|
|
|
|
2017-07-11 11:58:19 +08:00
|
|
|
// Check if any pair of mutually exclusive options was seen. Note that since every option must have
|
|
|
|
// a short name we only need to check those.
|
2018-07-22 08:58:22 +08:00
|
|
|
static int check_for_mutually_exclusive_flags(const argparse_cmd_opts_t &opts, io_streams_t &streams) {
|
2018-07-22 08:41:05 +08:00
|
|
|
for (const auto &kv : opts.options) {
|
|
|
|
const auto &opt_spec = kv.second;
|
2017-07-11 11:58:19 +08:00
|
|
|
if (opt_spec->num_seen == 0) continue;
|
|
|
|
|
|
|
|
// We saw this option at least once. Check all the sets of mutually exclusive options to see
|
|
|
|
// if this option appears in any of them.
|
2018-07-22 08:42:45 +08:00
|
|
|
for (const auto &xarg_set : opts.exclusive_flag_sets) {
|
2017-07-11 11:58:19 +08:00
|
|
|
auto found = std::find(xarg_set.begin(), xarg_set.end(), opt_spec->short_flag);
|
|
|
|
if (found != xarg_set.end()) {
|
|
|
|
// Okay, this option is in a mutually exclusive set of options. Check if any of the
|
|
|
|
// other mutually exclusive options have been seen.
|
2018-07-22 08:42:45 +08:00
|
|
|
for (const auto &xflag : xarg_set) {
|
2018-07-22 08:41:05 +08:00
|
|
|
auto xopt_spec_iter = opts.options.find(xflag);
|
|
|
|
if (xopt_spec_iter == opts.options.end()) continue;
|
|
|
|
|
|
|
|
const auto &xopt_spec = xopt_spec_iter->second;
|
2017-07-11 11:58:19 +08:00
|
|
|
// Ignore this flag in the list of mutually exclusive flags.
|
|
|
|
if (xopt_spec->short_flag == opt_spec->short_flag) continue;
|
|
|
|
|
|
|
|
// If it is a different flag check if it has been seen.
|
|
|
|
if (xopt_spec->num_seen) {
|
|
|
|
wcstring flag1;
|
|
|
|
if (opt_spec->short_flag_valid) flag1 = wcstring(1, opt_spec->short_flag);
|
|
|
|
if (!opt_spec->long_flag.empty()) {
|
|
|
|
if (opt_spec->short_flag_valid) flag1 += L"/";
|
|
|
|
flag1 += opt_spec->long_flag;
|
|
|
|
}
|
|
|
|
wcstring flag2;
|
|
|
|
if (xopt_spec->short_flag_valid) flag2 = wcstring(1, xopt_spec->short_flag);
|
|
|
|
if (!xopt_spec->long_flag.empty()) {
|
|
|
|
if (xopt_spec->short_flag_valid) flag2 += L"/";
|
|
|
|
flag2 += xopt_spec->long_flag;
|
|
|
|
}
|
2017-08-20 11:09:44 +08:00
|
|
|
// We want the flag order to be deterministic. Primarily to make unit
|
|
|
|
// testing easier.
|
|
|
|
if (flag1 > flag2) {
|
|
|
|
wcstring tmp(flag1);
|
2017-08-20 11:22:53 +08:00
|
|
|
flag1 = flag2;
|
|
|
|
flag2 = tmp;
|
2017-08-20 11:09:44 +08:00
|
|
|
}
|
2017-07-11 11:58:19 +08:00
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: Mutually exclusive flags '%ls' and `%ls` seen\n"),
|
|
|
|
opts.name.c_str(), flag1.c_str(), flag2.c_str());
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should be called after all the option specs have been parsed. At that point we have enough
|
|
|
|
// information to parse the values associated with any `--exclusive` flags.
|
|
|
|
static int parse_exclusive_args(argparse_cmd_opts_t &opts, io_streams_t &streams) {
|
2018-07-22 08:25:16 +08:00
|
|
|
for (const wcstring &raw_xflags : opts.raw_exclusive_flags) {
|
|
|
|
const wcstring_list_t xflags = split_string(raw_xflags, L',');
|
2017-07-11 11:58:19 +08:00
|
|
|
if (xflags.size() < 2) {
|
|
|
|
streams.err.append_format(_(L"%ls: exclusive flag string '%ls' is not valid\n"),
|
2017-07-19 13:40:25 +08:00
|
|
|
opts.name.c_str(), raw_xflags.c_str());
|
2017-07-11 11:58:19 +08:00
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<wchar_t> exclusive_set;
|
2018-07-22 08:42:45 +08:00
|
|
|
for (const auto &flag : xflags) {
|
2017-07-11 11:58:19 +08:00
|
|
|
if (flag.size() == 1 && opts.options.find(flag[0]) != opts.options.end()) {
|
|
|
|
// It's a short flag.
|
|
|
|
exclusive_set.push_back(flag[0]);
|
|
|
|
} else {
|
|
|
|
auto x = opts.long_to_short_flag.find(flag);
|
|
|
|
if (x != opts.long_to_short_flag.end()) {
|
|
|
|
// It's a long flag we store as its short flag equivalent.
|
|
|
|
exclusive_set.push_back(x->second);
|
|
|
|
} else {
|
|
|
|
streams.err.append_format(_(L"%ls: exclusive flag '%ls' is not valid\n"),
|
2017-07-19 13:40:25 +08:00
|
|
|
opts.name.c_str(), flag.c_str());
|
2017-07-11 11:58:19 +08:00
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the set of exclusive flags for use when parsing the supplied set of arguments.
|
|
|
|
opts.exclusive_flag_sets.push_back(exclusive_set);
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2018-07-22 08:41:05 +08:00
|
|
|
static bool parse_flag_modifiers(const argparse_cmd_opts_t &opts, const option_spec_ref_t &opt_spec,
|
2017-07-22 06:55:52 +08:00
|
|
|
const wcstring &option_spec, const wchar_t **opt_spec_str,
|
2017-07-08 05:32:41 +08:00
|
|
|
io_streams_t &streams) {
|
2017-07-22 06:55:52 +08:00
|
|
|
const wchar_t *s = *opt_spec_str;
|
2017-07-17 12:10:22 +08:00
|
|
|
if (opt_spec->short_flag == opts.implicit_int_flag && *s && *s != L'!') {
|
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: Implicit int short flag '%lc' does not allow modifiers like '%lc'\n"),
|
|
|
|
opts.name.c_str(), opt_spec->short_flag, *s);
|
2017-07-14 11:36:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-13 13:12:41 +08:00
|
|
|
if (*s == L'=') {
|
2017-07-08 05:32:41 +08:00
|
|
|
s++;
|
2017-07-13 13:12:41 +08:00
|
|
|
if (*s == L'?') {
|
2017-07-08 05:32:41 +08:00
|
|
|
opt_spec->num_allowed = -1; // optional arg
|
|
|
|
s++;
|
2017-07-13 13:12:41 +08:00
|
|
|
} else if (*s == L'+') {
|
2017-07-13 07:40:53 +08:00
|
|
|
opt_spec->num_allowed = 2; // mandatory arg and can appear more than once
|
|
|
|
s++;
|
2017-07-08 05:32:41 +08:00
|
|
|
} else {
|
|
|
|
opt_spec->num_allowed = 1; // mandatory arg and can appear only once
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:10:22 +08:00
|
|
|
if (*s == L'!') {
|
|
|
|
s++;
|
|
|
|
opt_spec->validation_command = wcstring(s);
|
|
|
|
} else if (*s) {
|
2017-07-08 05:32:41 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_INVALID_OPT_SPEC, opts.name.c_str(),
|
|
|
|
option_spec.c_str(), *s);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:10:22 +08:00
|
|
|
// Make sure we have some validation for implicit int flags.
|
|
|
|
if (opt_spec->short_flag == opts.implicit_int_flag && opt_spec->validation_command.empty()) {
|
|
|
|
opt_spec->validation_command = L"_validate_int";
|
|
|
|
}
|
|
|
|
|
2017-07-17 09:27:41 +08:00
|
|
|
if (opts.options.find(opt_spec->short_flag) != opts.options.end()) {
|
|
|
|
streams.err.append_format(L"%ls: Short flag '%lc' already defined\n", opts.name.c_str(),
|
|
|
|
opt_spec->short_flag);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-22 06:55:52 +08:00
|
|
|
*opt_spec_str = s;
|
2017-07-08 05:32:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-19 11:22:09 +08:00
|
|
|
/// Parse the text following the short flag letter.
|
2018-07-22 08:41:05 +08:00
|
|
|
static bool parse_option_spec_sep(argparse_cmd_opts_t &opts, const option_spec_ref_t &opt_spec,
|
|
|
|
const wcstring &option_spec, const wchar_t **opt_spec_str,
|
2017-07-19 11:22:09 +08:00
|
|
|
io_streams_t &streams) {
|
|
|
|
const wchar_t *s = *opt_spec_str;
|
2017-07-14 11:36:59 +08:00
|
|
|
if (*(s - 1) == L'#') {
|
|
|
|
if (*s != L'-') {
|
|
|
|
streams.err.append_format(
|
2017-07-19 13:40:25 +08:00
|
|
|
_(L"%ls: Short flag '#' must be followed by '-' and a long name\n"),
|
|
|
|
opts.name.c_str());
|
2017-07-14 11:36:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-17 09:27:41 +08:00
|
|
|
if (opts.implicit_int_flag) {
|
2017-07-17 12:10:22 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Implicit int flag '%lc' already defined\n"),
|
|
|
|
opts.name.c_str(), opts.implicit_int_flag);
|
2017-07-17 09:27:41 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
opts.implicit_int_flag = opt_spec->short_flag;
|
2017-07-14 11:36:59 +08:00
|
|
|
opt_spec->short_flag_valid = false;
|
|
|
|
s++;
|
2017-07-13 13:12:41 +08:00
|
|
|
} else if (*s == L'-') {
|
2017-07-08 05:32:41 +08:00
|
|
|
opt_spec->short_flag_valid = false;
|
|
|
|
s++;
|
2017-07-22 06:55:52 +08:00
|
|
|
if (!*s) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_INVALID_OPT_SPEC, opts.name.c_str(),
|
|
|
|
option_spec.c_str(), *(s - 1));
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-14 11:36:59 +08:00
|
|
|
} else if (*s == L'/') {
|
|
|
|
s++; // the struct is initialized assuming short_flag_valid should be true
|
2017-07-22 06:55:52 +08:00
|
|
|
if (!*s) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_INVALID_OPT_SPEC, opts.name.c_str(),
|
|
|
|
option_spec.c_str(), *(s - 1));
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-17 09:27:41 +08:00
|
|
|
} else if (*s == L'#') {
|
|
|
|
if (opts.implicit_int_flag) {
|
2017-07-17 12:10:22 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Implicit int flag '%lc' already defined\n"),
|
|
|
|
opts.name.c_str(), opts.implicit_int_flag);
|
2017-07-17 09:27:41 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
opts.implicit_int_flag = opt_spec->short_flag;
|
|
|
|
opt_spec->num_allowed = 1; // mandatory arg and can appear only once
|
|
|
|
s++; // the struct is initialized assuming short_flag_valid should be true
|
2017-07-08 05:32:41 +08:00
|
|
|
} else {
|
2017-07-22 06:55:52 +08:00
|
|
|
// Long flag name not allowed if second char isn't '/', '-' or '#' so just check for
|
2017-07-08 05:32:41 +08:00
|
|
|
// behavior modifier chars.
|
2017-07-22 06:55:52 +08:00
|
|
|
if (!parse_flag_modifiers(opts, opt_spec, option_spec, &s, streams)) return false;
|
2017-07-08 05:32:41 +08:00
|
|
|
}
|
|
|
|
|
2017-07-19 11:22:09 +08:00
|
|
|
*opt_spec_str = s;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This parses an option spec string into a struct option_spec.
|
2017-07-22 06:55:52 +08:00
|
|
|
static bool parse_option_spec(argparse_cmd_opts_t &opts, //!OCLINT(high npath complexity)
|
2018-07-22 08:41:05 +08:00
|
|
|
const wcstring &option_spec, io_streams_t &streams) {
|
2017-07-19 11:22:09 +08:00
|
|
|
if (option_spec.empty()) {
|
|
|
|
streams.err.append_format(_(L"%ls: An option spec must have a short flag letter\n"),
|
|
|
|
opts.name.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *s = option_spec.c_str();
|
|
|
|
if (!iswalnum(*s) && *s != L'#') {
|
|
|
|
streams.err.append_format(_(L"%ls: Short flag '%lc' invalid, must be alphanum or '#'\n"),
|
|
|
|
opts.name.c_str(), *s);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-22 08:41:05 +08:00
|
|
|
std::unique_ptr<option_spec_t> opt_spec(new option_spec_t{*s++});
|
|
|
|
|
|
|
|
// Try parsing stuff after the short flag.
|
|
|
|
if (*s && !parse_option_spec_sep(opts, opt_spec, option_spec, &s, streams)) {
|
|
|
|
return false;
|
2017-07-22 06:55:52 +08:00
|
|
|
}
|
2018-07-22 08:41:05 +08:00
|
|
|
|
|
|
|
// Collect any long flag name.
|
|
|
|
if (*s) {
|
|
|
|
const wchar_t *const long_flag_start = s;
|
|
|
|
while (*s && (*s == L'-' || *s == L'_' || iswalnum(*s))) s++;
|
|
|
|
if (s != long_flag_start) {
|
|
|
|
opt_spec->long_flag = wcstring(long_flag_start, s);
|
|
|
|
if (opts.long_to_short_flag.count(opt_spec->long_flag) > 0) {
|
|
|
|
streams.err.append_format(L"%ls: Long flag '%ls' already defined\n",
|
|
|
|
opts.name.c_str(), opt_spec->long_flag.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-22 06:55:52 +08:00
|
|
|
}
|
2018-07-22 08:41:05 +08:00
|
|
|
}
|
|
|
|
if (!parse_flag_modifiers(opts, opt_spec, option_spec, &s, streams)) {
|
|
|
|
return false;
|
2017-07-17 09:27:41 +08:00
|
|
|
}
|
2017-07-13 13:12:41 +08:00
|
|
|
|
2018-07-22 08:41:05 +08:00
|
|
|
// Record our long flag if we have one.
|
|
|
|
if (!opt_spec->long_flag.empty()) {
|
|
|
|
auto ins = opts.long_to_short_flag.emplace(opt_spec->long_flag, opt_spec->short_flag);
|
|
|
|
assert(ins.second && "Should have inserted long flag");
|
|
|
|
(void)ins;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record our option under its short flag.
|
|
|
|
opts.options[opt_spec->short_flag] = std::move(opt_spec);
|
|
|
|
return true;
|
2017-07-08 05:32:41 +08:00
|
|
|
}
|
|
|
|
|
2017-07-11 11:58:19 +08:00
|
|
|
static int collect_option_specs(argparse_cmd_opts_t &opts, int *optind, int argc, wchar_t **argv,
|
|
|
|
io_streams_t &streams) {
|
|
|
|
wchar_t *cmd = argv[0];
|
2017-07-13 07:40:53 +08:00
|
|
|
|
2017-07-13 13:12:41 +08:00
|
|
|
while (true) {
|
2017-07-11 11:58:19 +08:00
|
|
|
if (wcscmp(L"--", argv[*optind]) == 0) {
|
|
|
|
++*optind;
|
2017-07-13 07:40:53 +08:00
|
|
|
break;
|
2017-07-11 11:58:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!parse_option_spec(opts, argv[*optind], streams)) {
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-07-13 13:12:41 +08:00
|
|
|
if (++*optind == argc) {
|
|
|
|
streams.err.append_format(_(L"%ls: Missing -- separator\n"), cmd);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-07-11 11:58:19 +08:00
|
|
|
}
|
2017-07-13 07:40:53 +08:00
|
|
|
|
|
|
|
if (opts.options.empty()) {
|
|
|
|
streams.err.append_format(_(L"%ls: No option specs were provided\n"), cmd);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_CMD_OK;
|
2017-07-11 11:58:19 +08:00
|
|
|
}
|
|
|
|
|
2017-07-08 05:32:41 +08:00
|
|
|
static int parse_cmd_opts(argparse_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method)
|
|
|
|
int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) {
|
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'n': {
|
|
|
|
opts.name = w.woptarg;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-13 07:40:53 +08:00
|
|
|
case 's': {
|
|
|
|
opts.stop_nonopt = true;
|
2017-07-08 05:32:41 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-07-11 11:58:19 +08:00
|
|
|
case 'x': {
|
|
|
|
// Just save the raw string here. Later, when we have all the short and long flag
|
|
|
|
// definitions we'll parse these strings into a more useful data structure.
|
|
|
|
opts.raw_exclusive_flags.push_back(w.woptarg);
|
|
|
|
break;
|
|
|
|
}
|
2017-07-20 02:13:59 +08:00
|
|
|
case 'h': {
|
|
|
|
opts.print_help = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-12 08:15:11 +08:00
|
|
|
case 'N': {
|
|
|
|
long x = fish_wcstol(w.woptarg);
|
|
|
|
if (errno || x < 0) {
|
|
|
|
streams.err.append_format(_(L"%ls: Invalid --min-args value '%ls'\n"), cmd,
|
|
|
|
w.woptarg);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
opts.min_args = x;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'X': {
|
|
|
|
long x = fish_wcstol(w.woptarg);
|
|
|
|
if (errno || x < 0) {
|
|
|
|
streams.err.append_format(_(L"%ls: Invalid --max-args value '%ls'\n"), cmd,
|
|
|
|
w.woptarg);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
opts.max_args = x;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-08 05:32:41 +08:00
|
|
|
case ':': {
|
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
DIE("unexpected retval from wgetopt_long");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:44:53 +08:00
|
|
|
if (opts.print_help) return STATUS_CMD_OK;
|
|
|
|
|
2017-07-13 13:12:41 +08:00
|
|
|
if (argc == w.woptind || wcscmp(L"--", argv[w.woptind - 1]) == 0) {
|
|
|
|
// The user didn't specify any option specs.
|
|
|
|
streams.err.append_format(_(L"%ls: No option specs were provided\n"), cmd);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
2017-07-08 05:32:41 +08:00
|
|
|
*optind = w.woptind;
|
2017-07-11 11:58:19 +08:00
|
|
|
return collect_option_specs(opts, optind, argc, argv, streams);
|
2017-07-08 05:32:41 +08:00
|
|
|
}
|
|
|
|
|
2018-07-22 08:58:22 +08:00
|
|
|
static void populate_option_strings(const argparse_cmd_opts_t &opts, wcstring *short_options,
|
|
|
|
std::vector<woption> *long_options) {
|
2018-07-22 08:41:05 +08:00
|
|
|
for (const auto &kv : opts.options) {
|
|
|
|
const auto &opt_spec = kv.second;
|
2018-07-22 08:58:22 +08:00
|
|
|
if (opt_spec->short_flag_valid) short_options->push_back(opt_spec->short_flag);
|
2017-07-08 05:32:41 +08:00
|
|
|
|
|
|
|
int arg_type = no_argument;
|
|
|
|
if (opt_spec->num_allowed == -1) {
|
|
|
|
arg_type = optional_argument;
|
2018-07-22 08:58:22 +08:00
|
|
|
if (opt_spec->short_flag_valid) short_options->append(L"::");
|
2017-07-08 05:32:41 +08:00
|
|
|
} else if (opt_spec->num_allowed > 0) {
|
|
|
|
arg_type = required_argument;
|
2018-07-22 08:58:22 +08:00
|
|
|
if (opt_spec->short_flag_valid) short_options->append(L":");
|
2017-07-08 05:32:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!opt_spec->long_flag.empty()) {
|
2018-07-22 08:58:22 +08:00
|
|
|
long_options->push_back(
|
|
|
|
{opt_spec->long_flag.c_str(), arg_type, NULL, opt_spec->short_flag});
|
2017-07-08 05:32:41 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-22 08:58:22 +08:00
|
|
|
long_options->push_back({NULL, 0, NULL, 0});
|
2017-07-08 05:32:41 +08:00
|
|
|
}
|
|
|
|
|
2018-07-22 08:58:22 +08:00
|
|
|
static int validate_arg(const argparse_cmd_opts_t &opts, option_spec_t *opt_spec, bool is_long_flag,
|
2017-07-17 12:10:22 +08:00
|
|
|
const wchar_t *woptarg, io_streams_t &streams) {
|
|
|
|
// Obviously if there is no arg validation command we assume the arg is okay.
|
|
|
|
if (opt_spec->validation_command.empty()) return STATUS_CMD_OK;
|
|
|
|
|
|
|
|
wcstring_list_t cmd_output;
|
|
|
|
|
|
|
|
env_push(true);
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set_one(L"_argparse_cmd", ENV_LOCAL, opts.name);
|
2017-07-17 12:10:22 +08:00
|
|
|
if (is_long_flag) {
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set_one(var_name_prefix + L"name", ENV_LOCAL, opt_spec->long_flag);
|
2017-07-17 12:10:22 +08:00
|
|
|
} else {
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set_one(var_name_prefix + L"name", ENV_LOCAL,
|
|
|
|
wcstring(1, opt_spec->short_flag).c_str());
|
2017-07-17 12:10:22 +08:00
|
|
|
}
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set_one(var_name_prefix + L"value", ENV_LOCAL, woptarg);
|
2017-07-17 12:10:22 +08:00
|
|
|
|
|
|
|
int retval = exec_subshell(opt_spec->validation_command, cmd_output, false);
|
2018-07-22 08:42:45 +08:00
|
|
|
for (const auto &output : cmd_output) {
|
|
|
|
streams.err.append(output);
|
2017-07-17 12:10:22 +08:00
|
|
|
streams.err.push_back(L'\n');
|
|
|
|
}
|
|
|
|
env_pop();
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if it is an implicit integer option. Try to parse and validate it as a valid integer. The
|
|
|
|
// code that parses option specs has already ensured that implicit integers have either an explicit
|
|
|
|
// or implicit validation function.
|
|
|
|
static int check_for_implicit_int(argparse_cmd_opts_t &opts, const wchar_t *val, const wchar_t *cmd,
|
|
|
|
const wchar_t *const *argv, wgetopter_t &w, int opt, int long_idx,
|
|
|
|
parser_t &parser, io_streams_t &streams) {
|
|
|
|
if (opts.implicit_int_flag == L'\0') {
|
|
|
|
// There is no implicit integer option so report an error.
|
|
|
|
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt == '?') {
|
|
|
|
// It's a flag that might be an implicit integer flag. Try to parse it as an integer to
|
|
|
|
// decide if it is in fact something like `-NNN` or an invalid flag.
|
|
|
|
(void)fish_wcstol(val);
|
|
|
|
if (errno) {
|
|
|
|
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, it looks like an integer. See if it passes the validation checks.
|
|
|
|
auto found = opts.options.find(opts.implicit_int_flag);
|
|
|
|
assert(found != opts.options.end());
|
2018-07-22 08:41:05 +08:00
|
|
|
const auto &opt_spec = found->second;
|
|
|
|
int retval = validate_arg(opts, opt_spec.get(), long_idx != -1, val, streams);
|
2017-07-17 12:10:22 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
// It's a valid integer so store it and return success.
|
|
|
|
opt_spec->vals.clear();
|
|
|
|
opt_spec->vals.push_back(wcstring(val));
|
|
|
|
opt_spec->num_seen++;
|
|
|
|
w.nextchar = NULL;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-22 06:55:52 +08:00
|
|
|
static int handle_flag(argparse_cmd_opts_t &opts, option_spec_t *opt_spec, int long_idx,
|
|
|
|
const wchar_t *woptarg, io_streams_t &streams) {
|
|
|
|
opt_spec->num_seen++;
|
|
|
|
if (opt_spec->num_allowed == 0) {
|
|
|
|
// It's a boolean flag. Save the flag we saw since it might be useful to know if the
|
|
|
|
// short or long flag was given.
|
|
|
|
assert(!woptarg);
|
|
|
|
if (long_idx == -1) {
|
|
|
|
opt_spec->vals.push_back(wcstring(1, L'-') + opt_spec->short_flag);
|
|
|
|
} else {
|
|
|
|
opt_spec->vals.push_back(L"--" + opt_spec->long_flag);
|
|
|
|
}
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (woptarg) {
|
|
|
|
int retval = validate_arg(opts, opt_spec, long_idx != -1, woptarg, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt_spec->num_allowed == -1 || opt_spec->num_allowed == 1) {
|
|
|
|
// We're depending on `wgetopt_long()` to report that a mandatory value is missing if
|
|
|
|
// `opt_spec->num_allowed == 1` and thus return ':' so that we don't take this branch if
|
|
|
|
// the mandatory arg is missing.
|
|
|
|
opt_spec->vals.clear();
|
|
|
|
if (woptarg) {
|
|
|
|
opt_spec->vals.push_back(woptarg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(woptarg);
|
|
|
|
opt_spec->vals.push_back(woptarg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-11 11:58:19 +08:00
|
|
|
static int argparse_parse_flags(argparse_cmd_opts_t &opts, const wchar_t *short_options,
|
|
|
|
const woption *long_options, const wchar_t *cmd, int argc,
|
|
|
|
wchar_t **argv, int *optind, parser_t &parser,
|
|
|
|
io_streams_t &streams) {
|
|
|
|
int opt;
|
2017-07-17 12:10:22 +08:00
|
|
|
int long_idx = -1;
|
2017-07-11 11:58:19 +08:00
|
|
|
wgetopter_t w;
|
2017-07-17 12:10:22 +08:00
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, &long_idx)) != -1) {
|
2017-07-11 11:58:19 +08:00
|
|
|
if (opt == ':') {
|
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-07-17 12:10:22 +08:00
|
|
|
|
2017-07-11 11:58:19 +08:00
|
|
|
if (opt == '?') {
|
2017-07-17 12:10:22 +08:00
|
|
|
// It's not a recognized flag. See if it's an implicit int flag.
|
|
|
|
int retval = check_for_implicit_int(opts, argv[w.woptind - 1] + 1, cmd, argv, w, opt,
|
|
|
|
long_idx, parser, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
long_idx = -1;
|
|
|
|
continue;
|
2017-07-11 11:58:19 +08:00
|
|
|
}
|
|
|
|
|
2017-07-17 12:10:22 +08:00
|
|
|
// It's a recognized flag.
|
2017-07-11 11:58:19 +08:00
|
|
|
auto found = opts.options.find(opt);
|
|
|
|
assert(found != opts.options.end());
|
|
|
|
|
2018-07-22 08:41:05 +08:00
|
|
|
int retval = handle_flag(opts, found->second.get(), long_idx, w.woptarg, streams);
|
2017-07-22 06:55:52 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
2017-07-17 12:10:22 +08:00
|
|
|
long_idx = -1;
|
2017-07-11 11:58:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*optind = w.woptind;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-08 05:32:41 +08:00
|
|
|
// This function mimics the `wgetopt_long()` usage found elsewhere in our other builtin commands.
|
|
|
|
// It's different in that the short and long option structures are constructed dynamically based on
|
|
|
|
// arguments provided to the `argparse` command.
|
|
|
|
static int argparse_parse_args(argparse_cmd_opts_t &opts, const wcstring_list_t &args,
|
2017-07-11 11:58:19 +08:00
|
|
|
parser_t &parser, io_streams_t &streams) {
|
2017-07-08 05:32:41 +08:00
|
|
|
if (args.empty()) return STATUS_CMD_OK;
|
|
|
|
|
2017-07-13 07:40:53 +08:00
|
|
|
wcstring short_options = opts.stop_nonopt ? L"+:" : L":";
|
2018-07-22 08:58:22 +08:00
|
|
|
std::vector<woption> long_options;
|
|
|
|
populate_option_strings(opts, &short_options, &long_options);
|
|
|
|
|
|
|
|
// long_options should have a "null terminator"
|
|
|
|
assert(!long_options.empty() && long_options.back().name == nullptr);
|
2017-07-08 05:32:41 +08:00
|
|
|
|
|
|
|
const wchar_t *cmd = opts.name.c_str();
|
2017-07-11 11:58:19 +08:00
|
|
|
int argc = static_cast<int>(args.size());
|
2017-07-08 05:32:41 +08:00
|
|
|
|
2017-07-13 13:12:41 +08:00
|
|
|
// We need to convert our wcstring_list_t to a <wchar_t **> that can be used by wgetopt_long().
|
|
|
|
// This ensures the memory for the data structure is freed when we leave this scope.
|
2017-07-08 05:32:41 +08:00
|
|
|
null_terminated_array_t<wchar_t> argv_container(args);
|
2018-08-05 07:15:06 +08:00
|
|
|
auto argv = argv_container.get();
|
2017-07-08 05:32:41 +08:00
|
|
|
|
2017-07-11 11:58:19 +08:00
|
|
|
int optind;
|
2018-07-22 08:58:22 +08:00
|
|
|
int retval = argparse_parse_flags(opts, short_options.c_str(), long_options.data(), cmd, argc,
|
2017-07-11 11:58:19 +08:00
|
|
|
argv, &optind, parser, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
2017-07-08 05:32:41 +08:00
|
|
|
|
2017-07-11 11:58:19 +08:00
|
|
|
retval = check_for_mutually_exclusive_flags(opts, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
2017-07-08 05:32:41 +08:00
|
|
|
|
2017-07-11 11:58:19 +08:00
|
|
|
for (int i = optind; argv[i]; i++) opts.argv.push_back(argv[i]);
|
2017-07-13 13:12:41 +08:00
|
|
|
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-14 11:36:59 +08:00
|
|
|
static int check_min_max_args_constraints(argparse_cmd_opts_t &opts, parser_t &parser,
|
|
|
|
io_streams_t &streams) {
|
2017-07-13 13:12:41 +08:00
|
|
|
UNUSED(parser);
|
|
|
|
const wchar_t *cmd = opts.name.c_str();
|
|
|
|
|
|
|
|
if (opts.argv.size() < opts.min_args) {
|
2017-07-12 08:15:11 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_MIN_ARG_COUNT1, cmd, opts.min_args, opts.argv.size());
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
2017-07-12 11:25:08 +08:00
|
|
|
if (opts.max_args != SIZE_MAX && opts.argv.size() > opts.max_args) {
|
2017-07-12 08:15:11 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_MAX_ARG_COUNT1, cmd, opts.max_args, opts.argv.size());
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
2017-07-12 08:53:43 +08:00
|
|
|
|
2017-07-08 05:32:41 +08:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-14 11:36:59 +08:00
|
|
|
/// Put the result of parsing the supplied args into the caller environment as local vars.
|
|
|
|
static void set_argparse_result_vars(argparse_cmd_opts_t &opts) {
|
2018-07-22 08:41:05 +08:00
|
|
|
for (const auto &kv : opts.options) {
|
|
|
|
const auto &opt_spec = kv.second;
|
2017-07-14 11:36:59 +08:00
|
|
|
if (!opt_spec->num_seen) continue;
|
|
|
|
|
|
|
|
if (opt_spec->short_flag_valid) {
|
2017-08-28 17:51:34 +08:00
|
|
|
env_set(var_name_prefix + opt_spec->short_flag, ENV_LOCAL, opt_spec->vals);
|
2017-07-14 11:36:59 +08:00
|
|
|
}
|
|
|
|
if (!opt_spec->long_flag.empty()) {
|
|
|
|
// We do a simple replacement of all non alphanum chars rather than calling
|
|
|
|
// escape_string(long_flag, 0, STRING_STYLE_VAR).
|
|
|
|
wcstring long_flag = opt_spec->long_flag;
|
|
|
|
for (size_t pos = 0; pos < long_flag.size(); pos++) {
|
|
|
|
if (!iswalnum(long_flag[pos])) long_flag[pos] = L'_';
|
|
|
|
}
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set(var_name_prefix + long_flag, ENV_LOCAL, opt_spec->vals);
|
2017-07-14 11:36:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set(L"argv", ENV_LOCAL, opts.argv);
|
2017-07-14 11:36:59 +08:00
|
|
|
}
|
|
|
|
|
2017-07-08 05:32:41 +08:00
|
|
|
/// The argparse builtin. This is explicitly not compatible with the BSD or GNU version of this
|
|
|
|
/// command. That's because fish doesn't have the weird quoting problems of POSIX shells. So we
|
|
|
|
/// don't need to support flags like `--unquoted`. Similarly we don't want to support introducing
|
|
|
|
/// long options with a single dash so we don't support the `--alternative` flag. That `getopt` is
|
|
|
|
/// an external command also means its output has to be in a form that can be eval'd. Because our
|
|
|
|
/// version is a builtin it can directly set variables local to the current scope (e.g., a
|
|
|
|
/// function). It doesn't need to write anything to stdout that then needs to be eval'd.
|
|
|
|
int builtin_argparse(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|
|
|
const wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
argparse_cmd_opts_t opts;
|
|
|
|
|
|
|
|
int optind;
|
|
|
|
int retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
2017-07-20 02:13:59 +08:00
|
|
|
if (opts.print_help) {
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-08 05:32:41 +08:00
|
|
|
wcstring_list_t args;
|
|
|
|
args.push_back(opts.name);
|
|
|
|
while (optind < argc) args.push_back(argv[optind++]);
|
|
|
|
|
2017-07-11 11:58:19 +08:00
|
|
|
retval = parse_exclusive_args(opts, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
2017-07-08 05:32:41 +08:00
|
|
|
retval = argparse_parse_args(opts, args, parser, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
2017-07-14 11:36:59 +08:00
|
|
|
retval = check_min_max_args_constraints(opts, parser, streams);
|
2017-07-13 13:12:41 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
2017-07-14 11:36:59 +08:00
|
|
|
set_argparse_result_vars(opts);
|
2017-07-08 05:32:41 +08:00
|
|
|
return retval;
|
|
|
|
}
|