Skip to content

Idea: argument validation system #125

@wolfgang42

Description

@wolfgang42

I was thinking last night about the argument validation we've been discussing in #122. I had the idea of an allow_empty_string config setting, but then I thought—why should the empty string be special? There are a lot of different validations that might be important for an argument, such as whether the file exists or if the value is an integer.

To that end, here's a straw-man proposal for a validations system:

  • Flags and arguments have a new config key, validate, which takes an array of validations to check the value for. (Optional: If there is only one validation, it can be passed as a bare string.)
  • Validations are defined by a function named validate_${validation_name} which takes one argument, the value to be validated, and echos a string if the value is not valid.
  • Bashly has some built-in validations, but additional ones can be defined in lib/ by individual programs.

Here are some example validations which people might write:

validate_not_empty() {
  [[ -z "$1" ]] && echo "must not be empty"
}
validate_file_exists() {
  [[ -e "$1" ]] && echo "must be an existing file"
}
validate_commit_exists() {
  git rev-parse --quiet --verify "$1^{commit}" || echo "must be a valid git commit"
}
validate_is_integer() {
  [[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer"
}

And an example config:

commands:
- name: upload
  args:
  - name: source
    required: true
    validate: file_exists
  flags:
  - long: --scratch-file
    validate: [file_readable, file_writable]

WDYT?

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions