slap icon indicating copy to clipboard operation
slap copied to clipboard

Document how to allow arguments to start with hyphens

Open ngirard opened this issue 3 years ago • 3 comments

Using the following test script:

#!/bin/bash

# shellcheck disable=SC2154

slap deps bat confirm rg sd|| exit 1

eval "$(slap parse bash _ -- "$@" <<-EOF
name: test
version: "0.1"
about: Using an argument that contains '--'

settings:
    - ArgRequiredElseHelp
    - ColorAuto

global_settings:
    - ColoredHelp

args:
    - arg:
        help: The argument
        required: true
EOF
)"; [[ -z "${_success}" ]] && exit 1

printf -- "arg:%s\n" "${_arg_vals}"

Executing ./test value outputs var:value, but executing `./test '--value' leads to

error: Found argument '--value' which wasn't expected, or isn't valid in this context

In my scenario I need to be able to have arguments that start with --.

Is there a workaround ?

ngirard avatar Nov 06 '20 22:11 ngirard

The solution consists in

  • adding allow_hyphen_values: true to the argument; and
  • prepending -- to the argument list in the invocation: ./test -- '--value'

I tried both separately before but not jointly.

I'm leaving this issue open because I think it would be nice to provide an example that illustrate this.

I think Slap is underrated and deserves a much wider audience, but much of them might not find it straightforward to use Clap's documentation. Adding more examples would definitely make a difference.

ngirard avatar Nov 06 '20 23:11 ngirard

I think it would be cool also to have some editor intellisense support, so you can see available options and what they do. See #10
This is doable because in clap's master there is a JSON schema we can use: https://github.com/clap-rs/clap/blob/master/clap.schema.json

guardam avatar Nov 08 '20 16:11 guardam

As for the example, as soon as I have time I'll add one.

guardam avatar Nov 08 '20 16:11 guardam