-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare_flags.py
executable file
·48 lines (42 loc) · 1.48 KB
/
prepare_flags.py
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
#! /bin/env python
#
# prepare_flags.py: Utility to create the list of flags supported by target progrem (jpegoptim in this case).
#
import argparse
import subprocess
import re
import json
ignore_flags = ['dest', 'stdout', 'stdin', 'overwrite', 'help', 'threshold', 'version']
def main(args):
cmd = ('jpegoptim', '--help')
cp = subprocess.run(cmd, capture_output=True, text=True)
# lines = cp.stderr.split('\n')
lines = cp.stderr.split('--')
flags = {}
for line in lines[1:]:
values = {}
key,help = re.split('-.+,',line,maxsplit=1)[0].split(maxsplit=1)
if key.find('=') != -1:
key = key.split('=')[0]
values['type'] = 'int'
values['default'] = 999
else:
values['type'] = 'bool'
values['action'] = 'store_true'
values['default'] = False
if key not in ignore_flags:
values['help'] = re.subn('\n *', ' ', help)[0].rstrip()
values['dest'] = key
flags[key] = values
if args.doc:
for f,v in flags.items():
print(' [--{}]'.format(f))
print(' {}'.format(v['help']))
else:
with open('flags.json', 'w') as fp:
json.dump(flags, fp, indent=4)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--doc", action='store_true', help="Generate documentation in format appropiate to add to README.rst")
args = parser.parse_args()
main(args)