Skip to content

Instantly share code, notes, and snippets.

@justinmassiot
Last active December 25, 2023 19:31
Show Gist options
  • Save justinmassiot/c3f54aafb38ae4e73a31393f9fdcb6ee to your computer and use it in GitHub Desktop.
Save justinmassiot/c3f54aafb38ae4e73a31393f9fdcb6ee to your computer and use it in GitHub Desktop.
**Advanced usage of command line switches (a.k.a. arguments) thanks to the 'argparse' library.** The help message of this script should look like this: ``` usage: argparse.py [-h] [-r RECIPES] [-p] [-q] [-l LOGFILE] client A beautiful description.
import argparse
# command line options
# the help message shows the default values
argParser = argparse.ArgumentParser( \
formatter_class=argparse.ArgumentDefaultsHelpFormatter, \
description='''A beautiful description.''' \
)
argParser.add_argument('client', help='Reference code of the target client. Must exist in the mapping dictionary.') # mandatory
argParser.add_argument('-r', '--recipes', help='JSON file containing the recipes for pins renaming. Must contain one section by client.', default='CLIENTS_Remap_Part-PinNames.json')
argParser.add_argument('-p', '--progress', help='Show the progress of the process. Add more \'p\' to get a finer grain progress animation.', action='count', default=0) # https://stackoverflow.com/a/8335918
argParser.add_argument('-q', '--quiet', help='Errors will not be printed to stderr.', action='store_true')
argParser.add_argument('-l', '--logfile', help='Log all matches and effective replacements to the given file.')
args = argParser.parse_args()
# 'args.progress' will contain an integer value, or zero if not specified
# -p => 1
# -pppp => 4
# --progress --progress => 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment