Getopts
The predecessor to HistoryThe original
In 1995, The getopts command has also been ported to the IBM i operating system.[5] The modern usage of UsageThe usage synopsis of getopt and getopts is similar to its C sibling: getopt optstring [parameters] getopts optstring varname [parameters]
The way one uses the commands however varies a lot:
EnhancementsIn various getoptsIn spring 2004 (Solaris 10 beta development), the libc implementation for getopt() was enhanced to support long options. As a result, this new feature was also available in the built-in command KornShell and Zsh both have an extension for long arguments. The former is defined as in Solaris,[8] while the latter is implemented via a separate KornShell additionally implements optstring extensions for options beginning with In Linux getoptAn alternative to The Linux enhanced version of Comparison
ExamplesSuppose we are building a Wikipedia downloader in bash that takes three options and zero extra arguments: wpdown -a article name -l [language] -v When possible, we allow the following long arguments: -a --article -l --language, --lang -v --verbose For clarity, no help text is included, and we assume there is a program that downloads any webpage. In addition, all programs are of the form: #!/bin/bash
verbose=0
article=
lang=en
# [EXAMPLE HERE]
if ((verbose > 2)); then
printf '%s\n' 'Non-option arguments:'
printf '%q ' "${remaining[@]]}"
fi
if ((verbose > 1)); then
printf 'Downloading %s:%s\n' "$lang" "$article"
fi
if [[ ! $article ]]; then
printf '%s\n' "No articles!" >&2
exit 1
fi
save_webpage "https://${lang}.wikipedia.org/wiki/${article}"
Using old getoptThe old getopt does not support optional arguments: # parse everything; if it fails we bail
args=`getopt 'a:l:v' $*` || exit
# now we have the sanitized args... replace the original with it
set -- $args
while true; do
case $1 in
(-v) ((verbose++)); shift;;
(-a) article=$2; shift 2;;
(-l) lang=$2; shift 2;;
(--) shift; break;;
(*) exit 1;; # error
esac
done
remaining=("$@")
This script will also break with any article title with a space or a shell metacharacter (like ? or *) in it. Using getoptsGetopts give the script the look and feel of the C interface, although in POSIX optional arguments are still absent: #!/bin/sh
while getopts ':a:l:v' opt; do
case $opt in
(v) ((verbose++));;
(a) article=$OPTARG;;
(l) lang=$OPTARG;;
(:) # "optional arguments" (missing option-argument handling)
case $OPTARG in
(a) exit 1;; # error, according to our syntax
(l) :;; # acceptable but does nothing
esac;;
esac
done
shift "$((OPTIND - 1))"
# remaining is "$@"
Since we are no longer operating on shell options directly, we no longer need to shift them within the loop. However, a slicing operation is required to remove the parsed options and leave the remaining arguments. It is fairly simple to emulate long option support of flags by treating Using Linux getoptLinux getopt escapes its output and an "eval" command is needed to have the shell interpret it. The rest is unchanged: #!/bin/bash
# We use "${@}" instead of "${*}" to preserve argument-boundary information
args=$(getopt --options 'a:l::v' --longoptions 'article:,lang::,language::,verbose' -- "${@}") || exit
eval "set -- ${args}"
while true; do
case "${1}" in
(-v | --verbose)
((verbose++))
shift
;;
(-a | --article)
article=${2}
shift 2
;;
(-l | --lang | --language)
# handle optional: getopt normalizes it into an empty string
if [[ -n ${2} ]] ; then
lang=${2}
fi
shift 2
;;
(--)
shift
break
;;
(*)
exit 1 # error
;;
esac
done
remaining_args=("${@}")
See alsoReferences
External links
|
Portal di Ensiklopedia Dunia