Eol*_*Eol 2 python function argparse
嘿伙计们,我遇到了从argpars调用函数的问题.这是我的脚本的简化版本,这是有效的,打印我给-s或-p的任何值
import argparse def main(): parser = argparse.ArgumentParser(description="Do you wish to scan for live hosts or conduct a port scan?") parser.add_argument("-s", dest='ip3octets', action='store', help='Enter the first three octets of the class C network to scan for live hosts') parser.add_argument("-p", dest='ip', action='store',help='conduct a portscan of specified host') args = parser.parse_args() print args.ip3octets print args.ip
然而,这对我来说在逻辑上相同会产生错误:
import argparse def main(): parser = argparse.ArgumentParser(description="Do you wish to scan for live hosts or conduct a port scan?") parser.add_argument("-s", dest='ip3octets', action='store', help='Enter the first three octets of the class C network to scan for live hosts') parser.add_argument("-p", dest='ip', action='store',help='conduct a portscan of specified host') args = parser.parse_args() printip3octets() printip() def printip3octets(): print args.ip3octets def printip(): print args.ip if __name__ == "__main__":main()
有谁知道我哪里出错了?非常感谢!
它不完全相同,请参阅此问题以解释原因.
你有(至少)2个选择:
args
作为参数传递给您的函数args
一个全局变量.我不确定其他人是否同意,但我个人会将所有解析器功能移到if
声明中,即主要看起来像:
def main(args):
printip3octets(args)
printip(args)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6469 次 |
最近记录: |