-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Perhaps we can add an argument parsing pass that happens before variable expansion to generalize the trick of special-casing count --help. The idea is simple: When a builtin or function is called, options are parsed before variable expansions and striped from $argv. This makes count --help no longer a special case, since when --help is passed literally, it doesn't appear in $argv at all.
To generalize this to functions, make option specification part of the function definition. E.g. to write a function that takes a -h option (short for --help), and a -c (short for --count) that takes an argument:
function foo -o h,help,'Display help' -o c:,count:,'Specify a count'
if test $opt_help = 1
...
end
if test (count $opt_count) = 1
...
end
endAfter -o is short,long,description. If short and long both ends with :, an argument is required after the option. The description can be used to provide tab-completion.
Inside the function, opt_help is either 0 or 1, while opt_count is either a 0-element list or 1-element list, depending on whether the caller has specified the option.
Additional ideas:
-
Default value can be specified like:
function foo -o c,count:1 end
which guarantees that $opt_count is always a 1-element list - when the caller didn't specify, it defaults to
1.