Is there a way to control the whitespace of the help-text that argparse
prints to stdout?
The example help-text at https://docs.python.org/3/howto/argparse.html are all short, and I couldn't find any verbiage describing how to handle formatting for long strings.
The only thing that came to mind was "\n
" - but base on my attempt, that seems to be stripped outright:
#! /usr/bin/python3
import argparse
import json
def parseArgs():
"""
To learn argparse.
"""
parser = argparse.ArgumentParser()
parser.add_argument('--json_args', required = False, nargs = '?',
default = '{}',
help = """JSON-encapsulated key/value optargs\n
e.g. {"foo": "bar"}
"""
)
a, _ = parser.parse_known_args()
j = json.loads(a.json_args)
r = {}
r['secret'] = 'open_sesame'
for k, v in j.items():
r[k.lower()] = v
return r
def main():
args = parseArgs()
print(json.dumps(args))
if __name__ == '__main__':
try:
main()
except Exception as e:
print(e)
sys.exit(1)
$ python3 ./a.py --help
usage: a.py [-h] [--json_args [JSON_ARGS]]
optional arguments:
-h, --help show this help message and exit
--json_args [JSON_ARGS]
JSON-encapsulated key/value optargs e.g. {"foo":
"bar"}