Skip to content

Instantly share code, notes, and snippets.

@csev1755
Last active December 1, 2021 19:26
Show Gist options
  • Save csev1755/b7f863aa6f1c8ff6428741e8f6650f9f to your computer and use it in GitHub Desktop.
Save csev1755/b7f863aa6f1c8ff6428741e8f6650f9f to your computer and use it in GitHub Desktop.
Quick and basic argument parsing in Python
import argparse
parser = argparse.ArgumentParser()
# List of desired argument names
argnames = [
'argument1',
'argument2',
'argument3'
]
# Turn list into arguments
for x in argnames:
parser.add_argument(f'-{x}', dest=f"{x}", default='', type=str, nargs='?', const='')
# Create dictionary with the values of those arguments
argvalues = {}
for x in argnames:
argvalues[x] = getattr((parser.parse_args()), x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment