1
# Common getopt function...
2
# This does not output anything -- it sets the variables in environment right
4
# To parse options, source this file. It will parse any options specified in
5
# "$options" and in addition will handle -d/--dir and -A/--archive. The -d
6
# option is handled by changing to the directory right away, the -A option's
7
# value is simply stored in "$archive" variable.
8
# The options variable is a space-separated list of clauses like:
9
# <name>[|<alternate names...>][={f|s}]
11
# The name is name of the variable/function (and in the same time variable).
13
# If "--foo" is encountered, foo=yes is executed
15
# If "--foo", "-f" or "--bar" is encountered, foo=--foo is executed,
16
# resp. foo=-f, foo==--bar.
18
# If "--foo" is encountered, foo is set to next argument (alternate names
21
# If "--foo" is encountered, process_foo is called with the next argument.
22
# (of course, alternate names are possible too).
24
# "dir|d=f archive|A=s" should be specified to handle dir and archive options
25
# (all the rest is provided here).
27
# WARNING: Option names may only contain alphanumerics and underscore, NO '-' !!
29
# The file must be sourced directly in the appropriate context where options
30
# should be parsed (since functions have privat argument lists) and does the
31
# parsing whlie being sourced. So $options must be defined beforehand.
34
cd "$1" || { echo "* Can't change to directory $1" >&2; exit 1; }
37
aba_describe_argument () {
38
local spec preeq posteq name variant
39
for spec in $options; do
43
*=*) posteq="=${spec##*=}";;
46
while [ -n "$preeq" ]; do
48
preeq=${preeq#$variant}
50
test "$1" = "--$variant" -o "$1" = "-$variant" && echo "$name$posteq"
55
archive=$(tla my-default-archive)
58
shift # Gradualy clean up old args
59
if [ -z "$options" ]; then
61
elif [ -n "$next" ]; then
63
*=f) eval "process_${next%=f} \"$arg\"";;
64
*=s) eval "${next%=s}=\"$arg\"";;
71
desc=$(aba_describe_argument $arg)
72
test -z "$desc" && { echo "* Unrecognized option: $arg" >&2; exit 1; }
77
*) set -- "$@" "$arg";;
82
# arch-tag: 85616ea1-0cd4-4f5d-be25-530325e9081d