0

I'm creating an error for passing this argument. Where if the user doesn't enter 'followers' or 'following' it tells them. The problem is, the error is coming up when I enter the correct argument as well.

parser = argparse.ArgumentParser('iStats')
parser.add_argument('-r', '--relationship', help='Followers or Following', required=True)
args = vars(parser.parse_args())

if args['relationship'] is not 'following' or 'followers':
    parser.error("argument MUST be followers OR following")

Error I'm getting

C:\Users\Daniel>istats.py -r following
usage: iStats [-h] [-r RELATIONSHIP]
iStats: error: argument MUST be followers OR following

EDIT: After reassessing my understanding, I have updated my code with the preferred method.

parser = argparse.ArgumentParser('iStats')
parser.add_argument('-r', '--relationship', choices=['following', 'followers'], help='Choose following or followers.', required=True)
args = vars(parser.parse_args())
dxrth
  • 33
  • 7
  • Your understanding of boolean logic is flawed; it is not like english grammar. Use `if args['relationship'] not in ('following', 'followers'):`. Better yet, use the [`choices` option](https://docs.python.org/2/library/argparse.html#choices) to `parser.add_argument()` and have `argparse` validate the possible values. – Martijn Pieters Jul 12 '15 at 00:50
  • Went with choices. Thanks! – dxrth Jul 12 '15 at 01:00

0 Answers0