argparse: Pass validation variables as exported

This was written before local-exported variables did anything useful.

Passing these vars as local-exports removes the need to define the
validation function with `--no-scope-shadowing` which is quite the
hack.
This commit is contained in:
Fabian Homborg 2020-03-01 19:28:51 +01:00
parent 0f34459fce
commit bfc1de9ef4
2 changed files with 5 additions and 5 deletions

View file

@ -114,7 +114,7 @@ Sometimes you need to validate the option values. For example, that it is a vali
- ``_flag_value`` will be set to the value associated with the flag being processed.
If you do this via a function it should be defined with the ``--no-scope-shadowing`` flag. Otherwise it won't have access to those variables.
These variables are passed to the function as local exported variables.
The script should write any error messages to stdout, not stderr. It should return a status of zero if the flag value is valid otherwise a non-zero status to indicate it is invalid.

View file

@ -457,13 +457,13 @@ static int validate_arg(parser_t &parser, const argparse_cmd_opts_t &opts, optio
auto &vars = parser.vars();
vars.push(true);
vars.set_one(L"_argparse_cmd", ENV_LOCAL, opts.name);
vars.set_one(L"_argparse_cmd", ENV_LOCAL | ENV_EXPORT, opts.name);
if (is_long_flag) {
vars.set_one(var_name_prefix + L"name", ENV_LOCAL, opt_spec->long_flag);
vars.set_one(var_name_prefix + L"name", ENV_LOCAL | ENV_EXPORT, opt_spec->long_flag);
} else {
vars.set_one(var_name_prefix + L"name", ENV_LOCAL, wcstring(1, opt_spec->short_flag));
vars.set_one(var_name_prefix + L"name", ENV_LOCAL | ENV_EXPORT, wcstring(1, opt_spec->short_flag));
}
vars.set_one(var_name_prefix + L"value", ENV_LOCAL, woptarg);
vars.set_one(var_name_prefix + L"value", ENV_LOCAL | ENV_EXPORT, woptarg);
int retval = exec_subshell(opt_spec->validation_command, parser, cmd_output, false);
for (const auto &output : cmd_output) {