Skip to content

Instantly share code, notes, and snippets.

@kmanalo
Created December 19, 2014 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmanalo/0638440f7dff16f8b0d2 to your computer and use it in GitHub Desktop.
Save kmanalo/0638440f7dff16f8b0d2 to your computer and use it in GitHub Desktop.
import argparse
import sys
parser = argparse.ArgumentParser()
action1 = parser.add_argument('--user', help="username")
action2 = parser.add_argument('--bar',
help="Only use this if --user is set")
action3 = parser.add_argument('--bar2',
help="Only use this if --user is set")
argv = set(sys.argv)
def second_depends_on_first(action1, action2):
''' the second action depends on the first action being specified '''
# Inspired by: http://stackoverflow.com/a/11455440/992834
if ((argv & set(action2.option_strings)) and not (argv & set(action1.option_strings))): # set intersection
parser.error(' or '.join(action1.option_strings) + ' must be given with ' +
' or '.join(action2.option_strings))
second_depends_on_first(action1, action2)
second_depends_on_first(action1, action3)
parser.parse_args()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment