在 Python 中使用命令行参数调用另一个库函数的问题
Issue calling another library function using command line argument in Python
我正在尝试根据输入的命令行参数调用不同的库函数。
我的主函数将接受输入参数,并将根据传递给 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() 函数的导入库的主体
def gather(self):
if not os.path.exists('intel'):
os.mkdir('intel')
print "Starting update process"
counter = 0
ioc_list = []
timestr = time.strftime("%Y%m%d-%H%M%S")
for source in IP.iteritems():
print source
name = source + timestr
print name
file = open(name, 'w')
c = connect(source,params=url_param())
for line in c:
if line.startswith("/") or line.startswith('\n') or line.startswith("#"):
pass
else:
file.write(line+'\n')
因此,当“-update”参数传递到命令行时,将调用我的 gather() 函数。
gather() 函数的作用是创建一个名为 "intel" 的目录。
然后它将遍历 IP 列表并根据 IP 和时间戳创建文件名。然后它将调用 connect 函数来创建到 IP 的 HTTP 连接。
它将解析IP的内容并将其写入文件。
我无法使用我在此处添加的程序来实现此目的。
由于某种原因,来自 main() 函数本身的调用没有成功。
我尝试在 gather() 函数中添加 "print" 语句以查看哪些部分在工作。
请社区帮助我。
解决这个问题的关键在于论证的特点。通过定义 parser.add_argument('-update', type=str, nargs='?', help='Update the local storage')
我假设一个参数将与 -update
参数一起传递到命令行。
但我只关心 -update arg 出现在命令行中而没有任何其他实体。
这可以通过将脚本行更改为:
来解决
parser.add_argument('-update', action='store_true', help='Update the local storage')
这将确保只有 -update 的存在会调用函数“gather()
”。
我正在尝试根据输入的命令行参数调用不同的库函数。
我的主函数将接受输入参数,并将根据传递给 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() 函数的导入库的主体
def gather(self):
if not os.path.exists('intel'):
os.mkdir('intel')
print "Starting update process"
counter = 0
ioc_list = []
timestr = time.strftime("%Y%m%d-%H%M%S")
for source in IP.iteritems():
print source
name = source + timestr
print name
file = open(name, 'w')
c = connect(source,params=url_param())
for line in c:
if line.startswith("/") or line.startswith('\n') or line.startswith("#"):
pass
else:
file.write(line+'\n')
因此,当“-update”参数传递到命令行时,将调用我的 gather() 函数。 gather() 函数的作用是创建一个名为 "intel" 的目录。 然后它将遍历 IP 列表并根据 IP 和时间戳创建文件名。然后它将调用 connect 函数来创建到 IP 的 HTTP 连接。 它将解析IP的内容并将其写入文件。
我无法使用我在此处添加的程序来实现此目的。 由于某种原因,来自 main() 函数本身的调用没有成功。 我尝试在 gather() 函数中添加 "print" 语句以查看哪些部分在工作。 请社区帮助我。
解决这个问题的关键在于论证的特点。通过定义 parser.add_argument('-update', type=str, nargs='?', help='Update the local storage')
我假设一个参数将与 -update
参数一起传递到命令行。
但我只关心 -update arg 出现在命令行中而没有任何其他实体。
这可以通过将脚本行更改为:
parser.add_argument('-update', action='store_true', help='Update the local storage')
这将确保只有 -update 的存在会调用函数“gather()
”。