Skip to content

Instantly share code, notes, and snippets.

@develmaycare
Created December 11, 2016 15:55
Show Gist options
  • Save develmaycare/4bbe38e2829e93d4e9da074dae25d2cd to your computer and use it in GitHub Desktop.
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.
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