简体   繁体   English

无法在Python中解析命令行参数

[英]Unable to parse Command line argument in Python

I am trying to create a script that takes command line argument and based on the input, it calls out to relevant function. 我正在尝试创建一个接受命令行参数并基于输入的脚本,该脚本调出相关功能。 Here is how my main function looks like: 这是我的主要功能的样子:

from lib.updatefeed import gather
#stdlib imports
import argparse



def main():
    print "test"
    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('-ip', type=str, nargs='+', help="Search for a single IP")

    parser.add_argument('-list', type=str, nargs='?', help="Search for a list of IP")
    parser.add_argument('-update', type=str, nargs='?', help='Update the local storage')
    args = parser.parse_args()
    if args.ip:
        if len(args.ip) > 4:
            print "Too many"
            sys.exit(1)
        parse_ip(args.ip)
    if args.list:
        parse_ipList(list)
    if args.update:
        print "updating"
        gather()

if __name__ == '__main__':
    main()

All other Arguments are working fine and the respective functions are being called. 所有其他参数都工作正常,并且正在调用各个函数。 The only problem is with the "update" argument. 唯一的问题是“更新”参数。 For some reason, the gather() function is not getting called when the -update arg is passed. 由于某些原因,传递-update arg时未调用collect gather()函数。 I have also added a print statement before the function call but that is also not getting printed either. 我还在函数调用之前添加了一条print语句,但也没有得到打印。

Can anyone help me in identifying the root cause. 谁能帮助我确定根本原因。

Here is a part of my gather function as well: 这也是我的收集功能的一部分:

def gather(self):
    if not os.path.exists('New'):
        os.mkdir('New')
    print "Starting feed update process"
parser.add_argument('-update', type=str, nargs='?', help='Update the local storage')

declares the option -update as taking a single optional argument ( nargs='?' ); 将选项-update声明为采用单个可选参数( nargs='?' ); the value of the option will be either the argument, if supplied, or the value of the default key. 选项的值将是自变量(如果提供)或default键的值。 However, you don't provide a default key, and the default default is None . 但是,您没有提供default密钥,并且默认default值为None

So if you just provide the command-line option -update with no argument, then the values of args.update will be None , and the test: 因此,如果仅提供不带参数的命令行选项-update ,则args.update的值将为None ,并且测试:

if args.update:
        print "updating"
        gather()

will fail, so nothing will be done. 将会失败,所以什么也不会做。

Apparently, you only care whether -update is present in the command-line, so it should not take any arguments. 显然,您只关心命令行中是否存在-update ,因此它不应带有任何参数。 To handle this case, define the option as having the action store_true , and leave out the type and nargs parameters: 要处理这种情况,请将选项定义为具有动作store_true ,并省略typenargs参数:

parser.add_argument('-update', action='store_true', help='Update the local storage')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM