解析 Jython 2.1 中的命令行选项

发布于 2024-12-18 06:02:37 字数 443 浏览 3 评论 0原文

我正在使用 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

optparse 在 2.3 中被添加到 python 中。 Jython 2.1 中还有哪些其他选项?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦浮鱼 2024-12-25 06:02:37

像这样的事情怎么样:

args = sys.argv[:]  # Copy so don't destroy original
while len(args) > 0:
    current_arg = args[0]

    if current_arg == '-f':
        foo = args[1]
        args = args[2:]
    elif current_arg == '-b':
        bar = args[1]
        args = args[2:]
    elif current_arg == '-z':
        baz = args[1]
        args = args[2:]
    else:
        print 'Unknown argument: %r' % args[0]
        args = args[1:]

免责声明:没有以任何方式进行测试。

How about something like this:

args = sys.argv[:]  # Copy so don't destroy original
while len(args) > 0:
    current_arg = args[0]

    if current_arg == '-f':
        foo = args[1]
        args = args[2:]
    elif current_arg == '-b':
        bar = args[1]
        args = args[2:]
    elif current_arg == '-z':
        baz = args[1]
        args = args[2:]
    else:
        print 'Unknown argument: %r' % args[0]
        args = args[1:]

Disclaimer: Not tested in any way.

悲欢浪云 2024-12-25 06:02:37

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.

当爱已成负担 2024-12-25 06:02:37

请注意,大多数库实际上是简单的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文