Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can argparse set default value of optional parameter to null or empty?

I am using python argparse to read an optional parameter that defines an "exemplar file" that users sometimes provide via the command line. I want the default value of the variable to be empty, that is no file found.

parser.add_argument("--exemplar_file", help = "file to inspire book", default = '')

Will this do it?

like image 722
Fred Zimmerman Avatar asked Jul 22 '16 18:07

Fred Zimmerman


1 Answers

Just don't set a default:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--exemplar_file', help='file to inspire book')

args = parser.parse_args()

print(args.exemplar_file)

# Output:
# None
like image 123
user94559 Avatar answered Oct 19 '22 23:10

user94559