Search code examples
shellparsingargumentsfish

Named argument for fish function with argparse


The fish function builtin has the option -a/--argument-names which allows binding an argument to a name.

Is it still possible to do this while also using the argparse builtin? If so, how?


Solution

  • I designed and implemented argparse. Yes, technically you can use the --argument flag and argparse in the same function but doing so makes no sense. The --argument option operates completely independently of argparse and has no knowledge of flags. Create the following function:

    function x -a arg1 -a arg2
        set --show arg1 arg2 argv
    end
    

    Now invoke it thusly: x --flag val arg. Notice that --flag is bound to arg1 and val is bound to arg2 and argv still contains all of the arguments passed to the function. If your function has flags you shouldn't use --argument. If it doesn't then there isn't any point in using argparse.