Browse Source

Hack completion code together to simplify updates

Alois Mahdal 8 years ago
parent
commit
67bd2e79f6
2 changed files with 44 additions and 7 deletions
  1. 15
    5
      src/complete.bash
  2. 29
    2
      src/shellfu/saturnin.sh

+ 15
- 5
src/complete.bash View File

@@ -1,12 +1,15 @@
1 1
 #!/bin/bash
2 2
 
3
-__satcmd() {
3
+__SATURNIN_COMPLETE_CMDNAME=satcmd
4
+
5
+#shellcheck disable=SC2016
6
+__SATURNIN_COMPLETE_CODETMPL='
7
+__%s() {
4 8
     local cur opts sopts
5 9
     COMPREPLY=()
6 10
     cur="${COMP_WORDS[COMP_CWORD]}"
7 11
     sopts="-d -h -v"
8 12
     opts="--help --verbose --debug --version --version-semver"
9
-
10 13
     case "$cur" in
11 14
         -*)
12 15
             COMPREPLY=(
@@ -20,9 +23,16 @@ __satcmd() {
20 23
             ;;
21 24
         *)
22 25
             COMPREPLY=(
23
-                $(compgen -W "$(satcmd _ls_sc)" "${cur}")
26
+                $(compgen -W "$(%s _ls_sc)" "${cur}")
24 27
             )
25 28
     esac
26
-}
29
+}'
30
+
31
+eval "$(
32
+    #shellcheck disable=SC2059
33
+    printf "$__SATURNIN_COMPLETE_CODETMPL" \
34
+        $__SATURNIN_COMPLETE_CMDNAME \
35
+        $__SATURNIN_COMPLETE_CMDNAME
36
+)"
27 37
 
28
-complete -F __satcmd satcmd
38
+complete -F "__$__SATURNIN_COMPLETE_CMDNAME" "$__SATURNIN_COMPLETE_CMDNAME"

+ 29
- 2
src/shellfu/saturnin.sh View File

@@ -98,7 +98,7 @@ saturnin__conf() {
98 98
         *.*:*)  ig_mode=-e; ig_query=$1; shift 1; break ;;
99 99
         *)      warn "bad syntax: $*"; return 2 ;;
100 100
     esac done
101
-    test -n "$ig_mode" || { warn "could not determine inigrep mode"; return 2; }
101
+    test -n "$ig_mode" || { warn "could not determine inigrep mode"; _saturnin__conf_usage; }
102 102
     debug -v ig_limit ig_query ig_mode Strategy
103 103
     if test -n "$*";
104 104
     then
@@ -108,7 +108,7 @@ saturnin__conf() {
108 108
         files=("${ig_query%%.*}$SATURNIN_CONF_SUFFIX")
109 109
     else
110 110
         warn "dunno what to load"
111
-        return 2
111
+        _saturnin__conf_usage
112 112
     fi
113 113
     debug -v files
114 114
     #shellcheck disable=SC2086
@@ -355,6 +355,33 @@ _saturnin__conf__load() {
355 355
     true
356 356
 }
357 357
 
358
+_saturnin__conf_usage() {
359
+    #
360
+    #
361
+    #
362
+    mkusage "[options] [-e] SECTION.KEY [FILE...]"                             \
363
+            "[options] -r SECTION.KEY [FILE...]"                               \
364
+            "[options] -K SECTION [FILE...]"                                   \
365
+            "[options] -P [FILE...]"                                           \
366
+            "[options] -S [FILE...]"                                           \
367
+        -o                                                                     \
368
+            "-j     join all files before applying query"                      \
369
+            "-1     ensure single line is returned"                            \
370
+        -c                                                                     \
371
+            "-e     use normal mode (default)"                                 \
372
+            "-r     use raw mode (preserves RHS whitespace and some comments)" \
373
+            "-K     list available keys in SECTION"                            \
374
+            "-S     list available sections"                                   \
375
+            "-P     list available keypaths (SECTION.KEY)"                     \
376
+        --                                                                     \
377
+            "Uses inigrep to query config files.  SECTION is section name (may"\
378
+            "contain dots itself, KEY is key name.  Unless FILE is provided,"  \
379
+            "filename is inferred from SECTION (eg. 'foo.ini' if 'foo.bar' was"\
380
+            "sectionname) and searched in folders given in SATURNIN_CONF_PATH."\
381
+            "If FILE contains slash, no search is done and just single FILE is"\
382
+            "used instead."
383
+}
384
+
358 385
 _saturnin__nl2colon() {
359 386
     #
360 387
     # Convert newline-based list of paths to colon:based:list