Skip to content

enhance argparse with a way to validate flag values #4211

Closed

Description

@krader1961

Most commands that have flags which take a value also need to validate that the value is reasonable. For example, a common check is that the value is an integer greater than zero. Or that the value is a string taken from a finite set of valid values. Or that it matches a particular pattern. We should augment argparse with a mechanism for having it perform such checks.

We should allow running arbitrary fish script in the same way that complete --condition='fish_code...' does it. Obviously this also supports a simple function name with optional arguments if the user prefers that style. The statement would be run with its local scope populated with three variables:

o _argparse_cmd would be set to the argparse --name value or argparse if a command name wasn't given (for inclusion in error messages),
o _flag_name would be set to the flag seen (e.g., max-args if --max-args was processed or m if -m was processed),
o _flag_value set to the value associated with the flag.

The fish script should write any validation error message to stderr and return a non-zero status if the value failed validation. Else return a status of zero if the value is acceptable.

The fish statement to run would be separated from the rest of the option spec by an exclamation-mark. For example, m/max=!_validate_int --min=1 would run the function _validate_int with its argv set to 1 and the aforementioned vars defined. In this example the hypothetical _validate_int function accepts two optional flags that cause it to also check that the value is in an acceptable range as well as being a properly formatted integer.

Activity

  1. locked as resolved and limited conversation to collaborators on Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions