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: foo
Call 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 options
Called 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=mynewfile
This 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: foo
Call 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 options
Called 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=mynewfile
This 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_demo
execute without any arguments, it called thezparseopts_demo()
function and set the local variablesflag_help
andflag_verbose
with anull
value, right ?Then after initialize
zmodload
andzparseopts
, 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
# Passbuild
as the argument and thezparseopts_demo
script or function will detect there is an argument calledbuild
and proceed to do something. If user executrezparseopts_demo
without passing thebuild
parameter then the script will perform something else.If I execute
zparseopts_demo -h
orzparseopts_demo --help
, then it will print out the help message.Or does it mean I need to use the format like
zparseopts_demo -p build
if I want to passbuild
as an argument but CANNOT simply pass the build argument aszparseopts_demo build
?