Python: Can optparse have the ACTION attribute to act both like STORE and STORE_TRUE?

最后都变了- 提交于 2019-12-25 02:08:20

问题


I am using optparse to get command line input.

Lets say that I am running a script demo.py and it creates some output. But unless I specify the command line input, the output is not written to a file.

I am trying to do the following:

python demo.py in command line should run the script, but not write the output anywhere.

python demo.py -o in command line should write the output to my default file name output.txt.

python demo.py -o demooutput.txt in command line should write the output to file demooutput.txt.

PS: I would not prefer to switch to argparse from optparse.


回答1:


You can use optparse-callbacks to achieve this.

Here is how it wiill work for your use case.

parser.add_option("-o", action="callback", dest="output", callback=my_callback)

def my_callback(option, opt, value, parser):
     if len(parser.rargs) > 0:
         next_arg = parser.rargs[0]
         if not next_arg.startswith("-"):
             # Next argument is not another option
             del parser.rargs[0]
             setattr(parser.values, option.dest, next_arg)
             return
     # If not processed, set the default value
     setattr(parser.values, option.dest, "output.txt")



回答2:


I don't think there is unfortunately - the only way I can think of is hacking around the problem by adding your own logic statements. The following code should do the trick.

import re, sys
import optparse from OptionParser    
usage = "usage: %prog [options] arg"
parser = OptionParser(usage)
if '-f' in argv:
    a = argv.index('-f')
    if (a != len(argv)-1) and re.search('[.]txt', argv[a+1]):
        parser.add_option("-f", "--foo", dest="foo")
    else:
        parser.add_option("-f", dest="foo", action="store_true")



回答3:


This doesn't answer the direct question, 'how to define an Action...', but it handles the inputs in a simple way.

Set '-o' to be 'store_true'. If True check the 'args' variable for a file name.

(options, args) = parser.parse_args()
if options.o:
    if args:
        dest = args[0]
    else:
        dest = 'output.txt'
else:
    dest = ''

(In argparse the equivalent would be to define a positional argument with nargs='?'.)

If these are the only arguments, you could also get by with checking for the filename without requiring the `-o'.

Another possibility - 'store_const', with the positional 'filename' having priority:

parser = optparse.OptionParser()
parser.add_option('-o',dest='dest',action='store_const', const='output.txt', default='')
(options, args) = parser.parse_args()
if args:
    options.dest = args[0]
print options


来源:https://stackoverflow.com/questions/21722509/python-can-optparse-have-the-action-attribute-to-act-both-like-store-and-store

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!