Reputation: 4564
def check_file(user_name,default_name):
while True:
try:
#### check user name matches the default name
if ('%s'%(user_name)) == '%s'%(default_name):
print("file matches")
break
except:
print("wrong file.")
continue
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-k1',type=argparse.FileType('r'),default='k1file.txt',required=True,
help='file input')
parser.add_argument('-k2',type=argparse.FileType('r'),default='k2file.txt',required=True,
help='file input')
args = parser.parse_args()
check_file(args.k1,args.get_default('k1'))
check_file(args.k2,args.get_default('k2'))
Present output:
AttributeError: 'Namespace' object has no attribute 'k1'
Upvotes: 0
Views: 214
Reputation: 21275
The add_argument
method returns the Action
that was just added. Which has details about the argument definition - including the default value.
import argparse
parser = argparse.ArgumentParser()
arg_action = parser.add_argument('-f', default='file')
print(arg_action.default)
Prints:
file
And you should be able to use that to compare the actual arguments with the default values.
Upvotes: 1