I'm attempting to allow an optional argument that takes a single command line argument of a filename:
parser.add_argument('--somefile', nargs=1, type=argparse.FileType('r'),
required=False, default='defaultfile.json')
The problem is when I specify a file using the --somefile foo.json I get back a handle in a list:
args.somefile=[<_io.TextIOWrapper name='foo.json' mode='r' encoding='UTF-8'>]
When I don't specify it and want the default, I get back a handle, but not in a list:
args.somefile=<_io.TextIOWrapper name='defaultfile.json' mode='r' encoding='UTF-8'>
How do I get args.somefile to return the same type of structure (handle either in a list or not in a list) consistently?