Created
July 9, 2022 18:00
-
-
Save kristopherjohnson/322b159c856b837dfcbb730db1fe2c70 to your computer and use it in GitHub Desktop.
Simple example of using the Python argparse module
This file contains hidden or 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
#!/usr/bin/env python3 | |
"""Tests/examples for the argparse module. | |
Run "python3 argparsetest.py -h" for help. | |
""" | |
from argparse import ArgumentParser | |
def main(): | |
"""Main function.""" | |
ap = ArgumentParser( | |
description='Test argument parsing', | |
epilog='Prints the results of argument parsing.' | |
) | |
ap.add_argument( | |
'--version', | |
action='version', version='%(prog)s 1.0' | |
) | |
ap.add_argument( | |
'-v', '--verbose', | |
action='count', default=0, | |
help='increase verbosity' | |
) | |
ap.add_argument( | |
'arg', nargs='*', | |
help='command parameters' | |
) | |
args = ap.parse_args() | |
print(vars(args)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment