Created
December 19, 2014 22:46
-
-
Save kmanalo/0638440f7dff16f8b0d2 to your computer and use it in GitHub Desktop.
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 | |
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