forked from docopt/docopts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestee.sh
executable file
·68 lines (64 loc) · 2.41 KB
/
testee.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
#!/bin/bash
#
# Testee script for docopts. This script reads an arbitrary docstring from
# standard input and uses it to parse whatever arguments are passed to it
# into a Bash 4 associative array, which is then dumped in JSON format.
#
# Pass this file as an argument to `language_agnostic_tester.py` to test
# docopts. At the moment of writing this (2012-10-16) docopts fails the
# Naval Fate test, as there is no way to determine from just the array
# keys if an option is repeatable or accepts an integer argument; thus,
# both `--speed=2` and `--speed --speed` map to `"--speed": 2`.
#
# There is currently no way to automatically test the operation mode of
# docopts that name-mangles elements into Bash variables, as this
# transformation cannot be deterministically reversed into a format
# language_agnostic_tester.py expects.
#
# To test docopts with different Python versions, set the `PYTHON` variable:
#
# PYTHON=/usr/bin/python3.2 language_agnostic_tester.py testee.sh
#
# Note that `language_agnostic_tester.py` itself is only compatible with
# Python 2.7.
script=$(${PYTHON:-python} ./docopts -A args - '' -- "$@" < /dev/stdin)
if [[ $(tail -n 1 <<< "$script") =~ ^exit\ [0-9]+$ ]] ; then
echo '"user-error"'; exit
fi
shopt -s extglob
eval "$script"
echo -n '{'
for key in "${!args[@]}" ; do
# if the key is not part of a fake nested array,
# print it as-is
if [[ -z "${args[${key%,*},#]}" ]] ; then
[[ -z $sep ]] && sep=, || echo $sep
value=${args[$key]}
case "$value" in
'') echo -n "\"$key\": null";;
+([0-9])) if [[ "${key^^}" == "$key" ]] || [[ "$key" == \<?*\> ]]
then
echo -n "\"$key\": \"$value\""
else
echo -n "\"$key\": $value"
fi;;
true|false) echo -n "\"$key\": $value";;
*) echo -n "\"$key\": \"$value\"";;
esac
# if the key is the length key of a fake nested array,
# print the whole array
elif [[ "${key: -2:2}" == ',#' ]] ; then
[[ -z $sep ]] && sep=, || echo $sep
key=${key%,*}
n=${args[$key,#]}
i=0
echo -n "\"$key\": ["
while [[ $i -lt $n ]] ; do
[[ $i -gt 0 ]] && echo -n ', '
echo -n "\"${args[$key,$i]}\""
i=$[$i+1]
done
echo -n ']'
fi
done
echo '}'