I'm following the example at https://docs.python.org/2/library/getopt.html for parsing command line options. I'm almost a newbie with python. Like the example my code is:
import getopt, sys
import pdb
config_filename = 'config.py'
orders_filename = 'default.out'
db_filename = 'sqlite.db'
action = False
def main():
c_args_parse()
print action
def c_args_parse():
pdb.set_trace()
try:
(opts, args) = getopt.getopt(sys.argv[1:], "hc:d:o:s:")
except getopt.GetoptError as err:
# print help information and exit:
print '\n' + str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
usage()
exit()
elif o in ("-c", "--config"):
config_filename = a
elif o in ("-d", "--db"):
db_filename = a
elif o in ("-o", "--output"):
orders_filename = a
elif o in ("-s", "--sync"):
action = a
else:
assert False, "\nUnhandled option"
if not action:
print '\nSpecifying a sync action is required'
usage()
sys.exit(2)
def usage():
usage = """
BESync [hcdo] -s orders|products|availablity
-h --help Prints this help
-c --config (arg) Path to config file
-d --db (arg) Path to sqlite db
-o --output (arg) Path to orders output file (arg)
-s --sync (arg) Actions (arg): orders, products, availability
"""
print usage
if __name__ == "__main__":
main()
Within the for loop, variable a seems to be empty while tuple (o,a) is fine.
running pdb trace:
$ python test.py -s orders
test.py(16)c_args_parse()
-> try:
(Pdb) n
> ./test.py(17)c_args_parse()
-> (opts, args) = getopt.getopt(sys.argv[1:], "hc:d:o:s:")
(Pdb)
> ./test.py(24)c_args_parse()
-> for o, a in opts:
(Pdb)
> ./test.py(25)c_args_parse()
-> if o in ("-h", "--help"):
(Pdb) o,a
('-s', 'orders')
(Pdb) o
'-s'
(Pdb) a
# returns nothing
Any explanation for this?
a
doesn't do what you think it does. a
is a debugger command which prints the arguments to the current function.
Try p a
to print the value of the expression a
.