Search code examples
fish

how to use optional flag in fish


I'm building a CLI tasks utility, (A cheap version of taskwarrior). I want to add some optional flags, such as -n

else if [ $cmd = 'delete' ]
argparse 'n/index'=? -- $argv
sed -i "$_flag_index d" ~/.tasks/data/Tdo.csv

but this gives an error

~/.tasks/run.sh (line 14): No matches for wildcard “'n/index'=?”. See help expand. argparse 'n/index'=? -- $argv

I'm unable to understand the correct usage of optional flags, and I've not been able to find enough resources, the fish shell documentation isn't sufficient for a novice in shell scripting given lack of examples.

How to accept an optional argument n/index, and further execute some code if the argument has been given, and something else otherwise, and is it possible to add integer constraints on optional arguments?


Solution

  • Did you help expand like fish told you?

    The unquoted ? is being handled as a globbing character. Use 'n/index=?'

    $ set argv --index=10
    
    $ argparse 'n/index'=? -- $argv
    fish: No matches for wildcard ''n/index'=?'. See `help expand`.
    argparse --ignore-unknown 'n/index'=? -- $argv
                              ^
    
    # quote the whole thing
    #          v         v
    $ argparse 'n/index=?' -- $argv
    
    $ set -S _flag_index
    $_flag_index: set in local scope, unexported, with 1 elements
    $_flag_index[1]: |10|