python argparse default arguments versus given arguments

Using python 3.8 argparse:

def_value= 'whatever'
argParser.add_argument ("-w", "--whatever", type=str, nargs=1, default=def_value, help="Whatever. Defaults to %s"  % def_value)
args= argParser.parse_args()
print (args.whatever)

If I call my program passing a option -w with a value, args.whatever is a list. I mean:

myprog -w just_a_test

but, I pass nothing, the default argument is a string.


This is annoying, I mean, I have to test the type of args.whatever before using it; If it is a is a string, just use it as string; if it is a list, I have to use it as args.whatever[0], you see ?

What is the best way to deal with such thing ?

PS: I think it is specific to 3.x, as I recall 2.x returned strings all the time.


did you try to remove the nargs option?