aboutsummaryrefslogtreecommitdiff
path: root/examples/debug_output.sh
blob: b3ecec27afc98f716c46f00efca80a78fb2e940a (plain)
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
#!/bin/sh
#
# This script does the very simple job of echoing some text. If a '-d' (or
# '--debug') flag is given, additinal "debug" output is enabled.
#
# This script demonstrates the use of a boolean flag to enable custom
# functionality in a script.
#
# Try running these:
# $ ./debug_output.sh speak
# $ ./debug_output.sh sing
# $ ./debug_output.sh --debug sing

# source shflags
. ../src/shflags

# define flags
DEFINE_boolean 'debug' false 'enable debug mode' 'd'
FLAGS_HELP=`cat <<EOF
commands:
  speak:  say something
  sing:   sing something
EOF`


debug()
{
  [ ${FLAGS_debug} -eq ${FLAGS_TRUE} ] || return
  echo "DEBUG: $@" >&2
}

die() {
  [ $# -gt 0 ] && echo "error: $@" >&2
  flags_help
  exit 1
}


# parse the command-line
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"

command=$1
case ${command} in
  '') die ;;

  speak)
    debug "I'm getting ready to say something..."
    echo 'The answer to the question "What is the meaning of life?" is "42".'
    ;;

  sing)
    debug "I'm getting ready to sing something..."
    echo 'I love to sing! La diddy da dum!'
    ;;

  *) die "unrecognized command (${command})" ;;
esac