OptionError: optparse.py invalid action: error

1.2k views Asked by At

Greetings I am receiving an error when I attempt to place arguments into my python script. Here is the function

from optparse import OptionParser

def getparams():
usage = "usage: %prog -d <dcb_ip> -p <port> "
parser = OptionParser(usage)
parser.add_option("-d", "--dcb_ip",
                  action="store",
                  dest="dcb_ip",
                  default="172.14.20.31",
                  help="Specifiy the IP address of the device, default is [%default].")
parser.add_option("-p", "--port",
                  action="store",
                  type="int",
                  dest="port",
                  default=51500,
                  help="Specify the pcl port of the monitor, default is [%default].")
parser.add_option("-i", "--interactive", action="store_true",  default=False, dest="interactive")
parser.add_option("-l", "--loop", action="store",  default=False, dest="loop")
parser.add_option("-w", "--write", action="write_mem",  default=False, dest="interactive")  #Write HART option
parser.add_option("-r", "--read", action="read_mem",  default=False, dest="interactive")
parser.add_option("-c", "--Cancel", action="cancel_mem",  default=False, dest="interactive")
(options, args) = parser.parse_args(sys.argv)    
return options

Does anyone know why I keep getting

Traceback (most recent call last):
  File "C:\Users\Documents\devices_15_JUL_2013\RSimulator.py", line 73, in getparams
    parser.add_option("-w", "--write", action="write_mem",  default=False)  #Write mem option
OptionError: option -w/--write: invalid action: 'write_mem''

I thought -c might be a common option or something but even when I changed it still did not work :(

1

There are 1 answers

1
Martijn Pieters On BEST ANSWER

write_mem, read_mem and cancel_mem are not valid values for the action keyword; see Standard option actions for what is available.

It is not clear what you expect those options to do, however. You specified the destination as interactive, but that is already in use as a boolean flag for the -i, --interactive option.

If you wanted to set boolean flags for each of these in their _mem options, then do so with store_true actions:

parser.add_option("-w", "--write", action="store_true", default=False, dest="write_mem")
parser.add_option("-r", "--read", action="store_true", default=False, dest="read_mem")
parser.add_option("-c", "--cancel", action="store_true", default=False, dest="cancel_mem")