File: parseargs.sh

package info (click to toggle)
criu 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,500 kB
  • sloc: ansic: 139,280; python: 7,484; sh: 3,824; java: 2,799; makefile: 2,659; asm: 1,137; perl: 206; xml: 117; exp: 45
file content (92 lines) | stat: -rwxr-xr-x 1,723 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
#
# parse command line flags of the form --foo=bar and print out an eval-able line

name=$0

function die() {
	echo "$name: $*" >&2
	exit 1
}

# eat our flags first
while : ; do
	flag=$1
	shift || break
	case $flag in
		--flags-req=*)	# req'd flags
			oIFS="$IFS" IFS=","
			vars_req=(${flag#*=})
			IFS="$oIFS"
			;;
		--flags-opt=*)	# optional flags
			oIFS="$IFS" IFS=","
			vars_opt=(${flag#*=})
			IFS="$oIFS"
			;;
		--name=*)	# name to report errors as
			name=${flag#*=}
			;;
		--flags-only)	# report only flags
			show_flags=true show_args=false
			;;
		--no-flags)	# report only remaining args
			show_flags=false show_args=true
			;;
		--)		# end of our flags; external flags follow
			break
			;;
	esac
done

# consume external flags
while : ; do
	flag=$1
	shift || break
	case $flag in
		--*=*)
			;;
		--)		# end of external flags; uninterpreted arguments follow
			break
			;;
		*)		# pass unrecognized arguments through
			args="$args '$flag'"
			continue
			;;
	esac

	flagname=${flag%%=*}
	flagname=${flagname#--}
	flagval=${flag#*=}

	# check if this flag is declared
	case " ${vars_req[*]} ${vars_opt[*]} " in
		*" $flagname "*)
			;;
		*)		# pass unrecognized flags through
			args="$args '$flag'"
			continue
			;;
	esac

	eval $flagname=\"$flagval\"
done

# check that we have all required flags
for var in ${vars_req[@]}; do
	${!var+true} die "--$var is required"
done

# now print 'em out
if ${show_flags:-true}; then
	for var in ${vars_req[@]} ${vars_opt[@]}; do
		# only print those that are set (even to an empty string)
		${!var+echo $var="'${!var}'"}
	done
fi
if ${show_args:-true}; then
	for arg in "$@"; do	# get quotes right
		args="$args '$arg'"
	done
	echo "set -- $args"
fi