Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python argparse: Make at least one argument required

I've been using argparse for a Python program that can -process, -upload or both:

parser = argparse.ArgumentParser(description='Log archiver arguments.') parser.add_argument('-process', action='store_true') parser.add_argument('-upload',  action='store_true') args = parser.parse_args() 

The program is meaningless without at least one parameter. How can I configure argparse to force at least one parameter to be chosen?

UPDATE:

Following the comments: What's the Pythonic way to parametrize a program with at least one option?

like image 535
Adam Matan Avatar asked Jul 17 '11 09:07

Adam Matan


1 Answers

if not (args.process or args.upload):     parser.error('No action requested, add -process or -upload') 
like image 192
phihag Avatar answered Sep 29 '22 11:09

phihag