2 # (C) 2006-2011 Andre Noll
4 if [[ $(type -t gsu_is_a_number) != "function" ]]; then
5 GSU_DIR=${GSU_DIR:=$HOME/.gsu}
6 . $GSU_DIR/common || exit 1
11 gsu_short_msg "# Usage: $_gsu_self command [options]"
15 # Each line matching this is recognized as a subcommand. The name
16 # of the subcommand is the first subexpression.
17 export gsu_command_regex='^com_\([-a-zA-Z_0-9]\+\)()'
19 _gsu_available_commands()
22 printf "help\nman\nprefs\n"
23 sed -ne "s/$gsu_command_regex/\1/g;T;p" $0
24 } | sort | tr '\n' ' ')"
26 export -f _gsu_available_commands
28 _gsu_print_available_commands()
31 gsu_short_msg "Available commands:"
32 for i in $gsu_cmds; do
35 if test $(($count % 4)) -eq 0; then
39 if test ${#i} -lt 8; then
47 export -f _gsu_print_available_commands
49 export gsu_prefs_txt="
50 Print the current preferences.
54 If -e is given, the config file is opened with the default editor. Without
55 options, the command prints out a list of all cmt config variables, together
56 with their current value and the default value."
59 local i conf="${gsu_config_file:=$HOME/.$gsu_name.rc}"
61 if [[ "$1" = "-e" ]]; then
65 [[ $? -ne 0 ]] && return
67 result="${EDITOR:-vi}"
69 [[ $? -ne 0 ]] && return
74 for ((i=0; i < ${#gsu_options[@]}; i++)); do
75 local name= option_type= default_value= required=
76 local description= help_text=
77 eval "${gsu_options[$i]}"
78 eval val='"$'${gsu_config_var_prefix}_$name'"'
87 printf " $option_type: $description"
88 if [[ "$required" != "yes" && "$required" != "true" ]]; then
89 printf " [$default_value]"
92 [[ -n "$help_text" ]] && sed -e '/^[ ]*$/d; s/^[ ]*/# /g' <<< "$help_text"
94 [[ "$val" == "$default_value" ]] && printf " # default"
107 local equal_signs="=================================================="
108 local minus_signs="--------------------------------------------------"
111 echo "$_gsu_self (_${gsu_banner_txt}_) manual"
112 echo "${equal_signs:0:${#_gsu_self} + ${#gsu_banner_txt} + 16}"
115 sed -e '1,/^#\{70,\}/d' -e '/^#\{70,\}/,$d' $0 -e 's/^# *//'
118 echo "$_gsu_self usage"
119 echo "${minus_signs:0:${#_gsu_self} + 6}"
122 echo "Each command has its own set of options as described below."
126 echo "Available commands:"
128 _gsu_available_commands
129 for com in $result; do
131 if test $num -lt 4; then
134 echo "${minus_signs:0:$num}"
136 echo "${minus_signs:0:$num}"
146 local txt="### $_gsu_self --"
147 if test -z "$gsu_banner_txt"; then
148 txt="$txt set \$gsu_banner_txt to customize this message"
150 txt="$txt $gsu_banner_txt"
152 gsu_short_msg "$txt ###"
154 export -f _gsu_banner_msg
156 export gsu_help_txt="
159 Usage: help [command]
161 Without arguments, print the list of available commands. Otherwise,
162 print the help text for the given command."
167 if test -z "$1"; then
171 printf "com_help()\n$gsu_help_txt" | head -n 4; echo "--"
172 printf "com_man()\n$gsu_man_txt" | head -n 4; echo "--"
173 printf "com_prefs()\n$gsu_prefs_txt" | head -n 4; echo "--"
174 grep -A 2 "$gsu_command_regex" $0
175 } | grep -v -- '--' \
176 | sed -e "/$gsu_command_regex/bs" \
177 -e 'H;$!d;x;s/\n//g;b' \
179 -e 'x;s/\n//g;${p;x;}' \
180 | sed -e "s/${gsu_command_regex}#*/\1\t/" \
184 if test ${#a} -lt 8; then
190 echo "# Try $_gsu_self help <command> for info on <command>."
194 if test "$1" = "help"; then
199 if test "$1" = "man"; then
204 if test "$1" = "prefs"; then
205 echo "$gsu_prefs_txt"
210 if grep -q "^com_$1()" $0; then
211 sed -e "1,/^com_$1()$/d" -e '/^{/,$d' -e 's/^## *//' $0
214 _gsu_print_available_commands
216 ret=-$E_GSU_BAD_COMMAND
220 # Wrapper for bash's getopts.
222 # Aborts on programming errors such as missing or invalid option string. On
223 # success $result contains shell code that can be eval'ed. For each defined
224 # option x, the local variable o_x will be created when calling eval "$result".
225 # o_x contains true/false for options without an argument or the emtpy string/the
226 # given argument, depending on whether this option was contained in the "$@"
230 # gsu_getopts abc:x:y
232 # [[ $ret -lt 0 ]] && return
234 # [[ "$o_a" = "true ]] && echo "The -a flag was given"
235 # [[ -n "$o_c" ]] && echo "The -c option was given with arg $o_c"
238 local i c tab=' ' cr='
241 gsu_check_arg_count $# 1 1
242 if [[ $ret -lt 0 ]]; then
248 result="invalid optstring $1"
249 if [[ -z "$1" ]] || grep -q '::' <<< "$1" ; then
254 for ((i=0; i < ${#1}; i++)); do
260 result="invalid character $c in optstring"
266 for ((i=0; i < ${#1}; i++)); do
268 c2=${1:$(($i + 1)):1}
270 if [[ "$c2" = ":" ]]; then
278 while getopts $1 opt \"\$@\"; do
281 for ((i=0; i < ${#1}; i++)); do
283 c2=${1:$(($i + 1)):1}
284 result+="$tab$tab$c1) o_$c1="
285 if [[ "$c2" = ":" ]]; then
286 result+="\"\$OPTARG\""
296 result=\"invalid option given\"
301 shift \$((\$OPTIND - 1))
305 export -f gsu_getopts
311 _gsu_available_commands
313 if test $# -eq 0; then
315 _gsu_print_available_commands
320 # check internal commands
321 if [[ "$arg" = "help" || "$arg" = "man" || "$arg" = "prefs" ]]; then
323 if [[ "$ret" -lt 0 ]]; then
331 for i in $gsu_cmds; do
332 if test "$arg" = "$i"; then
334 if [[ "$ret" -lt 0 ]]; then
342 ret=-$E_GSU_BAD_COMMAND
345 _gsu_print_available_commands
350 # Check number of arguments.
352 # Usage: gsu_check_arg_count <num_given> <num1> [<num2>]
354 # Check that <num_given> is between <num1> and <num2> inclusively.
355 # If only <num1> ist given, num2 is assumed to be infinity.
358 # 0 0 no argument allowed
359 # 1 1 exactly one argument required
360 # 0 2 at most two arguments admissible
361 # 2 at least two arguments reqired
363 gsu_check_arg_count()
365 ret=-$E_GSU_BAD_ARG_COUNT
366 if [[ $# -eq 2 ]]; then # only num1 is given
367 result="at least $2 args required, $1 given"
368 [[ $1 -lt $2 ]] && return
372 # num1 and num2 given
373 result="need at least $2 args, $1 given"
374 [[ $1 -lt $2 ]] && return
375 result="need at most $3 args, $1 given"
376 [[ $1 -gt $3 ]] && return
379 export -f gsu_check_arg_count