Parameters parsing for python applications

command line arguments is a standard and one of the most common ways to pass parameters to a python script. There exist a list of python libraries that help with that task. Here I am going to list some of them. argparse The default choice for the python developer. The module is included in python standard library and comes together with any python distribution. Example of usage: #!/usr/bin/env bash import argparse def hello(count, name): """Simple program that greets NAME for a total of COUNT times....

April 21, 2020 · SergeM

Argparse recipies

How to include default values in ‘–help’ 1 2 3 parser = argparse.ArgumentParser( # ... other options ... formatter_class=argparse.ArgumentDefaultsHelpFormatter) Output looks like this: 1 2 3 --scan-time [SCAN_TIME] Wait SCAN-TIME seconds between status checks. (default: 5) Another tip: add ‘%(default)’ to the help parameter to control what is displayed. 1 2 parser.add_argument("--type", default="toto", choices=["toto","titi"], help = "type (default: %(default)s)") source

June 24, 2017 · SergeM