|
a |
|
b/tools/argparse.bash |
|
|
1 |
#!/usr/bin/env bash |
|
|
2 |
|
|
|
3 |
# Use python's argparse module in shell scripts |
|
|
4 |
# |
|
|
5 |
# The function `argparse` parses its arguments using |
|
|
6 |
# argparse.ArgumentParser; the parser is defined in the function's |
|
|
7 |
# stdin. |
|
|
8 |
# |
|
|
9 |
# Executing ``argparse.bash`` (as opposed to sourcing it) prints a |
|
|
10 |
# script template. |
|
|
11 |
# |
|
|
12 |
# https://github.com/nhoffman/argparse-bash |
|
|
13 |
# MIT License - Copyright (c) 2015 Noah Hoffman |
|
|
14 |
# |
|
|
15 |
# The MIT License (MIT) |
|
|
16 |
# |
|
|
17 |
# Copyright (c) 2015 Noah Hoffman |
|
|
18 |
# |
|
|
19 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
|
20 |
# of this software and associated documentation files (the "Software"), to deal |
|
|
21 |
# in the Software without restriction, including without limitation the rights |
|
|
22 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
|
23 |
# copies of the Software, and to permit persons to whom the Software is |
|
|
24 |
# furnished to do so, subject to the following conditions: |
|
|
25 |
# |
|
|
26 |
# The above copyright notice and this permission notice shall be included in |
|
|
27 |
# all copies or substantial portions of the Software. |
|
|
28 |
# |
|
|
29 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
|
30 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
|
31 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
|
32 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
|
33 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
|
34 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
|
35 |
# THE SOFTWARE. |
|
|
36 |
|
|
|
37 |
argparse(){ |
|
|
38 |
argparser=$(mktemp 2>/dev/null || mktemp -t argparser) |
|
|
39 |
cat > "$argparser" <<EOF |
|
|
40 |
from __future__ import print_function |
|
|
41 |
import argparse |
|
|
42 |
import os |
|
|
43 |
import sys |
|
|
44 |
|
|
|
45 |
|
|
|
46 |
class MyArgumentParser(argparse.ArgumentParser): |
|
|
47 |
def print_help(self, file=None): |
|
|
48 |
"""Print help and exit with error""" |
|
|
49 |
super(MyArgumentParser, self).print_help(file=file) |
|
|
50 |
sys.exit(1) |
|
|
51 |
|
|
|
52 |
parser = MyArgumentParser(prog=os.path.basename("$0"), |
|
|
53 |
description="""$ARGPARSE_DESCRIPTION""") |
|
|
54 |
EOF |
|
|
55 |
|
|
|
56 |
# stdin to this function should contain the parser definition |
|
|
57 |
cat >> "$argparser" |
|
|
58 |
|
|
|
59 |
cat >> "$argparser" <<EOF |
|
|
60 |
args = parser.parse_args() |
|
|
61 |
for arg in [a for a in dir(args) if not a.startswith('_')]: |
|
|
62 |
key = arg.upper() |
|
|
63 |
value = getattr(args, arg, None) |
|
|
64 |
|
|
|
65 |
if isinstance(value, bool) or value is None: |
|
|
66 |
print('{0}="{1}";'.format(key, 'yes' if value else '')) |
|
|
67 |
elif isinstance(value, list): |
|
|
68 |
print('{0}=({1});'.format(key, ' '.join('"{0}"'.format(s) for s in value))) |
|
|
69 |
else: |
|
|
70 |
print('{0}="{1}";'.format(key, value)) |
|
|
71 |
EOF |
|
|
72 |
|
|
|
73 |
# Define variables corresponding to the options if the args can be |
|
|
74 |
# parsed without errors; otherwise, print the text of the error |
|
|
75 |
# message. |
|
|
76 |
if python "$argparser" "$@" &> /dev/null; then |
|
|
77 |
eval $(python "$argparser" "$@") |
|
|
78 |
retval=0 |
|
|
79 |
else |
|
|
80 |
python "$argparser" "$@" |
|
|
81 |
retval=1 |
|
|
82 |
fi |
|
|
83 |
|
|
|
84 |
rm "$argparser" |
|
|
85 |
return $retval |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
# print a script template when this script is executed |
|
|
89 |
if [[ $0 == *argparse.bash ]]; then |
|
|
90 |
cat <<FOO |
|
|
91 |
#!/usr/bin/env bash |
|
|
92 |
|
|
|
93 |
source \$(dirname \$0)/argparse.bash || exit 1 |
|
|
94 |
argparse "\$@" <<EOF || exit 1 |
|
|
95 |
parser.add_argument('infile') |
|
|
96 |
parser.add_argument('-o', '--outfile') |
|
|
97 |
|
|
|
98 |
EOF |
|
|
99 |
|
|
|
100 |
echo "INFILE: \${INFILE}" |
|
|
101 |
echo "OUTFILE: \${OUTFILE}" |
|
|
102 |
FOO |
|
|
103 |
fi |