-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.bash
More file actions
executable file
·41 lines (40 loc) · 1.12 KB
/
example.bash
File metadata and controls
executable file
·41 lines (40 loc) · 1.12 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
#!/usr/bin/env bash
# NOTE this file can be run with zsh and ksh just fine (clparser uses the same syntax for all 3)
make || exit
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; positionals: asdf test;'
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
'
#$help && exit
# print out the raw output
echo raw output:
vars="$(echo "$spec" | ./clparser -e --help-msg "$helpmsg" -- "$@")"
ec=$?
echo "$vars"
if [ "$ec" -ne 0 ]; then
echo exit code: $ec
exit $ec
fi
# actually run it and show results
echo variable results:
eval "$vars"
echo f,flag = "${flags[flag]}"
echo g = "${flags[g]}"
echo qwerty = "${flags[qwerty]}"
echo h,help = "${flags[help]}"
echo q,asdf = "${params[asdf]}"
echo u = "${params[u]}"
echo nothing = "${params[nothing]}"
echo zzz,z,Z = "${params[Z]}"
echo positional params:
echo asdf = "${positionals[asdf]}"
echo test = "${positionals[test]}"
printf 'positional args:'
printf " '%s'" "${args[@]}"
printf '\n'