Answers for "argparse choices"

7

python argparse file argument

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'))
args = parser.parse_args()

print(args.file.readlines())
Posted by: Guest on July-20-2020
2

python argparser flags

parser.add_argument("-v", "--verbose", action="store_true",
                    help="verbose output")
Posted by: Guest on April-01-2020
0

argparse choices

>>> parser = argparse.ArgumentParser(prog='game.py')
>>> parser.add_argument('move', choices=['rock', 'paper', 'scissors'])
>>> parser.parse_args(['rock'])
Namespace(move='rock')
>>> parser.parse_args(['fire'])
usage: game.py [-h] {rock,paper,scissors}
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
'paper', 'scissors')
Posted by: Guest on April-17-2021

Browse Popular Code Answers by Language