python - Argparse optional args override positional -
i'm trying figure out way have argparse positional arguments mutually exclusive optional argument.
for example:
./adder 20 32 output: 52 ./adder --interactive adder>
right now, if try '--interactive', tells me i'm missing positional arguments.
example code:
parser = argparse.argumentparser() parser.add_argument('a', metavar='first') parser.add_argument('b', metavar='second') parser.add_argument('--interactive') parser.parse_args()
i wondering if there elegant way (preferably using argparse functionality) '--interactive' disables requirement of using 2 positional arguments.
usually positional arguments can not omitted. can try nargs='*'
alternative.
parser = argparse.argumentparser() parser.add_argument('ab', nargs='*', type='int') parser.add_argument('--interactive', action='store_true') args = parser.parse_args()
use list args.ab
store positional argument. if omitted, args.ab
empty list.
Comments
Post a Comment