解析 Jython 2.1 中的命令行选项
我正在使用 Jython 2.1 进行 wsadmin 脚本编写,并希望找到一种更好的方法来解析命令行选项。我目前正在这样做:
-> deploy.py foo bar baz
然后在脚本中:
foo = sys.arg[0]
bar = sys.arg[1]
baz = sys.arg[2]
但想这样做:
->部署.py -f foo -b bar -z baz
I'm using Jython 2.1 for wsadmin scripting and want to find a better way of parsing command line options. I'm currently doing this:
-> deploy.py foo bar baz
and then in the script:
foo = sys.arg[0]
bar = sys.arg[1]
baz = sys.arg[2]
but would like to do this:
-> deploy.py -f foo -b bar -z baz
optparse was added to python in 2.3. What other options do I have in Jython 2.1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的事情怎么样:
免责声明:没有以任何方式进行测试。
How about something like this:
Disclaimer: Not tested in any way.
getopt 库与 Jython 2.1 捆绑在一起。它不像较新的参数解析模块那么花哨,但仍然比滚动您自己的参数解析要好得多。
<代码>
导入getopt
getopt 文档:http://docs.python .org/release/2.1.1/lib/module-getopt.html
我在 WebSphere Appserver 7.0.0.x 下使用它。我看到您已经用 websphere-6.1 标记了这个问题 - 不幸的是我现在手头没有 WAS 6.1 系统来测试。
编辑:在 WebSphere 6.1 上验证; getopt 存在。
The getopt library is bundled with Jython 2.1. It's not as fancy as the newer argument parsing modules, but still much better than rolling your own argument parsing.
import getopt
Documentation for getopt: http://docs.python.org/release/2.1.1/lib/module-getopt.html
I'm using it under WebSphere Appserver 7.0.0.x. I see you've tagged this question with websphere-6.1 - unfortunately I don't have a WAS 6.1 system at hand to test right now.
EDIT: Verified on WebSphere 6.1; getopt is present.
请注意,大多数库实际上是简单的 Python 模块,您可以在 Python 发行版的 \Lib 下找到它们,因此通常只需简单的文件副本即可获得该库。
在本例中,我将 optparse.py(及其依赖项 textparse.py)从 Python 2.7 复制到 Jython 2.2,并且它似乎导入得很好。
Note that most libraries are actually simple Python modules that you can find under \Lib in your Python distribution, so often a simple file copy will give you the library.
In this case, I copied optparse.py (with its dependency textparse.py) from Python 2.7 to Jython 2.2, and it seems to import just fine.