Skip to content

Instantly share code, notes, and snippets.

@ticky
Created February 26, 2014 11:56
Show Gist options
  • Save ticky/9228243 to your computer and use it in GitHub Desktop.
Save ticky/9228243 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Trying to achieve a command line option list like this
# argparsetest.py [--url <url> [[--string <string>] [--string-two <string>] | --boolean [--number <number>]]]
# This crashes if you pass it --help. Why?
from argparse import ArgumentParser
parser = ArgumentParser('demonstration of argparse issue')
parser.add_argument('-u', '--url', metavar='<url>', type=str, help='URL to process')
mode_group = parser.add_mutually_exclusive_group()
no_tweet_group = mode_group.add_argument_group()
no_tweet_group.add_argument('-b', '--boolean', action='store_const', const=True, default=False, help='set something')
no_tweet_group.add_argument('-n', '--number', metavar='<number>', type=int, help='set a number')
active_group = mode_group.add_argument_group()
active_group.add_argument('-s', '--string', metavar='<string>', type=str, help='set a string')
active_group.add_argument('-t', '--string-two', metavar='<string>', type=str, help='set another string')
cli_args = parser.parse_args()
print(cli_args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment