Python 2.7 在Python中使用命令行参数调用另一个库函数时出现问题

Python 2.7 在Python中使用命令行参数调用另一个库函数时出现问题,python-2.7,arguments,parameter-passing,Python 2.7,Arguments,Parameter Passing,我试图根据输入命令行参数调用不同的库函数 我的主函数将接受输入参数,并根据传递给cli的参数调用该函数。这是我的主要功能 from lib.updatefeed import * def main(): ...... ...... parser.add_argument('-update', type=str, nargs='?', help='Update the local storage') if args.update: gathe

我试图根据输入命令行参数调用不同的库函数

我的主函数将接受输入参数,并根据传递给cli的参数调用该函数。这是我的主要功能

from lib.updatefeed import *
    def main():
    ......
    ......
    parser.add_argument('-update', type=str, nargs='?', help='Update the local storage')
    if args.update:
        gather(args.update)

    if __name__ == '__main__':
        main()
这里,gather()是我已经在主程序中导入的另一个python库中的另一个函数

下面是使用gather()函数导入的库的主体

因此,在将“-update”参数传递到命令行时,将调用my gather()函数。 函数的作用是创建一个名为“intel”的目录。 然后它将遍历IP列表,并根据IP和时间戳创建文件名。然后,它将调用connect函数来创建到IP的HTTP连接。 它将解析IP的内容并将其写入文件

我不能通过使用我在这里添加的程序来实现这一点。 出于某种原因,main()函数本身的调用没有成功。 我尝试在gather()函数中添加“print”语句,以查看哪些部分正在工作。
社区能在这方面帮助我吗。

这个问题的解决方案在于论点的特点。通过定义
parser.add_参数('-update',type=str,nargs='?',help='update the local storage')
我估计一个参数将与
-update
参数一起传递到命令行。 但是我只关心命令行中出现的-update arg,而不需要任何额外的实体。 可以通过将脚本行更改为:

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

这将确保only-update的存在将调用函数“
gather()
”。

请将带有导入的行包含在您的代码示例中?您在上面粘贴的哪一行会得到错误?错误消息是什么?@jhinghaus:在包含gather()函数的库中添加了导入“from lib.updatefeed import*”,其中updatefeed.py。@barny我添加的代码中没有错误。当参数传递给main时,它只是不调用gather()函数。打印args.update时会得到什么?在“if args.update:”行之前打印它
parser.add_argument('-update', action='store_true', help='Update the local storage')