-
Notifications
You must be signed in to change notification settings - Fork 1
/
docopt.sh
executable file
·116 lines (99 loc) · 2.81 KB
/
docopt.sh
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
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
## Helper script to autogenerate getopts code out of the target script's help message
## The script must contain a usage description with '##' at the beginning of each line (that is, like this one)
##
## usage: getopt.sh [options]
##
## options:
## -s <script_path> The path to the script to be parsed
## -d enable debug logs [default:0]
#
# Changelog
# 2017-07-09
# - Added ability to parse and apply default values in flag's description
# 2017-01-20
# - script's output is copied in system clipboard
# 2016-12-21
# - added helper case
# - added support to boolean flags
# 2016-12-21 created
which xclip > /dev/null
do_xclip=$?
[ $# -eq 0 ] && sed -ne 's/^## \(.*\)/\1/p' $0 && exit 1
while getopts 'hs:d' OPT; do
case $OPT in
h)
sed -ne 's/^## \(.*\)/\1/p' $0
exit 1
;;
s)
_script_path=$OPTARG
;;
d)
_d=1
;;
\?)
echo "---"
sed -ne 's/^## \(.*\)/\1/p' $0
exit 1
;;
esac
done
dlog(){
[ ! -z $_d ] && echo $@
}
varsfile=$(mktemp /tmp/varsfile.XXX)
sed -n 's_^##\s*-\(.*\)_\1_p' $_script_path | sed -n 's|\(\w\)\s*<\(\w\+\)>|\1: _\2=$OPTARG|p' | cut -d ' ' -f1,2 > $varsfile
sed -n 's_^##\s*-\(.*\)_\1_p' $_script_path | sed -n '/\w\s*<\w\+>/! s|\(\w\)|\1 _\1=1|p' | cut -d ' ' -f1,2 >> $varsfile
flaglist=`cut -d ' ' -f1 $varsfile | tr -d '\n'`
variables=`cut -d ' ' -f2 $varsfile`
defaults=$(mktemp /tmp/defaults.XXX)
sed -n 's_^##\s*-\(.*\)_\1_p' $_script_path | sed -n 's|\(\w\)\s*<\(\w\+\)>\s*.*\[default:\s*\(.*\)\]|_\2=\3|p' > $defaults
sed -n 's_^##\s*-\(.*\)_\1_p' $_script_path | sed -n '/\w\s*<\w\+>/! s|\(\w\)\s*.*\[default:\s*\(.*\)\]|_\1=\2|p' >> $defaults
dlog content of varsfile
[ ! -z $_d ] && cat $varsfile && echo
dlog content of defaults
[ ! -z $_d ] && cat $defaults && echo
exec 5<&1
exec 1> ./tmpfile
if [ $(cat $defaults | wc -l) -gt 0 ]; then
echo "# Default values"
cat $defaults
fi
cat << EOF
# No-arguments is not allowed
[ \$# -eq 0 ] && sed -ne 's/^## \(.*\)/\1/p' \$0 && exit 1
# Parsing flags and arguments
while getopts 'h${flaglist}' OPT; do
case \$OPT in
h)
sed -ne 's/^## \(.*\)/\1/p' \$0
exit 1
;;
EOF
IFS=$'\n' # make newlines the only separator
for j in $(cat $varsfile)
do
flag=$(echo $j | cut -c1)
var=$(echo $j | cut -d' ' -f2)
cat << EOF
$flag)
$var
;;
EOF
done
cat << EOF
\?)
echo "---"
sed -ne 's/^## \(.*\)/\1/p' \$0
exit 1
;;
esac
done
EOF
# Show result in stdout
cat ./tmpfile >&5
# Copy result in system clipboard
cat ./tmpfile | xclip
cat ./tmpfile | xclip -sel clip