Using the argparse output to call functions

前端 未结 3 1145
北恋
北恋 2021-02-14 06:39

Currently my code looks like this. It allows me to parse multiple parameters my program script gets. Is there a different way that is closer to \'best practices\'? I haven\'t se

相关标签:
3条回答
  • 2021-02-14 06:47

    You could supply a custom action for an argument by, and I quote:

    passing an object that implements the Action API. The easiest way to do this is to extend argparse.Action, supplying an appropriate __call__ method. The __call__ method should accept four parameters:

    1. parser: The ArgumentParser object which contains this action.
    2. namespace: The namespace object that will be returned by parse_args(). Most actions add an attribute to this object.
    3. values: The associated command-line args, with any type-conversions applied.(Type-conversions are specified with the type keyword argument to add_argument().
    4. option_string: The option string that was used to invoke this action. The option_string argument is optional, and will be absent if the action is associated with a positional argument.
    0 讨论(0)
  • 2021-02-14 06:55

    See http://docs.python.org/library/argparse.html#sub-commands:

    One particularly effective way of handling sub-commands is to combine the use of the add_subparsers() method with calls to set_defaults() so that each subparser knows which Python function it should execute.

    In a nutshell:

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers()
    
    weather_parser = subparsers.add_parser('get-weather')
    weather_parser.add_argument('--bar')
    weather_parser.set_defaults(function=get_weather)  # !
    
    args = parser.parse_args(['get-weather', '--bar', 'quux'])
    print args.function(args)
    

    Here we create a subparser for the command get-weather and assign the function get_weather to it.

    Note that the documentation says that the keyword/attribute is named func but it's definitely function as of argparse 1.1.

    The resulting code is a bit too wordy so I've published a small package "argh" that makes things simpler, e.g.:

    parser = argparse.ArgumentParser()
    add_commands(parser, [get_weather])
    print dispatch(parser, ['get-weather', '--bar', 'quux'])
    

    "Argh" can do more but I'll let stack overflow answer that. :-)

    0 讨论(0)
  • 2021-02-14 06:58

    With the exception of --version, which is very commonly an option, the actions you've provided are better off treated as "subcommands".

    I'm unaware of the argparse specifics, as I have yet to try Python 2.7, but you might take a look at the svn command as an example, here's some pseudocode for the command line:

    myprog [--version] <command> [<command opts>...]
    

    Where <command> in:

    add|edit|getweather|post|custompost|list
    

    And <command opts> are options specific to that command. Using optparse (which is similar), this would mean that your command would be returned in args, when calling parse_args, allowing you to do something like this:

    opts, args = parser.parse_args()
    if opts.version:
        ...
    else:
        getattr("do_" + args[0])(*args[1:])
    

    I find this pattern particularly useful for debugging, where I'd provide access to internal functions from the command line, and pass various arguments for testing. Adjust the selection of the command handler as appropriate for your own project.

    0 讨论(0)
提交回复
热议问题