OptParse是一个从Python2.3版本起引入的一个编写命令行工具模块,示例如下

######example.py######


import optparse

if __name__ == "__main__":
    parser = optparse.OptionParser("usage: %prog [options] arg1 arg2")
    parser.add_option("-H", "--host", type="string", dest="hostname", default="127.0.0.1", help="specify hostname ro run on")
    parser.add_option("-p", "--port", type="int", dest="port", default=80, help="port num to run on")

    (options, args) = parser.parse_args()
    #if len(args) != 2:
    #    parser.error("icorrect number of arguments")
    hostname = options.hostname
    port = options = options.port

    print(hostname, port)

OptParse选项工具模块

使用默认的参数

OptParse选项工具模块

使用参数选项

OptParse选项工具模块

 

相关文章:

  • 2022-12-23
  • 2022-01-19
  • 2021-12-15
  • 2021-06-07
  • 2021-11-25
  • 2022-02-07
  • 2022-02-05
  • 2021-09-25
猜你喜欢
  • 2022-01-23
  • 2021-06-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案