{"thread":{"id":"878","subject":"[PATCH 1/6] Add infrastructure for option parsing to cg-Xlib","startedAt":"2005-06-09T11:15:14Z","lastAt":"2005-06-09T11:15:14Z","messageCount":1,"participants":["Dan Holmsand"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"4777","messageId":"42A824C2.8060207@gmail.com","threadId":"878","inReplyTo":null,"subject":"[PATCH 1/6] Add infrastructure for option parsing to cg-Xlib","fromName":"Dan Holmsand","fromEmail":"holmsand@gmail.com","sentAt":"2005-06-09T11:15:14Z","receivedAt":"2005-06-09T11:15:14Z","isPatch":true,"sender":{"key":"holmsand@gmail.com","avatar":"https://gravatar.com/avatar/5c722084bafd85e754a02efad01fe69107eb6f393253c49232c5c9f7faa974df?d=mp&s=160"},"body":"Adds a new function, optparse, that handles combined options\njust like optget (cg-log -cf, for example).\n\nWhen called without any argument, it returns 0 if there is any\noption to parse.\n\nWhen called as \"optparse -f\" or \"optparse --foo\" it returns 0\nif the current option in $ARGS (at $ARGPOS) matches.\n\nWhen called as \"optparse -f= or \"optparse --foo=\" it requires\nan argument.\n\nFor long options, it takes an optional second argument, specifying\nthe number of characters in the option name that has to be present\n(defaults to 1). So \"optparse --foo 2\" will match \"--fo\", but not\n\"--f\".\n\nJust as with optget, options can be given after arguments as\nwell, as in \"cg-log Documentation -rorigin\". Option parsing\nstops after \"--\".\n\noptparse leaves unparsed arguments in the $ARGS array.\n\nSigned-off-by: Dan Holmsand <holmsand@gmail.com>\n---\n\n\n\n cg-Xlib |   49 +++++++++++++++++++++++++++++++++++++++++++++++++\n 1 files changed, 49 insertions(+), 0 deletions(-)\n\ndiff --git a/cg-Xlib b/cg-Xlib\n--- a/cg-Xlib\n+++ b/cg-Xlib\n@@ -137,11 +137,60 @@ print_help () {\n }\n \n for option in \"$@\"; do\n+\t[ \"$option\" = -- ] && break\n \tif [ \"$option\" = \"-h\" -o \"$option\" = \"--help\" ]; then\n \t\tprint_help ${_cg_cmd##cg-}\n \tfi\n done\n \n+ARGS=(\"$@\")\n+ARGPOS=0\n+\n+optshift() {\n+\tunset ARGS[$ARGPOS]\n+\tARGS=(\"${ARGS[@]}\")\n+\t[ -z \"$1\" -o -n \"${ARGS[$ARGPOS]}\" ] || \n+\tdie \"option \\`$1' requires an argument\"\n+}\n+\n+optfail() {\n+\tdie \"unrecognized option \\`${ARGS[$ARGPOS]}'\"\n+}\n+\n+optconflict() {\n+\tdie \"conflicting option \\`$CUROPT'\"\n+}\n+\n+optparse() {\n+\tunset OPTARG\n+\t[ -z \"$1\" ] && case ${ARGS[$ARGPOS]} in\n+\t--)\toptshift; return 1 ;;\n+\t-*)\treturn 0 ;;\n+\t*)\twhile (( ${#ARGS[@]} > ++ARGPOS )); do\n+\t\t\t[[ \"${ARGS[$ARGPOS]}\" == -- ]] && return 1\n+\t\t\t[[ \"${ARGS[$ARGPOS]}\" == -* ]] && return 0\n+\t\tdone; return 1 ;;\n+\tesac\n+\n+\tCUROPT=${ARGS[$ARGPOS]}\n+\tlocal match=${1%=} minmatch=${2:-1} opt=$CUROPT o=$CUROPT val\n+\t[[ $1 == *= ]] && val=$match\n+\tcase $match in\n+\t--*)\t[ \"$val\" ] && o=${o%%=*}\n+\t\t[ ${#o} -ge $((2 + $minmatch)) -a \\\n+\t\t\t\"${match:0:${#o}}\" = \"$o\" ] || return 1\n+\t\t[[ -n \"$val\" && \"$opt\" == *=?* ]] && ARGS[$ARGPOS]=${opt#*=} ||\n+\t\toptshift $val ;;\n+\t-?)\t[[ $o == $match* ]] || return 1\n+\t\t[[ $o != -?-* || -n \"$val\" ]] || optfail\n+\t\tARGS[$ARGPOS]=${o#$match}\n+\t\t[ \"${ARGS[$ARGPOS]}\" ] && \n+\t\t{ [ \"$val\" ] || ARGS[$ARGPOS]=-${ARGS[$ARGPOS]}; } || \n+\t\toptshift $val ;;\n+\t*)\tdie \"optparse cannot handle $1\" ;;\n+\tesac\n+\t[ -z \"$val\" ] || { OPTARG=${ARGS[$ARGPOS]}; optshift; } \n+}\n \n # Check if we have something to work on, unless the script can do w/o it.\n if [ ! \"$_git_repo_unneeded\" ]; then\n"}]}