浏览代码

Add option to add clarifying message

After usage message, before exiting---that's the sweet spot.
Alois Mahdal 8 年前
父节点
当前提交
55c9541512
共有 1 个文件被更改,包括 11 次插入2 次删除
  1. 11
    2
      src/include-bash/pretty.sh

+ 11
- 2
src/include-bash/pretty.sh 查看文件

122
     #
122
     #
123
     # Echo out usage patterns and (by default) `exit 2`
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
     # Each pattern is prefixed by "usage: " and a resolved
127
     # Each pattern is prefixed by "usage: " and a resolved
128
     # script/function name to produce traditional usage hint.
128
     # script/function name to produce traditional usage hint.
135
     # that would be fed to `exit` will be used as exit status
135
     # that would be fed to `exit` will be used as exit status
136
     # of this function.
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
     # Use "--" to delimit end of arguments processed by mkusage.
142
     # Use "--" to delimit end of arguments processed by mkusage.
139
     #
143
     #
140
     # Recommended usage is to define usage() in your script and
144
     # Recommended usage is to define usage() in your script and
141
     # use this in its body.  That way you only need to define
145
     # use this in its body.  That way you only need to define
142
     # usage patterns once and skip to them from any place where
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
     local es=$EXIT_USAGE    # our exit status
151
     local es=$EXIT_USAGE    # our exit status
146
     local doexit=true       # should we exit?
152
     local doexit=true       # should we exit?
153
+    local cmsg              # clarification message
147
     while true; do case "$1" in
154
     while true; do case "$1" in
148
         -e) es="$2";        shift 2 || return 2 ;;
155
         -e) es="$2";        shift 2 || return 2 ;;
149
         -E) doexit=false;   shift ;;
156
         -E) doexit=false;   shift ;;
150
         -k) es=$EXIT_OK;    shift ;;
157
         -k) es=$EXIT_OK;    shift ;;
158
+        -w) cmsg="$2";      shift 2 || return 2 ;;
151
         --)                 shift; break ;;
159
         --)                 shift; break ;;
152
         *)                  break ;;
160
         *)                  break ;;
153
     esac done
161
     esac done
154
     _pretty__echo -u "$@";
162
     _pretty__echo -u "$@";
163
+    test -n "$cmsg" && warn "bad usage: $cmsg"
155
     $doexit && exit "$es"
164
     $doexit && exit "$es"
156
     return "$es"
165
     return "$es"
157
 }
166
 }