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]"
14 # Each line matching this is recognized as a subcommand. The name
15 # of the subcommand is the first subexpression.
16 export gsu_command_regex='^com_\([-a-zA-Z_0-9]\+\)()'
18 _gsu_available_commands()
21 printf "help\nman\nprefs\n"
22 sed -ne "s/$gsu_command_regex/\1/g;T;p" $0
23 } | sort | tr '\n' ' ')"
26 _gsu_print_available_commands()
29 gsu_short_msg "Available commands:"
30 for i in $gsu_cmds; do
33 if test $(($count % 4)) -eq 0; then
37 if test ${#i} -lt 8; then
46 export gsu_prefs_txt="
47 Print the current preferences.
51 If -e is given, the config file is opened with the default editor. Without
52 options, the command prints out a list of all cmt config variables, together
53 with their current value and the default value."
56 local i conf="${gsu_config_file:=$HOME/.$gsu_name.rc}"
58 if [[ "$1" = "-e" ]]; then
62 [[ $? -ne 0 ]] && return
64 result="${EDITOR:-vi}"
66 [[ $? -ne 0 ]] && return
71 for ((i=0; i < ${#gsu_options[@]}; i++)); do
72 local name= option_type= default_value= required=
73 local description= help_text=
74 eval "${gsu_options[$i]}"
75 eval val='"$'${gsu_config_var_prefix}_$name'"'
84 printf " $option_type: $description"
85 if [[ "$required" != "yes" && "$required" != "true" ]]; then
86 printf " [$default_value]"
89 [[ -n "$help_text" ]] && sed -e '/^[ ]*$/d; s/^[ ]*/# /g' <<< "$help_text"
91 [[ "$val" == "$default_value" ]] && printf " # default"
103 local equal_signs="=================================================="
104 local minus_signs="--------------------------------------------------"
107 echo "$_gsu_self (_${gsu_banner_txt}_) manual"
108 echo "${equal_signs:0:${#_gsu_self} + ${#gsu_banner_txt} + 16}"
111 sed -e '1,/^#\{70,\}/d' -e '/^#\{70,\}/,$d' $0 -e 's/^# *//'
114 echo "$_gsu_self usage"
115 echo "${minus_signs:0:${#_gsu_self} + 6}"
118 echo "Each command has its own set of options as described below."
122 echo "Available commands:"
124 _gsu_available_commands
125 for com in $result; do
127 if test $num -lt 4; then
130 echo "${minus_signs:0:$num}"
132 echo "${minus_signs:0:$num}"
141 local txt="### $_gsu_self --"
142 if test -z "$gsu_banner_txt"; then
143 txt="$txt set \$gsu_banner_txt to customize this message"
145 txt="$txt $gsu_banner_txt"
147 gsu_short_msg "$txt ###"
150 export gsu_help_txt="
153 Usage: help [command]
155 Without arguments, print the list of available commands. Otherwise,
156 print the help text for the given command."
161 if test -z "$1"; then
165 printf "com_help()\n$gsu_help_txt" | head -n 4; echo "--"
166 printf "com_man()\n$gsu_man_txt" | head -n 4; echo "--"
167 printf "com_prefs()\n$gsu_prefs_txt" | head -n 4; echo "--"
168 grep -A 2 "$gsu_command_regex" $0
169 } | grep -v -- '--' \
170 | sed -e "/$gsu_command_regex/bs" \
171 -e 'H;$!d;x;s/\n//g;b' \
173 -e 'x;s/\n//g;${p;x;}' \
174 | sed -e "s/${gsu_command_regex}#*/\1\t/" \
178 if test ${#a} -lt 8; then
184 echo "# Try $_gsu_self help <command> for info on <command>."
188 if test "$1" = "help"; then
193 if test "$1" = "man"; then
198 if test "$1" = "prefs"; then
199 echo "$gsu_prefs_txt"
204 if grep -q "^com_$1()" $0; then
205 sed -e "1,/^com_$1()$/d" -e '/^{/,$d' -e 's/^## *//' $0
208 _gsu_print_available_commands
210 ret=-$E_GSU_BAD_COMMAND
213 # Wrapper for bash's getopts.
215 # Aborts on programming errors such as missing or invalid option string. On
216 # success $result contains shell code that can be eval'ed. For each defined
217 # option x, the local variable o_x will be created when calling eval "$result".
218 # o_x contains true/false for options without an argument or the emtpy string/the
219 # given argument, depending on whether this option was contained in the "$@"
223 # gsu_getopts abc:x:y
225 # [[ $ret -lt 0 ]] && return
227 # [[ "$o_a" = "true ]] && echo "The -a flag was given"
228 # [[ -n "$o_c" ]] && echo "The -c option was given with arg $o_c"
231 local i c tab=' ' cr='
234 gsu_check_arg_count $# 1 1
235 if [[ $ret -lt 0 ]]; then
241 result="invalid optstring $1"
242 if [[ -z "$1" ]] || grep -q '::' <<< "$1" ; then
247 for ((i=0; i < ${#1}; i++)); do
253 result="invalid character $c in optstring"
259 for ((i=0; i < ${#1}; i++)); do
261 c2=${1:$(($i + 1)):1}
263 if [[ "$c2" = ":" ]]; then
271 while getopts $1 opt \"\$@\"; do
274 for ((i=0; i < ${#1}; i++)); do
276 c2=${1:$(($i + 1)):1}
277 result+="$tab$tab$c1) o_$c1="
278 if [[ "$c2" = ":" ]]; then
279 result+="\"\$OPTARG\""
289 result=\"invalid option given\"
294 shift \$((\$OPTIND - 1))
303 _gsu_available_commands
305 if test $# -eq 0; then
307 _gsu_print_available_commands
312 # check internal commands
313 if [[ "$arg" = "help" || "$arg" = "man" || "$arg" = "prefs" ]]; then
315 if [[ "$ret" -lt 0 ]]; then
323 for i in $gsu_cmds; do
324 if test "$arg" = "$i"; then
326 if [[ "$ret" -lt 0 ]]; then
334 ret=-$E_GSU_BAD_COMMAND
337 _gsu_print_available_commands
341 # Check number of arguments.
343 # Usage: gsu_check_arg_count <num_given> <num1> [<num2>]
345 # Check that <num_given> is between <num1> and <num2> inclusively.
346 # If only <num1> ist given, num2 is assumed to be infinity.
349 # 0 0 no argument allowed
350 # 1 1 exactly one argument required
351 # 0 2 at most two arguments admissible
352 # 2 at least two arguments reqired
354 gsu_check_arg_count()
356 ret=-$E_GSU_BAD_ARG_COUNT
357 if [[ $# -eq 2 ]]; then # only num1 is given
358 result="at least $2 args required, $1 given"
359 [[ $1 -lt $2 ]] && return
363 # num1 and num2 given
364 result="need at least $2 args, $1 given"
365 [[ $1 -lt $2 ]] && return
366 result="need at most $3 args, $1 given"
367 [[ $1 -gt $3 ]] && return