Created
December 11, 2016 15:55
-
-
Save develmaycare/4bbe38e2829e93d4e9da074dae25d2cd to your computer and use it in GitHub Desktop.
Using Python argparse to create subparsers, allowing sub commands similar to pip, git, and brew.
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 | |
def main(): | |
"""Do something important.""" | |
parser = argparse.ArgumentParser(description=main.__doc__) | |
subparsers = parser.add_subparsers( | |
dest="subcommand", | |
metavar="sub1, sub2, sub3", | |
help='Sub-Commands' | |
) | |
sub1 = subparsers.add_parser( | |
"sub1", | |
help="Work with sub1 commands." | |
) | |
sub1.add_argument( | |
"--init", | |
action="store_true", | |
dest="initialize_sub1", | |
help="Initialize something related to sub1." | |
) | |
args = parser.parse_args() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment