Call with no args
$ optparsing_demo
--verbose: false
--filename: myfile
positional:Call with both short and long options, as well as positional args
$ optparsing_demo --verbose -f test.txt foo
--verbose: true
--filename: test.txt
positional: fooCall with -- to pass positionals that look like flags
$ optparsing_demo --filename=test.txt -- -v --verbose -f --filename are acceptable options
--verbose: false
--filename: test.txt
positional: -v --verbose -f --filename are acceptable optionsCalled incorrectly with positionals before intended opts
$ optparsing_demo do not put positionals before opts --verbose --filename=mynewfile
--verbose: false
--filename: myfile
positional: do not put positionals before opts --verbose --filename=mynewfileThis method of opt parsing does not support flag chaining like getopt does
$ optparsing_demo -vh
optparsing_demo: Unknown option '-vh'Call with no args
$ zparseopts_demo
--verbose:
--filename: myfile
positional:Call with both short and long options, as well as positional args
$ zparseopts_demo --verbose -f test.txt foo
--verbose: --verbose
--filename: test.txt
positional: fooCall with -- to pass positionals that look like flags
$ zparseopts_demo --filename test.txt -- -v --verbose -f --filename are acceptable options
--verbose:
--filename: test.txt
positional: -v --verbose -f --filename are acceptable optionsCalled incorrectly with positionals before intended opts. If you want this, zparseopts supports it with the -E flag.
$ zparseopts_demo do not put positionals before opts --verbose --filename=mynewfile
--verbose:
--filename: myfile
positional: do not put positionals before opts --verbose --filename=mynewfileThis method of opt parsing does supports flag chaining like getopt does
$ zparseopts_demo -vh
zparseopts_demo [-h|--help]
zparseopts_demo [-v|--verbose] [-f|--filename=<file>] [<message...>]
I don't quite understand what
[[ -z "$flag_help" ]] || { print -l $usage && return }does. Whenzparseopts_demoexecute without any arguments, it called thezparseopts_demo()function and set the local variablesflag_helpandflag_verbosewith anullvalue, right ?Then after initialize
zmodloadandzparseopts, the code ran[[ -z "$flag_help" ]] || { print -l $usage && return }, since $flag_help variable is null so[[ -z "$flag_help" ]]istrue, but why wouldn't it continues to execute{ print -l $usage && return }?No. What I mean is I don't want to use
-or--in the argument but to execute the script like this:zparseopts_demo build# Passbuildas the argument and thezparseopts_demoscript or function will detect there is an argument calledbuildand proceed to do something. If user executrezparseopts_demowithout passing thebuildparameter then the script will perform something else.If I execute
zparseopts_demo -horzparseopts_demo --help, then it will print out the help message.Or does it mean I need to use the format like
zparseopts_demo -p buildif I want to passbuildas an argument but CANNOT simply pass the build argument aszparseopts_demo build?