Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Replacing getopts with zparseopts
- X-seq: zsh-users 16029
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: Replacing getopts with zparseopts
- Date: Sun, 15 May 2011 11:00:05 -0700
- In-reply-to: <iqooau$tdf$1@dough.gmane.org>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <iqooau$tdf$1@dough.gmane.org>
On May 15, 4:34pm, Thorsten Kampe wrote:
}
} Could someone help me to create the below simple construct with
} zparseopts and making sure that "-d" and "--debug", "-h" and "--help",
} and "-v" and "--verbose" refer to the same option?!
This depends on what you intend by "refer to the same option."
All zparseopts does is read (and possibly strip) the options from the
command line and place their values in parameters. It's then up to
later code to interpret the parameters correctly.
For example with "zparseopts -A opthash ..." there isn't any direct
way to tell zparseopts that both "-d" and "--debug" should place a
value in $opthash[d]. If you want to use the -A form, you have to
later check for ${opthash[(i)-d|--debug]} or in some other way check
for both/either.
If you use individual arrays for each option, then you can do this:
local -a debug help vers usage
zparseopts d=debug -debug=debug \
h=help -help=help \
v=vers -version=vers \
\?=usage
and e.g. $+debug[1] indicates whether one of -d or --debug was used,
and the value in $debug will tell you which one. However, if *both*
were used, $debug will contain both, whereas if -d were used twice,
it would appear in $debug only once.
Compare this to "zparseopts -d+=debug" in which two uses of -d places
two array elements in $debug.
} ##
} while getopts dhv opt
} do case $opt in
} (d) setopt xtrace;;
}
} (h) print_help
} exit;;
}
} (v) print_version
} exit;;
}
} (\?) print_usage >&2
} exit 1;;
} esac
} done
} ##
It's probably obvious but to conclude the example begun above:
[[ -n $debug ]] && setopt xtrace
[[ -n $help ]] && { print_help; exit }
[[ -n $version ]] && { print_version; exit }
[[ -n $usage ]] && { print_usage >&2; exit 1 }
Note here we don't know in what *order* the options were given, so
when both -h and -v appear in either order it'll always print the
help and then exit. You could instead write --
[[ -n $version ]] && print_version
[[ -n $help ]] && print_help
[[ -n "$help$version" ]] && exit
-- to print both the version and the help when -h and -v are both used
in any order.
For this simple a case, zparseopts doesn't really save you any effort;
in fact it's probably a bit worse than getopts except that it handles
the long option forms. Where zparseopts really gets useful is when for
example you are writing a wrapper function for another command. In
that case you can create a zparseopts description of the options taken
by the wrapped command, and use "zparseopts -D ..." to strip them out
of the command line so the wrapper can manipulate what's left.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author