python - Allowing a specific value for an Argparse argument -


this question has answer here:

is possible require argparse argument 1 of few preset values?

my current approach examine argument manually , if it's not 1 of allowed values call print_help() , exit.

here's current implementation:

... parser.add_argument('--val', dest='val', action='store',                       help='special testing value')  args = parser.parse_args(sys.argv[1:]) if args.val not in ['a','b','c']:     parser.print_help()     sys.exit(1) 

it's not particularly difficult, rather appears messy.

an argparse argument can limited specific values choices parameter:

... parser.add_argument('--val', dest='val', action='store',                       choices=['a','b','c'],                       help='special testing value')  args = parser.parse_args(sys.argv[1:]) 

see docs more details.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -