Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python. Argparser. Removing not-needed arguments

I am parsing some command-line arguments, and most of them need to be passed to a method, but not all.

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dir", help = "Directory name", type = str, default = "backups")
parser.add_argument("-n", "--dbname", help = "Name of the database", type = str, default = "dmitrii")
parser.add_argument("-p", "--password", help = "Database password", type = str, default = "1123581321")
parser.add_argument("-u", "--user", help = "Database username", type = str, default = "Dmitriy")
parser.add_argument("-a", "--archive", help = "Archive backup", action="store_true")
args = parser.parse_args()

backup(**vars(args)) # the method where i need to pass most of the arguments, except archive. Now it passes all.
like image 627
Anarion Avatar asked Oct 17 '13 15:10

Anarion


1 Answers

Either create a new dictionary that does not have that key:

new_args = dict(k, v for k, v in args.items() if k != 'archive')

Or remove the key from your original dictionary:

archive_arg = args['archive'] # save for later
del args['archive'] #remove it
like image 182
Steven Rumbalski Avatar answered Sep 22 '22 18:09

Steven Rumbalski