Parcourir la source

Enable adding meta command help text

New variable SATURNIN_META_HELP can be used to control what happens when
user calls the meta command with `--help` argument.
Alois Mahdal il y a 7 ans
Parent
révision
4eb96c584c
1 fichiers modifiés avec 64 ajouts et 1 suppressions
  1. 64
    1
      src/saturnin.sh.skel

+ 64
- 1
src/saturnin.sh.skel Voir le fichier

@@ -68,6 +68,26 @@ SATURNIN_LIBEXEC="${SATURNIN_LIBEXEC:-}"
68 68
 #
69 69
 SATURNIN_LIBEXEC_PREFIX="${SATURNIN_LIBEXEC_PREFIX:-}"
70 70
 
71
+#
72
+# Meta-command help mode
73
+#
74
+# This controls what is displayed when user calls meta-command with --help
75
+# argument.  Following formats are supported:
76
+#
77
+#     +HELPFILE
78
+#     =HELPFILE
79
+#
80
+# In both cases, HELPFILE must be absolute path to a file containing
81
+# human-readable description of the meta-command, which will be directly
82
+# presented to user.
83
+#
84
+# If the leading character is '=' (equal sign), the help text consists of,
85
+# and only of the HELPFILE.  If the character is '+' (plus sign), the help
86
+# text is pre-pended with auto-generated usage message.
87
+#
88
+# If the value is empty, only the auto-generated usage message is printed.
89
+#
90
+SATURNIN_META_HELP="${SATURNIN_META_HELP:-}"
71 91
 
72 92
 saturnin__bug() {
73 93
     #
@@ -254,7 +274,7 @@ saturnin__main() {
254 274
     while true; do case $1 in
255 275
         -d|--debug)     export PRETTY_DEBUG=true;   shift ;;
256 276
         -v|--verbose)   export PRETTY_VERBOSE=true; shift ;;
257
-        -h|--help)      saturnin__usage -e 0;       exit  ;;
277
+        -h|--help)      saturnin__help;             exit  ;;
258 278
         --version)      saturnin__version;          exit  ;;
259 279
         --version-semver) saturnin__get app-version ;;
260 280
         --saturnin-get-*) saturnin__get "$1"        ;;
@@ -275,6 +295,49 @@ saturnin__main() {
275 295
     esac
276 296
 }
277 297
 
298
+_saturnin__cat_helpfile() {
299
+    #
300
+    # Print helpfile $1
301
+    #
302
+    local helpfile=$1   # path to help file
303
+    cat "$helpfile" >&2 && return 0
304
+    saturnin__bug "cannot print help file: $helpfile"
305
+    return 3
306
+}
307
+
308
+saturnin__help() {
309
+    #
310
+    # Print meta-command help text
311
+    #
312
+    # See $SATURNIN_META_HELP for details.
313
+    #
314
+    local introline     # introduction line
315
+    introline=$(basename "$0")
316
+    test -n "$SATURNIN_APP_TAGLINE" \
317
+     && introline+=" - $SATURNIN_APP_TAGLINE"
318
+    case "$SATURNIN_META_HELP" in
319
+        "")
320
+            echo "$introline"$'\n' >&2
321
+            saturnin__usage -E -e 0
322
+            ;;
323
+        +/*)
324
+            echo "$introline"$'\n' >&2
325
+            saturnin__usage -E
326
+            echo >&2
327
+            _saturnin__cat_helpfile "${SATURNIN_META_HELP:1}"
328
+            ;;
329
+        =/*)
330
+            _saturnin__cat_helpfile "${SATURNIN_META_HELP:1}"
331
+            ;;
332
+        *)
333
+            echo "$introline"$'\n' >&2
334
+            saturnin__usage -E
335
+            saturnin__bug "malformed SATURNIN_META_HELP: $SATURNIN_META_HELP"
336
+            return 3
337
+            ;;
338
+    esac
339
+}
340
+
278 341
 saturnin__conf_mkpath() {
279 342
     #
280 343
     # Assemble SATURNIN_CONF_PATH from locations $@