[gobolinux-devel] [gobolinux-commits] tools/Scripts/Functions OptionParser
Jonas Karlsson
jonka750 at student.liu.se
Mon Nov 20 00:00:58 UTC 2006
I've just commited my new implementation of OptionParser. The new
functionallity can be found in the short log, but I thought I might give
some examples to clear things up. Using Compile as an example, valid calls
are:
Compile -bS Foo
Compile Foo --no-sign
Compile -VnFooBar --version=2.3 Foo
Compile -xppc Foo
Compile -bGc"--no-app --without-lib" Foo
For the arguments there are a new array, $Arg, to be used instead of $1,
$2 in combinations with shift. $Arg[0] is the same as $0 and then the
arguments are numbered in the order of appearance.
Even though there's a new way of accessing arguments this commit didn't
break the old one, but people should try to change the way arguments are
handled in the existing scripts, using $Arg instead of $1 etc.
Questions, views, objections? - Please! :)
/Jonas
On Mon, 20 Nov 2006 00:47:12 +0100, Jonas Karlsson
<jonka750 at student.liu.se> wrote:
> CVSROOT: /sources/goboscripts
> Module name: tools
> Changes by: Jonas Karlsson <mohjive> 06/11/19 23:47:12
>
> Modified files:
> Scripts/Functions: OptionParser
>
> Log message:
> Commited the new option parser
> Short log
> * short options can be stacked
> * long options can be with or without =
> * arguments are reachable from $Arg[n] (to be used instead of $1 and
> shifts)
> * arguments can come in any order (for scripts that use $Arg[n])
> * everything after '--' is considered arguments
>
> CVSWeb URLs:
> http://cvs.savannah.gnu.org/viewcvs/tools/Scripts/Functions/OptionParser?cvsroot=goboscripts&r1=1.5&r2=1.6
>
> Patches:
> Index: OptionParser
> ===================================================================
> RCS file: /sources/goboscripts/tools/Scripts/Functions/OptionParser,v
> retrieving revision 1.5
> retrieving revision 1.6
> diff -u -b -r1.5 -r1.6
> --- OptionParser 18 Nov 2006 22:50:48 -0000 1.5
> +++ OptionParser 19 Nov 2006 23:47:12 -0000 1.6
> @@ -51,8 +51,6 @@
> fi
> }
> -
> -
> function Entry() {
> local i
> for i in `seq $optionsListSize`
> @@ -259,8 +257,9 @@
> exit 0
> }
> -function Parse_Options() {
> +function Parse_Options_Old() {
> local i
> + export parsedArguments=0
> savedOptions=("$@")
> if [ "$1" = "--help" -o "$1" = "-h" ] || [ "$helpOnNoArguments" -a
> -z "$*" ]
> then Show_Help
> @@ -319,6 +318,111 @@
> }
> }
> +function Parse_Options() {
> + Arg[0]=$0
> + if [ "$helpOnNoArguments" -a -z "$*" ]
> + then Show_Help
> + fi
> +
> + for (( i = 1; i <= $# ; i++ ))
> + do
> + eval option=\$$i
> +
> + if [ "${option:0:1}" = "-" ]; then
> + if [ "${option:1:1}" = "-" ]; then
> + if [ -z "${option:2:1}" ]; then
> + for (( l = 1; l <= $[$#-$i] ; l++ ))
> + do
> + eval Arg[$[${#Arg}+$l]]=\$$[$l+$i]
> + done
> + break
> + fi
> + if echo "$option" | grep -q = - ; then
> + opt=`echo $option | cut -d= -f1`
> + else
> + opt=$option
> + fi
> + export parsedArguments=$[parsedArguments+1]
> + for j in `seq $optionsListSize`
> + do
> + if [ "$opt" = "--${optionsLongList[j]}" ]
> + then
> + case "${optionsTypeList[j]}" in
> + "Boolean")
> + optionsStateList[j]="on"
> + ;;
> + "Entry"|"List")
> + if echo "$option" | grep -q = - ; then
> + val=`echo $option | cut -d= -f2`
> + else
> + i=$[i+1]
> + eval val=\$$i
> + export parsedArguments=$[parsedArguments+1]
> + fi
> + if [ -z "$val" ]
> + then
> + Log_Error "Unknown option: $opt requires an
> argument."
> + Show_Help
> + exit 1
> + fi
> + optionsStateList[j]="$val"
> + ;;
> + esac
> + break
> + fi
> + done
> + else
> + for (( k = 1; k <= ${#option} ; k++ ))
> + do
> + opt=${option:$k:1}
> + for j in `seq $optionsListSize`
> + do
> + if [ "$opt" = "${optionsShortList[j]}" ]
> + then
> + case "${optionsTypeList[j]}" in
> + "Boolean")
> + optionsStateList[j]="on"
> + break
> + ;;
> + "Entry"|"List")
> + if [ ! -z "${option:$[k+1]:1}" ]; then
> + val=${option:$[k+1]:${#option}}
> + else
> + i=$[i+1]
> + eval val=\$$i
> + export parsedArguments=$[parsedArguments+1]
> + fi
> + if [ -z "$val" ]
> + then
> + Log_Error "Unknown option: $opt requires an
> argument."
> + Show_Help
> + exit 1
> + fi
> + optionsStateList[j]="$val"
> + break 2
> + ;;
> + esac
> + fi
> + done
> + done
> + export parsedArguments=$[parsedArguments+1]
> + fi
> + else
> + Arg[$[${#Arg[@]}+1]]=$option
> + fi
> + if Boolean help
> + then Show_Help
> + fi
> + if Boolean version
> + then Show_Version
> + fi
> + if Boolean list-options
> + then List_Options
> + fi
> + done
> +# Parse_Options_Old "$@"
> +}
> +
> Add_Option_Boolean "h" "help" "Shows this help."
> Add_Option_Boolean "v" "version" "Show program version."
> Add_Option_Boolean "V" "verbose" "Enable verbose mode."
> _______________________________________________
> gobolinux-commits mailing list
> gobolinux-commits at lists.gobolinux.org
> http://lists.gobolinux.org/mailman/listinfo/gobolinux-commits
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
More information about the gobolinux-devel
mailing list