From: Dan Holmsand <holmsand@gmail.com>
To: git@vger.kernel.org
Cc: Petr Baudis <pasky@ucw.cz>
Subject: [PATCH 1/6] Add infrastructure for option parsing to cg-Xlib
Date: Thu, 09 Jun 2005 13:15:14 +0200 [thread overview]
Message-ID: <42A824C2.8060207@gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 837 bytes --]
Adds a new function, optparse, that handles combined options
just like optget (cg-log -cf, for example).
When called without any argument, it returns 0 if there is any
option to parse.
When called as "optparse -f" or "optparse --foo" it returns 0
if the current option in $ARGS (at $ARGPOS) matches.
When called as "optparse -f= or "optparse --foo=" it requires
an argument.
For long options, it takes an optional second argument, specifying
the number of characters in the option name that has to be present
(defaults to 1). So "optparse --foo 2" will match "--fo", but not
"--f".
Just as with optget, options can be given after arguments as
well, as in "cg-log Documentation -rorigin". Option parsing
stops after "--".
optparse leaves unparsed arguments in the $ARGS array.
Signed-off-by: Dan Holmsand <holmsand@gmail.com>
---
[-- Attachment #2: 1-optparse.patch.txt --]
[-- Type: text/plain, Size: 1740 bytes --]
cg-Xlib | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 49 insertions(+), 0 deletions(-)
diff --git a/cg-Xlib b/cg-Xlib
--- a/cg-Xlib
+++ b/cg-Xlib
@@ -137,11 +137,60 @@ print_help () {
}
for option in "$@"; do
+ [ "$option" = -- ] && break
if [ "$option" = "-h" -o "$option" = "--help" ]; then
print_help ${_cg_cmd##cg-}
fi
done
+ARGS=("$@")
+ARGPOS=0
+
+optshift() {
+ unset ARGS[$ARGPOS]
+ ARGS=("${ARGS[@]}")
+ [ -z "$1" -o -n "${ARGS[$ARGPOS]}" ] ||
+ die "option \`$1' requires an argument"
+}
+
+optfail() {
+ die "unrecognized option \`${ARGS[$ARGPOS]}'"
+}
+
+optconflict() {
+ die "conflicting option \`$CUROPT'"
+}
+
+optparse() {
+ unset OPTARG
+ [ -z "$1" ] && case ${ARGS[$ARGPOS]} in
+ --) optshift; return 1 ;;
+ -*) return 0 ;;
+ *) while (( ${#ARGS[@]} > ++ARGPOS )); do
+ [[ "${ARGS[$ARGPOS]}" == -- ]] && return 1
+ [[ "${ARGS[$ARGPOS]}" == -* ]] && return 0
+ done; return 1 ;;
+ esac
+
+ CUROPT=${ARGS[$ARGPOS]}
+ local match=${1%=} minmatch=${2:-1} opt=$CUROPT o=$CUROPT val
+ [[ $1 == *= ]] && val=$match
+ case $match in
+ --*) [ "$val" ] && o=${o%%=*}
+ [ ${#o} -ge $((2 + $minmatch)) -a \
+ "${match:0:${#o}}" = "$o" ] || return 1
+ [[ -n "$val" && "$opt" == *=?* ]] && ARGS[$ARGPOS]=${opt#*=} ||
+ optshift $val ;;
+ -?) [[ $o == $match* ]] || return 1
+ [[ $o != -?-* || -n "$val" ]] || optfail
+ ARGS[$ARGPOS]=${o#$match}
+ [ "${ARGS[$ARGPOS]}" ] &&
+ { [ "$val" ] || ARGS[$ARGPOS]=-${ARGS[$ARGPOS]}; } ||
+ optshift $val ;;
+ *) die "optparse cannot handle $1" ;;
+ esac
+ [ -z "$val" ] || { OPTARG=${ARGS[$ARGPOS]}; optshift; }
+}
# Check if we have something to work on, unless the script can do w/o it.
if [ ! "$_git_repo_unneeded" ]; then
reply other threads:[~2005-06-09 12:19 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=42A824C2.8060207@gmail.com \
--to=holmsand@gmail.com \
--cc=git@vger.kernel.org \
--cc=pasky@ucw.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).