Last active
December 1, 2021 19:26
-
-
Save csev1755/b7f863aa6f1c8ff6428741e8f6650f9f to your computer and use it in GitHub Desktop.
Quick and basic argument parsing in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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