~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to fai/aba/getopt

  • Committer: Robert Collins
  • Date: 2005-09-14 11:27:20 UTC
  • mto: (147.2.6) (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: robertc@robertcollins.net-20050914112720-c66a21de86eafa6e
trim fai cribbage

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Common getopt function...
2
 
# This does not output anything -- it sets the variables in environment right
3
 
# away
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}]
10
 
#
11
 
# The name is name of the variable/function (and in the same time variable).
12
 
#   foo
13
 
#       If "--foo" is encountered, foo=yes is executed
14
 
#   foo|f|bar
15
 
#       If "--foo", "-f" or "--bar" is encountered, foo=--foo is executed,
16
 
#       resp. foo=-f, foo==--bar.
17
 
#   foo=s
18
 
#       If "--foo" is encountered, foo is set to next argument (alternate names
19
 
#       are possible too).
20
 
#   foo=f
21
 
#       If "--foo" is encountered, process_foo is called with the next argument.
22
 
#       (of course, alternate names are possible too).
23
 
#
24
 
# "dir|d=f archive|A=s" should be specified to handle dir and archive options
25
 
# (all the rest is provided here).
26
 
#
27
 
# WARNING: Option names may only contain alphanumerics and underscore, NO '-' !!
28
 
 
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.
32
 
 
33
 
process_dir () {
34
 
    cd "$1" || { echo "* Can't change to directory $1" >&2; exit 1; }
35
 
}
36
 
 
37
 
aba_describe_argument () {
38
 
    local spec preeq posteq name variant
39
 
    for spec in $options; do
40
 
        preeq=${spec%%=*}
41
 
        name=${spec%%[|=]*}
42
 
        case $spec in
43
 
            *=*) posteq="=${spec##*=}";;
44
 
            *) posteq="";;
45
 
        esac
46
 
        while [ -n "$preeq" ]; do
47
 
            variant=${preeq%%|*}
48
 
            preeq=${preeq#$variant}
49
 
            preeq=${preeq#|}
50
 
            test "$1" = "--$variant" -o "$1" = "-$variant" && echo "$name$posteq"
51
 
        done
52
 
    done
53
 
}
54
 
 
55
 
archive=$(tla my-default-archive)
56
 
next=''
57
 
for arg; do
58
 
    shift # Gradualy clean up old args
59
 
    if [ -z "$options" ]; then
60
 
        set -- "$@" "$arg"
61
 
    elif [ -n "$next" ]; then
62
 
        case $next in
63
 
            *=f) eval "process_${next%=f} \"$arg\"";;
64
 
            *=s) eval "${next%=s}=\"$arg\"";;
65
 
        esac
66
 
        next=''
67
 
    else
68
 
        case $arg in
69
 
            --) options='';;
70
 
            -[a-zA-Z]|--[^-]?*)
71
 
                desc=$(aba_describe_argument $arg)
72
 
                test -z "$desc" && { echo "* Unrecognized option: $arg" >&2; exit 1; }
73
 
                case $desc in
74
 
                    *=[fs]) next=$desc;;
75
 
                    *) eval "$desc=yes";;
76
 
                esac;;
77
 
            *) set -- "$@" "$arg";;
78
 
        esac
79
 
    fi
80
 
done
81
 
 
82
 
# arch-tag: 85616ea1-0cd4-4f5d-be25-530325e9081d
83
 
# vim: set ft=sh: