Skip to content

Instantly share code, notes, and snippets.

@natewalck
Last active February 23, 2022 00:59
Show Gist options
  • Save natewalck/9139842 to your computer and use it in GitHub Desktop.
Save natewalck/9139842 to your computer and use it in GitHub Desktop.
Sub arguments for argparse
#!/usr/bin/python
import argparse
def main():
parser = argparse.ArgumentParser(description="Runs the script")
subparsers = parser.add_subparsers(help='Specify secondary options')
secondary_parser = subparsers.add_parser('secondary', help='secondary options')
parser.add_argument("--primary",
help="Primary Arguments",
action="store_true")
secondary_parser.add_argument('-o',
'--one',
help='Sub-argument one',
action='store_true')
secondary_parser.add_argument('-t',
'--two',
help='Sub-argument two',
action='store_true')
args = parser.parse_args()
if __name__ == "__main__":
main()
./test.py --help
usage: test.py [-h] [--primary] {secondary} ...
Runs the script
positional arguments:
{secondary} Specify secondary options
secondary secondary options
optional arguments:
-h, --help show this help message and exit
--primary Primary Arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment