Search code examples
parsingfish

Fish function option/param parser


It seems that there is some work in progress to add support for this in the future:

https://github.com/fish-shell/fish-shell/issues/478
https://github.com/xiaq/fish-shell/tree/opt-parse

But in the meantime, what's the recommended way to deal with this? Should I just parse $argv? If so do you have some tips/best practices?


Solution

  • I'm note sure if it is a best practice, but in the meantime you could do something like this:

    function options
      echo $argv | sed 's|--*|\\'\n'|g' | grep -v '^$'
    end
    
    function function_with_options
      for i in (options $argv)
        echo $i | read -l option value
    
        switch $option
          case a all
            echo all the things
          case f force
            echo force it
          case i ignore
            echo ignore the $value
        end
      end
    end
    

    Output:

    ➤ function_with_options -i thing -a --force
    ignore the thing
    all the things
    force it