2024-09-29 22:34:30 发布
网友
所以,我遇到了^{} module来解析命令行参数,尽管我无法理解这些文档。无论出于什么原因,我无法理解为什么这没有看到我的--domain example.com参数。
--domain example.com
$ ./httpdsetup.py -a -u zack --domain example.com [('-a', ''), ('-u', '')]
我打印出了转储到opts中的内容,以查看它看到了什么。下面的代码与文档站点上的代码完全相同。在
opts
我更喜欢argparse。Python documention here。 它在Python>;=2.7和>;=3.2中,但在python3.0和3.1中没有。如果安装中缺少它,只需将the single file from here复制到脚本所在的位置,或复制到Python安装中。在
argparse
以下是与argparse示例相似的内容:
#!/usr/bin/env python3 import sys def create_apache_vhost(*args, **kwargs): pass def create_lighty_vhost(*args, **kwargs): pass def main(argv): import argparse parser = argparse.ArgumentParser(description="Some server", formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument(' username', type=str) parser.add_argument('-u', dest='username', type=str) parser.add_argument(' apache', dest='httpd', action='store_const', const='apache') parser.add_argument('-a', dest='httpd', action='store_const', const='apache') parser.add_argument(' lighthttpd', dest='httpd', action='store_const', const='lighthttpd') parser.add_argument('-l', dest='httpd', action='store_const', const='lighthttpd') parser.add_argument(' domain', type=str) parser.add_argument(' vhost', type=str) parser.add_argument('-v', dest='domain', type=str) parser.add_argument(' dir', dest='directory', type=str) parser.add_argument('-d', dest='directory', type=str) defaults = { 'httpd': 'apache', } parser.set_defaults(**defaults) args = parser.parse_args(args=argv) print(args) if args.httpd == 'apache': create_apache_vhost(args.domain, args.directory, args.username) elif args.httpd == 'lighttpd': create_lighty_vhost(args.domain, args.directory, args.username) if __name__ == '__main__': main(sys.argv[1:])
我更喜欢
argparse
。Python documention here。 它在Python>;=2.7和>;=3.2中,但在python3.0和3.1中没有。如果安装中缺少它,只需将the single file from here复制到脚本所在的位置,或复制到Python安装中。在以下是与
argparse
示例相似的内容:相关问题 更多 >
编程相关推荐