使用 argparse 输出调用函数 [英] Using the argparse output to call functions

查看:28
本文介绍了使用 argparse 输出调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我的代码是这样的.它允许我解析我的程序脚本获得的多个参数.有没有更接近最佳实践"的不同方式?我还没有看到实际使用 argparse 输出的代码,只看到如何设置它.

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 seen code actually using the output of argparse, only how to set it up.

def useArguments():
    x = 0
    while x <= 5:
        if x == 0:                      
            if args.getweather != None:
                getWeather(args.getweather)
        if x == 1:
            if args.post != None:
                post(args.post)
        if x == 2:
            if args.custompost != None:
                custompost(args.custompost)
        if x == 3:
            if args.list != None:
                listAccounts(args.list)
        if x == 4:
            if args.add != None:
                addAccount(args.add[0])
        if x == 5:
            if args.edit != None:
                editAccount(args.edit[0])
        x = x + 1    


if __name__ == '__main__':

    updateConfig()

    parser = argparse.ArgumentParser(description='Post Yahoo weather to Twitter.', epilog="Report any bugs to example@email.com", prog='Program')

    parser.add_argument('-a', '--add', nargs=1, help='Add a new account. Use the desired account name as an argument.')
    parser.add_argument('-e', '--edit', nargs=1, choices=accountListSTR[:-1], help='Edit an account. Use the desired account name as an argument.')
    parser.add_argument('-g', '--getweather', nargs='*', choices=accountListSTR, help='Get weather and post here. Specify account(s) as argument. Use "all" for all accounts. If you specify multiple accounts, separate by a space NOT a comma.')
    parser.add_argument('-p', '--post', nargs='*', choices=accountListSTR, help='Post weather to Twitter. Specify account(s) as argument. Use "all" for all accounts. If you specify multiple accounts, separate by a space NOT a comma.')
    parser.add_argument('-c', '--custompost', nargs=2, help='Post a custom message. Specify an account then type the message. Make sure you use "" around the message. Use "all" for all accounts.')
    parser.add_argument('-l', '--list', action='store_const', const='all', help='List all accounts.')
    parser.add_argument('--version', action='version', version='%(prog)s 0.3.3')

    args = parser.parse_args()

    useArguments()

推荐答案

你可以提供一个自定义的 action 为一个论据,我引用:

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

传递一个实现了动作API.最简单的方法来做到这一点是扩展argparse.Action,提供适当的 __call__方法.__call__ 方法应该接受四个参数:

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:包含此操作的 ArgumentParser 对象.
  2. namespace:parse_args() 将返回的命名空间对象.大多数操作都会向该对象添加一个属性.
  3. values:关联的命令行参数,应用了任何类型转换.(类型转换由 add_argument() 的 type 关键字参数指定.
  4. option_string:用于调用此操作的选项字符串.option_string 参数是可选的,如果操作与位置参数相关联,则该参数将不存在.
  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.

这篇关于使用 argparse 输出调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆