Calling functions with argparse
嘿,伙计们,我在从argpars调用函数时遇到问题。这是我的脚本的一个简化版本,它可以工作,打印我给-s或-p的任何值。
1 2 3 4 5 6 7 8 9 10 11 12 | 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 |
然而,这对我来说逻辑上是相同的,会产生错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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个选项:
我不确定其他人是否同意,但就我个人而言,我将把所有解析器功能都移到
1 2 3 | def main(args): printip3octets(args) printip(args) |
1 2 3 4 5 6 7 | ... printip3octets(args) def printip3octets(args): print args.ip3octets ... |