2

I have a problem with giving arguments to a function when using callback. I am new to the command line argument scripting in python so go easy on me. Here's my code:

    from optparse import OptionParser
    import urllib

    def google(option, opt_str, value, parser):
        print options.f_name

    parser = OptionParser()
    parser.add_option("-f", "--first", type="string", dest="f_name", help="Supply a first name to search", action="store")
    parser.add_option("-l", "--last", type="string", dest="l_name", help="Supply a last name to search", action="store")
    parser.add_option("-g", "--google", action="callback", callback=google)

    (options, args) = parser.parse_args()

And can't seem to figure out why it wouldn't print out the user supplied input. I've looked at the doc for python on optparse and it just gets fuzzy. Anyways any possibly way I can use options.f_name in that function. I have been using something as such to put into the functions arguments to use.

    first_name = options.f_name

Then would supply the function with one of the arguments that didn't work either.

John
  • 73
  • 2
  • 8

4 Answers4

1

Old post, but for posterity: You need to specify the type of the argument in order to have optparse inject it into the value parameter:

def google(option, opt_str, value, parser):
    print value
parser.add_option("-g", "--google", action="callback", callback=google, type="string")

Full example showing how to return the value to the parser for inclusion into options:

from optparse import OptionParser

def doSomethingWithValue(value):
    return "baked beans and {}".format(value)

def google(option, opt_str, value, parser):
    setattr(parser.values, option.dest, doSomethingWithValue(value))

parser = OptionParser()
parser.add_option("-g", "--google", action="callback", callback=google, type="string", dest="googleOption")
(options, args) = parser.parse_args()
print(options)

# ./script.py          ==> {'googleOption': None}
# ./script.py -g spam  ==> {'googleOption': 'baked beans and spam'}
netminkey
  • 11
  • 1
0

optparse is hard to use. You should try docopt. http://docopt.org/

As a side note, urllib is hard to use too. Check out the requests module. http://docs.python-requests.org/en/latest/

user581592
  • 380
  • 1
  • 8
0

The order of evaluation of the arguments is not specified, so the callback for -g may be called before optparse handles the -f option. The only way to do this during the parse is to make them both callbacks that are aware of each other, and only when the second argument is handled does it perform the behaviour you are looking for. Is there any reason you can't just set a flag and handle it after the parse_args() is complete, then you will be sure all the arguments have been handled.

BTW: optparse is deprecated in favour of argparse.

AChampion
  • 29,683
  • 4
  • 59
  • 75
0

If you check the documentation, you'll find out that you actually need to use:

parser.values.f_name

Of course you should make sure that you take the precautions for cases where it hasn't been defined yet.

Community
  • 1
  • 1
peterph
  • 980
  • 6
  • 11