Python:如果输入了CL参数,则扩展函数

时间:2014-06-03 15:15:33

标签: python if-statement command-line-arguments

基本上,我想打印出部分命令输出,只有" -v"进入,没有做更多变量的第二个功能。

def ldaptst(host):
  for LDAPIP in getLDAPIPs():
    global vlan
    global admip

    ldaptst_output = os.popen("ldaptst -vvvv -s " + LDAPIP + " -n " + host).read ().split ('\n')
    for entry in ldaptst_output:
      if "VLAN" in entry:
        vlan = entry.split(": ")[1]
      if "iphostnumber[0]" in entry:
        admip = entry.split("=")[1]
    if vlan != "vlan":
      print "VLAN \t\t\t: " + vlan
    if sys.argv[1] == "-v":
      if admip != "admin IPv6":
        print "ADM IPv6 Address \t: " + admip
    print ""
    break


if __name__ == "__main__":
  signal.signal(signal.SIGINT, signalHandler)
  if len (sys.argv) == 1:
    print "No free rides, enter hostname!"
  elif len (sys.argv) >= 3 and sys.argv[1] == "-v":
    hosts = sys.argv[2:]
    for host in hosts:
      if len(host) != 14:
        print "Host should have 14 characters."
      else:
        ldaptst(host)
  else:
    hosts = sys.argv[1:]
    for host in hosts:
      if len(host) != 14:
        print "Host should have 14 characters."
      else:
        ldaptst(host)

毋庸置疑,这if sys.argv[1] == "-v":并不起作用。你对这个优雅的方式有什么建议吗?

1 个答案:

答案 0 :(得分:0)

发现问题。实际上,if sys.argv[1] == "-v":确实有效,问题是变量中的拼写错误。