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

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

问题描述

目前我的code看起来是这样的。这使我可以解析多个参数我的程序脚本获取。是否有更接近最佳实践以不同的方式?我还没有看到实际使用 argparse 的输出code,只是如何设置它。

  DEF useArguments():
    X = 0
    而X'LT; = 5:
        如果x == 0:
            如果args.getweather =无!
                getWeather(args.getweather)
        如果x == 1:
            如果args.post =无!
                后(args.post)
        如果x == 2:
            如果args.custompost =无!
                custompost(args.custompost)
        如果x == 3:
            如果args.list =无!
                listAccounts(args.list)
        如果x == 4:
            如果args.add =无!
                addAccount(args.add [0])
        如果x == 5:
            如果args.edit =无!
                editAccount(args.edit [0])
        X = X + 1个
如果__name__ =='__main__':    updateConfig()    解析器= argparse.ArgumentParser(说明='邮政雅虎天气到Twitter。',收尾=报告任何错误,以example@email.com,PROG ='计划')    parser.add_argument(' - 一','--add',NARGS = 1,帮助='添加一个新的帐户使用所需的帐户名作为参数。')
    parser.add_argument(' - E','--edit',NARGS = 1,选择= accountListSTR [: - 1],帮助='。编辑帐户使用所需的帐户名作为参数)
    parser.add_argument(' - G','--getweather',NARGS ='*',选择= accountListSTR,帮助='获取天气和张贴在这里指定帐户作为参数,使用全的所有账户。如果指定了多个帐户,用空格而不是逗号分开。')
    parser.add_argument(。' - P','--post',NARGS ='*',选择= accountListSTR,帮助='天气后到Twitter指定帐户作为参数,使用全的所有账户如。您可以指定多个帐户,用空格而不是逗号分开。')
    parser.add_argument(' - C','--custompost',NARGS = 2,帮助='邮政自定义消息,然后指定的账户类型的信息请确保您使用所有的消息各地使用。所有帐户。')
    parser.add_argument(' - L','--list',行动='store_const',常量='所有',帮助=列出的所有帐户。')
    parser.add_argument(' - 版本',行动='版本',版本=%(前卫)■0.3.3)    ARGS = parser.parse_args()    useArguments()


解决方案

您可以提供一个自定义的 的行动由一个说法,我引用:


  

传递实现的对象
  操作API。最简单的方法来做到这一点
  是延长argparse.Action,
  提供适当的 __ __通话
  方法。在 __ __通话方法应
  接受四个参数:


  
  

      
  1. 解析器:该ArgumentParser对象包含这个动作

  2.   
  3. 空间:将由返回的命名空间对象parse_args()。大多数操作的属性添加到该对象。

  4.   
  5. :关联的命令行参数,与任何类型转换应用(类型转换的类型关键字参数指定 add_argument()

  6.   
  7. option_string :这是用来调用这个动作的选项字符串。在option_string参数是可选的,如果动作与位置相关的参数将不存在。

  8.   

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()

解决方案

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.

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

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