Why are my getopts failing if I provide a username?
I am having a problem with my first functional python script I am writing, the complete working excerpt is below.
My problem is that if I include the username ( -u
or --username
) command line parameter , it is always empty and messes up other command line arguments.
For example, if I run this with command line arguments -n tdelane -s 10.1.213.226 -p xxx -v
, I get this as output:
cliloc = /usr/local/bin/cli
server =
username = Administrator
password =
verbose = True
o = -n
a =
cliloc = /usr/local/bin/cli
server =
username =
password =
verbose = True
As you can see, once it gets to -n
it, not only does it not install anything, it also doesn't process anything. If I take it -n
, it works. Running with -s 10.1.213.226 -p xxx -v
is the output:
cliloc = /usr/local/bin/cli
server =
username = Administrator
password =
verbose = True
o = -s
a = 10.1.213.226
o = -p
a = xxx
o = -v
a =
cliloc = /usr/local/bin/cli
server = 10.1.213.226
username = Administrator
password = xxx
verbose = True
Here's the part of this script that matters:
import subprocess, getopt, sys, re
try:
opts, args = getopt.getopt(sys.argv[1:], "cs:np:hv",
["cli", "server", "username", "password", "help", "verbose"])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
cliloc = '/usr/local/bin/cli'
server = ''
username = 'Administrator'
password = ''
verbose = True
if verbose:
print "cliloc = %s" % (cliloc)
print "server = %s" % (server)
print "username = %s" % (username)
print "password = %s" % (password)
print "verbose = %s" % (verbose)
for o, a in opts:
if verbose:
print 'o = ' + o
print 'a = ' + a
if o == "-v":
verbose = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-c", "--cli"):
cliloc = a
elif o in ("-s", "--server"):
server = a
elif o in ("-n", "--username"):
username = a
elif o in ("-p", "--password"):
password = a
else:
assert False, "unhandled option"
if verbose:
print "cliloc = %s" % (cliloc)
print "server = %s" % (server)
print "username = %s" % (username)
print "password = %s" % (password)
print "verbose = %s" % (verbose)
Thanks in advance!
source to share
You have configured -n
not to accept arguments; after it there is no :
:
opts, args = getopt.getopt(sys.argv[1:], "cs:np:hv",
# ^ no colon after this option
["cli", "server", "username", "password", "help", "verbose"])
Add a colon:
opts, args = getopt.getopt(sys.argv[1:], "cs:n:p:hv",
# ^ insert colon here
["cli", "server", "username", "password", "help", "verbose"])
In the colon, the library knows to look for an argument, rather tdelane
than inserting everything into the unparsed option list:
>>> import getopt
>>> import shlex
>>> args = shlex.split('-n tdelane -s 10.1.213.226 -p xxx -v')
>>> getopt.getopt(args, "cs:np:hv")
([('-n', '')], ['tdelane', '-s', '10.1.213.226', '-p', 'xxx', '-v'])
>>> getopt.getopt(args, "cs:n:p:hv")
([('-n', 'tdelane'), ('-s', '10.1.213.226'), ('-p', 'xxx'), ('-v', '')], [])
Note that the module is getopt
rather .. archaic and outdated. Instead, I switched to argparse
.
source to share