Showing 1 changed files with 47 additions and 9 deletions
+47 -9
readconf
... ...
@@ -35,19 +35,35 @@ function print_version () {
35 35
     printf("%s version %s\n", appname, version)
36 36
 }
37 37
 
38
+function cli_error (rc) {
39
+    print "CLI error: " rc
40
+    exit 1
41
+}
42
+
38 43
 function help (rc) {
39
-    print_version()
40
-    if (rc != "") {
41
-        print "CLI error: " rc
42
-        exit 1
44
+    line = appname "\nversion " version "\n\n\033[4mOPTIONS:\033[0m"
45
+    while (getline < appname) {
46
+        if (/^#help:BEGIN/) help_started = 1
47
+        if (/^#help:END/) help_started = 0
48
+        if (help_started) {
49
+            if (/^[[:blank:]]*case \/\^(.+)\$\/[[:blank:]]*\:[[:blank:]]*$/) {
50
+                option = gensub(/^[[:blank:]]*case \/\^(.+)\$\/[[:blank:]]*\:[[:blank:]]*$/, "\\1", "1")
51
+                line = line "\n\n\033[1m" option "\033[0m"
52
+            }
53
+            else if (/^#help:/ && option != "") {
54
+                split($0, helpinfo, ":")
55
+                line = line "\n" helpinfo[2]
56
+            }
57
+        }
43 58
     }
59
+    print line | "pager -R"
44 60
     exit 0
45 61
 }
46 62
 
47 63
 BEGIN {
48 64
 # initialisation
49 65
     appname     = ENVIRON["_"]
50
-    version     = "0.9"
66
+    version     = "1.0.0"
51 67
     conf_arrays = ""
52 68
     filename    = ""
53 69
     sections    = ""
... ...
@@ -68,67 +84,89 @@ BEGIN {
68 84
 
69 85
     for (arg = 1; arg < ARGC; arg++)
70 86
         switch (ARGV[arg]) {
87
+#help:BEGIN
71 88
             case /^--conf=["']?.+["']?$/:
89
+#help:file containing the configuration to read
72 90
                 filename = gensub(/^--conf=["']?(.+)["']?$/, "\\1", "", ARGV[arg])
73 91
                 break
74 92
             case /^--conf$/:
93
+#help:file containing the configuration to read
75 94
                 filename = gensub(/^["']?(.+)["']?$/, "\\1", "", ARGV[++arg])
76 95
                 break
77 96
             case "--sections-list":
97
+#help:list all sections found in given config file
78 98
                 returnsectionlist = 1
79 99
                 break
80 100
             case /^--section=["']?.+["']?$/:
101
+#help:retrieve information for section <section>
81 102
                 s = gensub(/^--section=["']?(.+)["']?$/, "\\1", "", ARGV[arg])
82 103
                 onlysections[s] = true
83 104
                 break
84
-            case /^-s$/:
85
-                s = gensub(/^["']?(.+)["']?$/, "\\1", "", ARGV[++arg])
86
-                onlysections[s] = true
87
-                break
105
+#            case /^-s$/:
106
+##help:retrieve information for section <section>
107
+#                s = gensub(/^["']?(.+)["']?$/, "\\1", "", ARGV[++arg])
108
+#                onlysections[s] = true
109
+#                break
88 110
             case /^--clean$/:
111
+#help:output commands to clean all variables listed in configuration
89 112
                 clean = true
90 113
                 break
91 114
             case /^--unset$/:
115
+#help:output commands to unset variables listed in configuration file
92 116
                 unset = true
93 117
                 break
94 118
             case /^--compact$/:
119
+#help:output everything on a single line, elements seperated by semi-colon
95 120
                 eol = ";"
96 121
                 break
97 122
             case /^--array=["']?.+["']?$/:
123
+#help:learns the script this entry is intented to be treated as an array, can be used multiple times for multiples array names
98 124
                 arrayname=gensub(/^--array=["']?(.+)["']?$/, "\\1", "", ARGV[arg])
99 125
                 conf_arrays = conf_arrays "|" arrayname
100 126
                 key_array[arrayname]++
101 127
                 break
102 128
             case /^--first-element-array=[01]$/:
129
+#help:tells the script the array index begins at 0 or 1, defaults to 0
103 130
                 first_element_index = gensub(/^--first-element-array=([01])$/, "\\1", "", ARGV[arg])
104 131
                 break
105 132
             case /^--var-delim-begin=["']?.{3,}["']?$/:
133
+#help:tells the script what is the starting chars sequence to delimitate a variable, defaults to {{{, can be -+= (for example)
106 134
                 var_delimiters_begin = gensub(/^--var-delim-begin=["']?(.{3,})["']?$/, "\\1", "", ARGV[arg])
107 135
                 break
108 136
             case /^--var-delim-end=["']?.{3,}["']?$/:
137
+#help:tells the script what is the ending chars sequence to delimitate a variable, defaults to }}}, can be =+- (for example)
109 138
                 var_delimiters_end = gensub(/^--var-delim-end=["']?(.{3,})["']?$/, "\\1", "", ARGV[arg])
110 139
                 break
111 140
             case /^--case-sensitive$/:
141
+#help:tells the script if it has to be case-sensitive, defaults to insensitive
112 142
                  case_sensitive = true
113 143
                  break
114 144
             case /^--case-insensitive(|=lower|=UPPER)$/:
145
+#help:tells the script how to behave in insensitive mode (default)
146
+#help: lower = variables output in lowercase (default)
147
+#help: UPPER = variables output in UPPERCASE
115 148
                  case_sensitive = false
116 149
                  default_case = gensub(/^--case-insensitive[=]?(.*)$/, "\\1", "", ARGV[arg])
117 150
                  if (default_case == "")
118 151
                      default_case = "lower"
119 152
                  break
120 153
             case /^--key-value-delimiter=["']?.["']?$/:
154
+#help:tells the script which char is used to separate the key from its assorciated value in the configuration file, defaults to blank-char
121 155
                  key_value_delimiter = gensub(/^--key-value-delimiter=["']?(.)["']?$/, "\\1", "", ARGV[arg])
122 156
                  break
123 157
             case /^--in-var-space-replacement=["']?.["']?$/:
158
+#help:tells the script which char to use to replace a space found in a key, defaults to empty
124 159
                 in_var_space_remplacement = gensub(/^--in-var-space-replacement=["']?(.)["']?$/, "\\1", "", ARGV[arg])
125 160
                 break
126 161
             case /^help$/:
162
+#help:show this help
127 163
                  help()
128 164
                  break
129 165
             case /^version$/:
166
+#help:show the version of the script
130 167
                  print_version()
131 168
                  exit
169
+#help:END
132 170
             default:
133 171
                 help(ARGV[arg])
134 172
         }