-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclparser.1
More file actions
346 lines (346 loc) · 10.5 KB
/
clparser.1
File metadata and controls
346 lines (346 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
.TH clparser 1 "29 May, 2023"
.
.SH NAME
clparser - command line argument parser for shells
.
.SH SYNOPSIS
.\" .SY clparser
.\" .RI [ OPTIONS ]
.\" .B --
.\" .I COMMAND_LINE_ARGS
.\" [\&\.\|.\|.\&]
.\" .YS
.\" .
.SY clparser
.\" .OP \-A|\-\-override-argv
.RB { \-A | \-\-override-argv }
.\" .OP \-a|\-\-maintain-argv
.RB { \-a | \-\-maintain-argv }
.\" .OP \-S|\-s|\-\-shell shell
.RB { \-S | \-s | \-\-shell
.IR shell }
.RB { \-H | \-m | \-\-help\-msg
.IR helpmessage }
.B \-\-
.RI [ COMMAND_LINE_ARGS
\&.\|.\|.\&]
.YS
.
.SY clparser
.BR \-\-help | \-h
.YS
.
.SH DESCRIPTION
.B clparser
takes in command line arguments to parse as command line arguments and a specification for which command line arguments should exist from stdin, and outputs shell code to set variables set from the command line arguments to stdout.
Command line arguments to be parsed should be given after
.BR -- .
This will prevent the command line arguments to be parsed from being interpreted as
.B clparser
command line arguments.
.
.SS Argument Specification Format
The format specifies
.I flags
for setting some optional value to true and
.I parameters
for setting some (optional) value to the value passed in the command line arguments.
There are also
.I positionals
for setting some parameter values (like
.IR parameters )
through named positional arguments rather than specifying the name through
.B \-n
or
.BR \-\-name .
Arguments as specified as flags, parameters, or positionals by being listed after
.IR flags: ,
.IR parameters: ,
or
.IR positionals: ,
respectively.
Arguments are defined as in a space separated list, ended with a semicolon
.RI ( ; ).
E.g.:
.IR "flags: f help test;" .
Arguments to be set together are listed together in a comma separated list.
E.g.:
.IR "flags: f,flags help,h test,t,T;" .
.I positionals
specify the positional arguments in the order given after the
.I positionals:
keyword.
.PP
Parameters (and positionals) can have default values.
Default values set the value of the parameters if the parameter is not set with the input command line arguments.
Default values are set by appending an equals
.RI ( = )
after the parameters (e.g.:
.IR line,l=default ).
.PP
Flags can specify other flags that would negate its value if both are defined.
When a flag with a negation is input and its negation flag is input after it, then the output of the first flag will be false because the second value (set to true) negated it.
E.g.: if
.B \-i
has
.B \-o
set as a negation flag, then the following input arguments
.B \-i \-\-param
.I value
.B \-o
will set set
.B i
to false and
.B o
to true.
This can be helpful, especially when users alias a script with arguments that the user might want to override (imagine
.B \-i
was set in the alias and
.B \-\-param
.I value
.B \-o
was set by the user).
Negation flags can be set by appending equals minus
.RB ( =- )
after flags (e.g.:
.BR i,in=-out ).
Multiple negation flags can be given (e.g.:
.BR i,in=-out=-h ),
which would reset the value of
.B i
if
.B \-\-out
or
.B \-h
are specified after
.BR \-i .
.PP
For a full example, see the
.B EXAMPLES
section below.
.
.SS Parsing Features
There are 2 different types of command line arguments: words and single letters.
Words are specified after 2 dashes
.RB ( -- ),
letters are specified after 1 dash
.RB ( - ).
Letters can be combined on the same input (e.g.:
.BR \-aBc ).
Word parameters can have dashes
.RB ( - )
in them.
These words will be output with underscores
.RB ( _ )
instead of dashes to make a valid variable name.
.PP
Single letter parameters can be used in the combined format if they are the last letter and the value of the parameter is given as the next argument (e.g.:
.BR "\-aBc value" ),
where
.B c
is a parameter, and
.B a
and
.B B
are flags.
.PP
Word parameters can have the value specified in the same argument after an equals
.RB ( = )
(e.g.:
.B \-\-word=\c
.IR value ).
.PP
Positional arguments are defined as values that don't start with \- and do not follow parameters.
If a
.B --
is found, all arguments after it are interpreted as positional arguments.
.SS Description of Output
.B clparser
outputs shell syntax to set variables to the values of the parameters.
By default, the shell will try to choose the shell syntax from the name of the calling process (found in
.RI /proc/ ppid /comm).
If the shell has associative arrays (maps, hash tables, dicts, etc.),
.I flags
are set in an associative array called
.BR flags ,
and
.I parameters
are set in an associative array called
.BR params .
`sh`, `csh`, and `fish` do not have associative arrays, so flags and parameters are just set as variables.
Positional arguments are set in an array.
`sh` does not have arrays, so it overrides argv to place the positional arguments by default.
See the
.B OPTIONS
section below to see how to override these.
.PP
The output of parameter values and positional arguments is done through the existing positional arguments
.RB ( \(dq${\c
.I number\c
.BR }" )
to prevent issues with the shell interpreting whitespace or symbols in the parameters (see the
.B EXAMPLES
below for an example).
.
.SH OPTIONS
.BR \-h ,
.B \-\-help
Print help info to stderr.
Starts with options showing flags and params in usage for.
Parameters also include the value for the user to input based on the name of the parameter.
If parameters have default values, it shows it after an =.
Under the usage line, it shows lines for each argument with a specified help message (see \-\-help\-msg below).
.PP
.BR \-A ,
.B \-\-override\-argv
Output positional arguments in place of the shell's argv.
This may be useful in some cases, but most shells have arrays to put the arguments in.
This is used by default for `sh` since "$@" is the only way of splitting arguments by argument while maintaining whitespace.
.PP
.BR \-a ,
.B \-\-maintain\-argv
Output positional arguments in a variable named
.B args
instead of overriding argv.
This will allow the original argv to be analyzed and used after parsing arguments.
This will use an array for all shells other than `sh`, since it does not have arrays.
This is not a good option for `sh` since "$@" is the only way of splitting arguments by argument while maintaining whitespace.
.PP
.BR \-n ,
.B \-\-namespace
Namespace args by putting them in associative arrays when available or prepending flags_ or params_ to the variable names.
This option is on by default for shells with associative arrays.
.PP
.BR \-N ,
.B \-\-no\-namespace
Do not namespace args: set variable names directly, allowing flags and parameters to overlap.
This option is on by default for shells without associative arrays.
.PP
.BR \-E ,
.B \-\-no\-output\-empty
Do not output parameters that do not have a value (not specified on command line and no default value).
This can be helpful for a flag and parameter of the same name.
E.g.: a choose flag to indicate to use some user input, but also have a parameter of the same name that takes the user input.
This is only useful for shells that won't error on using undeclared variables that don't use associative arrays (sh and fish) (and namespace isn't used) (or no-namespace is used).
.PP
.BR \-u ,
.BR \-P ,
.B \-\-unknown\-positional
Rather than error on unknown arguments, assume all further arguments are positional.
This is basically allowing users to not need to pass in
.B --
to allow the rest of the arguments to be positional.
.PP
.BR \-e ,
.B \-\-help\-exits
The help option will automatically exit.
This is done by printing `exit` to stdout to be evaluated.
.PP
.BR \-s ,
.BR \-S ,
.B \-\-shell
.I shell
Specify the shell language syntax to be used.
By default, the shell will try to choose the shell syntax from the name of the calling process.
.PP
.BR \-p ,
.B \-\-prog\-name
.I prog_name
Specify the name of the script to use in the usage message.
By default, the shell will try to choose the path of the script of the calling process (the first argument in the cmdline).
This is taken from
.RI /proc/ ppid /cmdline.
.PP
.BR \-H ,
.BR \-m ,
.B \-\-help\-msg
.I helpmsgs
Specify help messages for specified flags/parameters.
The format is
.BR "argument = help message" ,
with argument, message pair per line.
See the
.B EXAMPLES
section below for an example format and output.
.
.SH EXIT STATUS
Exit with success (0) if the arguments and specification are parsed successfully.
Exit with code 1 if outputting help.
Exit with code 1 if there are not alphanumeric (or _ or \-) characters in input parameters.
Exit with code 2 if the passed argument was not found in the specification.
Exit with code 3 if parsing the specification string failed.
Exit with code 2 if there were neither flags or parameters defined in the specification string.
.PP
Yes, these should be separated better.
.
.SH EXAMPLES
Run
.B clparser
in POSIX compliant shells using
.EX sh
spec='flags: f,flag=-g=-h g=-h=-qwerty qwerty=-flag=-h=-g h,help; parameters: q,asdf u=defval nothing zzz,z,Z=someth;'
helpmsg='
help = print this help message
flag = a random flag
g = i dunno, its nonsense
qwerty = a keyboard layout
asdf = q param
u = something
zzz = a bunch of zs
\(aq
eval "$(echo "$spec" | clparser -- "$@")"
.EE
This will output code that looks like
.EX sh
flag=false
f=false
h=false
help=false
g=false
qwerty=true
asdf="${2}"
q="${2}"
Z='test'
zzz='test'
z='test'
u='defval'
nothing="${8}"
set -- "${4}" "${6}" "${10}" "${11}"
.EE
if the user called the script with the arguments
.EX
-fgq whatever --qwerty 'i dunno' --zzz=test words --nothing else -- --test hi
.EE
.PP
For `csh`, call
.B clparser
in this format:
.EX csh
eval `echo $spec:q | ./clparser --help-msg $helpmsg:q -- $argv:q`
.EE
Note that just using
.B $argv
will not preserve whitespace, which will cause bugs, so appending `:q` at the end of variable names is necessary.
.B clparser
and evaluate the resulting `csh` code.
.PP
See `fish` and `xonsh` example syntax in the github repo example files.
For `xonsh`, don't bother using this; just use
.BR argparse .
.PP
The
.B \-\-help
output will look like
.EX
Usage: "${0}" [ --flag -f ] [ -h --help ] [ -g ] [ --qwerty ] --asdf -q asdf [ -Z --zzz -z zzz = someth ] [ -u u = defval ] --nothing nothing
[ -h --help ] print this help message
[ --flag -f ] a random flag
[ -g ] i dunno, its nonsense
[ --qwerty ] a keyboard layout
--asdf -q asdf q param
[ -u u = defval ] something
[ -Z --zzz -z zzz = someth ] a bunch of zs
.EE
.PP
.B TODO
add examples of output for `bash` and `csh` and show use for `fish`.