Commit f473855a authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Implemented named argument parsing with set_args

parent fca8da1e
Loading
Loading
Loading
Loading
+43 −3
Original line number Diff line number Diff line
@@ -102,6 +102,44 @@ add_arg_alias ()
}


##
# set_arg()
# Set values for arguments from arguments and return the number of arguments 
# used. Fail if there are not enough arguments.
#
# Usage:
# set_arg <argument> [<value [<value> ...]]
##
set_arg ()
{
	local argument="$1"
	local arg dest metavar argtype default names name found
	local IFS
	for arg in ${!_ARGS_*}; do
		{
			IFS=,
			read dest metavar argtype default
			read -a names
		} <<< "${!arg}"
		for name in $names; do
			if [ $name = $argument ]; then
				found=yes
				break
			fi
		done
		[ "$found" ] && break
	done
	[ "$found" ] || die "unknown argument $argument"
	if [ $argtype = FLAG ]; then
		eval $dest=yes
		return 0
	else
		eval $dest=$2
		return 1
	fi
}


##
# add_option()
# Add an option (positional argument) for the parser to look for.
@@ -215,7 +253,7 @@ summarise_args ()
parse_args ()
{
	local command=$1
	local opts_only
	local opts_only num
	shift
	while [ "$1" ]; do
		if [ "$opts_only" ]; then
@@ -234,8 +272,10 @@ parse_args ()
				opts_only=yes
				;;
			-*) # argument
				set_arg "$1" "$2"
				set_arg "$@" || num=$?
				for i in `seq 0 $num`; do
					shift
				done
				;;
			*) # positional option
				next_opt "$1"