Forked from ikkemaniac/python-argparse-template.py
Last active
August 10, 2020 02:41
-
-
Save samesense/c1cc2f83430bca5ba3391c6dcede518e to your computer and use it in GitHub Desktop.
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 | |
""" | |
The idea here is to have one demo of each common argparse format | |
type. This is useful for me to be able to copy/paste into a new | |
script and have something to quickly edit and trim down to get | |
the functionality I need. | |
Example (in your terminal): | |
$ python3 argparse-template.py "hello" 123 --enable | |
""" | |
import argparse | |
def cmdline_args(): | |
# Make parser object | |
p = argparse.ArgumentParser(description=__doc__, | |
formatter_class=argparse.RawDescriptionHelpFormatter) | |
p.add_argument("required_positional_arg", | |
help="desc") | |
p.add_argument("required_int", type=int, | |
help="req number") | |
p.add_argument("--on", action="store_true", | |
help="include to enable") | |
p.add_argument("-v", "--verbosity", type=int, choices=[0,1,2], default=0, | |
help="increase output verbosity (default: %(default)s)") | |
group1 = p.add_mutually_exclusive_group(required=True) | |
group1.add_argument('--enable',action="store_true") | |
group1.add_argument('--disable',action="store_false") | |
return(p.parse_args()) | |
def main(arg1, arg2): | |
pass | |
# Try running with these args | |
# | |
# "Hello" 123 --enable | |
if __name__ == '__main__': | |
args = cmdline_args() | |
main(*cmdline_args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment