I have two arguments: --1st and --2nd I'm trying to make "-2nd" argument required only if "1st" argument set.
For example:
If "1st" is set and "2nd" is set - good
If "1st" is not set and "2nd" is not set - good
Other cases are bad. Help me, please
I have two arguments: --1st and --2nd I'm trying to make "-2nd" argument required only if "1st" argument set.
For example:
If "1st" is set and "2nd" is set - good
If "1st" is not set and "2nd" is not set - good
Other cases are bad. Help me, please
I like Kabanus' solution. Here is another one, which is simpler for new user:
parser = argparse.ArgumentParser()
parser.add_argument('--first')
parser.add_argument('--second')
options = parser.parse_args()
# Error checking
if (options.first is None) != (options.second is None):
print 'Error: --first and --second must both be supplied or omitted'
--1st and --2nd since options.1st does not work and getattr(options, '1st') is too messy. Instead, I use--firstand--second` for illustration purpose.(options.first is None) != (options.second is None) expressed your error condition succinctly.