Python Forum

Full Version: Why am I getting KeyError 'file' when using argparse?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to do that, but getting KeyError 'file'
import argparse
parser.add_argument('-s', '--server', '-f', '--file', metavar='', required=False, help="name of the user")

print(args['server'], args['file'])
myserver -s jail -f demo.txt
KeyError 'file'
Whats's wrong? How can I fix it?
You need to add_argument for each and every option

parser.add_argument('-s', '--server', metavar='', required=False, help="name of the user")
parser.add_argument('-f', '--file', metavar='', required=False, help="file")