Browse Source

Add option to add clarifying message

After usage message, before exiting---that's the sweet spot.
Alois Mahdal 8 years ago
parent
commit
55c9541512
1 changed files with 11 additions and 2 deletions
  1. 11
    2
      src/include-bash/pretty.sh

+ 11
- 2
src/include-bash/pretty.sh View File

@@ -122,7 +122,7 @@ mkusage() {
122 122
     #
123 123
     # Echo out usage patterns and (by default) `exit 2`
124 124
     #
125
-    #     mkusage [-e STATUS] [-E] pattern [patern...]
125
+    #     mkusage [-w MSG] [-e STATUS] [-E] pattern [patern...]
126 126
     #
127 127
     # Each pattern is prefixed by "usage: " and a resolved
128 128
     # script/function name to produce traditional usage hint.
@@ -135,23 +135,32 @@ mkusage() {
135 135
     # that would be fed to `exit` will be used as exit status
136 136
     # of this function.
137 137
     #
138
+    # Optionally, you can add -w MSG to add clarifying message
139
+    # (presented as warning) to help user understand what is
140
+    # wrong with arguments they have passed.
141
+    #
138 142
     # Use "--" to delimit end of arguments processed by mkusage.
139 143
     #
140 144
     # Recommended usage is to define usage() in your script and
141 145
     # use this in its body.  That way you only need to define
142 146
     # usage patterns once and skip to them from any place where
143
-    # you detect incorrect usage.
147
+    # you detect incorrect usage.  Optionally, this usage()
148
+    # function can pass first argument as -w for clarification
149
+    # messages.
144 150
     #
145 151
     local es=$EXIT_USAGE    # our exit status
146 152
     local doexit=true       # should we exit?
153
+    local cmsg              # clarification message
147 154
     while true; do case "$1" in
148 155
         -e) es="$2";        shift 2 || return 2 ;;
149 156
         -E) doexit=false;   shift ;;
150 157
         -k) es=$EXIT_OK;    shift ;;
158
+        -w) cmsg="$2";      shift 2 || return 2 ;;
151 159
         --)                 shift; break ;;
152 160
         *)                  break ;;
153 161
     esac done
154 162
     _pretty__echo -u "$@";
163
+    test -n "$cmsg" && warn "bad usage: $cmsg"
155 164
     $doexit && exit "$es"
156 165
     return "$es"
157 166
 }