Python sys.argv[1:] 不选择命令行选项

发布于 2024-10-16 04:55:24 字数 3893 浏览 5 评论 0原文

更新/解决方案:答案如下,来自扎克。事实上,问题是脚本文件 clenotes.cmd 本身的 DOS 行结尾。由于我对各种文件进行了如此多的摸索,因此我删除了整个目录,然后从 此处。我在文件上运行 Zack 的 perl 脚本,如下所示:

perl -pi.bak -e 's/[ \t\r]+$//' clenotes.cmd

然后我稍微编辑了命令执行,使最终脚本变为:

CWD=`dirname $0`
JYTHON_HOME="$CWD"
LIB_DIR="$JYTHON_HOME/lib"
NOTES_HOME="/opt/ibm/lotus/notes/"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$NOTES_HOME
java -cp "$LIB_DIR" -jar "$LIB_DIR/jython.jar" -Djython.home="$CWD/" -Dpython.path="$LIB_DIR:$CWD/ext" -Djava.library.path="$NOTES_HOME" "$LIB_DIR/clenotes/cletes/clenotes.py" "$@"

就是这样 - 其他一切都有效。无需编辑 clenotes.py 或 clenotes.cfg。非常感谢您坚持提出这个问题,我想这个问题最终很简单。


更新:我正在削减一些代码,以使其更具可读性,并从帖子中删除不必要的信息。


我正在尝试获取 Lotus Notes 命令行 在 Linux 上运行,但我遇到了与 python 文件中 sys.argv[1:] 相关的问题。 Windows 脚本在这里:

@echo off
@setlocal 
set CWD=%~dp0
set JYTHON_HOME=%CWD%
set LIB_DIR=%JYTHON_HOME%/lib
java -cp %LIB_DIR% -jar %LIB_DIR%/jython.jar -Djython.home=%CWD%  -python.path=%LIB_DIR%;%CWD%/ext %LIB_DIR%/clenotes/clenotes.py  %*

@endlocal

我在变量方面遇到了困难,所以对于 Linux,它看起来就像这样:

java -cp ./lib/ -jar ./lib/jython.jar -Djython.home=./ -Dpython.path=./lib:./ext -Djava.library.path=/opt/ibm/lotus/notes/ ./lib/clenotes/clenotes.py $*

我从目录中运行它。无论如何,令我困惑的是它没有获取我从命令行传递的任何选项。 clenotes.cmd --help 结果

No commands specified. Use --help option for usage.

如下是应该解析命令行参数的部分:

def main():    

  Output.log("Entering %s v%s" % (PROGRAM_NAME,VERSION),Output.LOGTYPE_DEBUG)
  cliOptions2=[]
  for opt in cliOptions:
    opt2=opt.replace('--','')
    opt2=opt2.replace('!','=')
    cliOptions2.append(opt2)
  opts=[]
  args=[]
  try:
    opts, args = getopt.getopt(sys.argv[1:], '', cliOptions2)

我在 32 位 chroot 环境中的 Arch Linux 64 位上使用 Python 3.1.3。我可以提供其他东西吗?

以防万一需要...此处是整个 clenotes.py 文件。

另外,根据评论中的要求,配置文件(包含帮助消息和可行的选项/参数)为 这里


更新

经过大量摆弄,我取得的最好进展是检查它在 (main最令人惊讶的是,当传递一个参数然后使用 print sys.argv 查看它的解析结果时,该选项会在其中出现一个尾随的 \r例如:

clenotes.cmd appointments
args is ['appointments\r']

在 Windows 上我做了同样的事情,args 被报告为 ['appointments'] 此外,手动设置 args=['appointments'] 然后注释掉。 getopt.getopt 分配值的部分起作用了

最后,我发现当使用多个参数时,其中的 n-1 个参数会被解释并使用,而第 n 个参数会被忽略。这是一种解决方法,因为我实际上可以使用该脚本......但显然它不是首选。如果我想查看今天的约会,我可以执行 clenotes.cmd Appointments --today --today ,它就会起作用。 sys.argv 将输出:['appointments', '--today', '--today\r']

那么...是什么导致了尾随 \r ?我认为这与实际脚本有关。再次注意:

java -cp ./lib/ -jar ./lib/jython.jar -Djython.home=./ -Dpython.path=./lib:./ext -Djava.library.path=/opt/ibm/lotus/notes/ ./lib/clenotes/clenotes.py $*

所以...一堆路径内容,然后是实际的 python 文件: clenotes.py $*

我从 这里

它是否拾取回车符?

Update/Solution: the answer is below, from Zack. The problem was, indeed, DOS line endings on the script file itself, clenotes.cmd. Since I futzed with the various files so much, I deleted the whole directory and then re-downloaded a fresh copy from HERE. I ran Zack's perl script on the file just like so:

perl -pi.bak -e 's/[ \t\r]+$//' clenotes.cmd

I then edited the command execution just slightly so that the final script became:

CWD=`dirname $0`
JYTHON_HOME="$CWD"
LIB_DIR="$JYTHON_HOME/lib"
NOTES_HOME="/opt/ibm/lotus/notes/"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$NOTES_HOME
java -cp "$LIB_DIR" -jar "$LIB_DIR/jython.jar" -Djython.home="$CWD/" -Dpython.path="$LIB_DIR:$CWD/ext" -Djava.library.path="$NOTES_HOME" "$LIB_DIR/clenotes/cletes/clenotes.py" "$@"

That was it -- everything else worked. No edits needed to clenotes.py or clenotes.cfg. Many thanks for sticking with the question, which I guess ended up being quite simple.


Update: I'm cutting down on some of the code to make this more readable and remove unnecessary information from the post.


I'm trying to get Lotus Notes command line to work on Linux and am having an issue with something related to sys.argv[1:] in the python file. The windows script is here:

@echo off
@setlocal 
set CWD=%~dp0
set JYTHON_HOME=%CWD%
set LIB_DIR=%JYTHON_HOME%/lib
java -cp %LIB_DIR% -jar %LIB_DIR%/jython.jar -Djython.home=%CWD%  -python.path=%LIB_DIR%;%CWD%/ext %LIB_DIR%/clenotes/clenotes.py  %*

@endlocal

I was having a tough time with variables, so for Linux, it simply looks like this:

java -cp ./lib/ -jar ./lib/jython.jar -Djython.home=./ -Dpython.path=./lib:./ext -Djava.library.path=/opt/ibm/lotus/notes/ ./lib/clenotes/clenotes.py $*

I run it from within the directory. In any case, what puzzles me is that it's not picking up any options I pass from the command line. clenotes.cmd --help results in

No commands specified. Use --help option for usage.

Here is the section where the command line arguments are supposed to be parsed:

def main():    

  Output.log("Entering %s v%s" % (PROGRAM_NAME,VERSION),Output.LOGTYPE_DEBUG)
  cliOptions2=[]
  for opt in cliOptions:
    opt2=opt.replace('--','')
    opt2=opt2.replace('!','=')
    cliOptions2.append(opt2)
  opts=[]
  args=[]
  try:
    opts, args = getopt.getopt(sys.argv[1:], '', cliOptions2)

I'm using Python 3.1.3 on Arch Linux 64bit in a 32bit chroot environment. Can I provide anything else?

Just in case it's needed... HERE is the whole clenotes.py file.

Also, as requested in the comments, the config file (which contains the help message and viable options/arguments, is HERE


Update

After a lot of fiddling, the best progress I have made has been to examine what it's setting as opts and args in the (main) method. Most surprising was that when passing an argument and then looking at it's parsed result using print sys.argv, the option would come up with a trailing \r in it. For example:

clenotes.cmd appointments
args is ['appointments\r']

On Windows I did the same and args was reported as ['appointments']. Furthermore, manually setting args=['appointments'] and then commenting out the section where getopt.getopt is assigning a value worked.

Lastly, I've found that when using multiple arguments, n-1 of them get interpreted and used while the nth one gets ignored. This is kind of a workaround since I can actually use the script... but obviously it's not preferred. If I want to look at today's appointments, I can execute clenotes.cmd appointments --today --today and it will work. sys.argv will spit out: ['appointments', '--today', '--today\r'].

So... what's causing the trailing \r? I'm thinking it has to do with the actual script. Note it again:

java -cp ./lib/ -jar ./lib/jython.jar -Djython.home=./ -Dpython.path=./lib:./ext -Djava.library.path=/opt/ibm/lotus/notes/ ./lib/clenotes/clenotes.py $*

So... bunch of path stuff and then the actual python file: clenotes.py $*

I got the $* from HERE

Is it picking up the carriage return??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

糖果控 2024-10-23 04:55:24

我认为你的问题是 clenotes.cfg 有 DOS 行结尾,Python 对此进行了误解。尝试将 clenotes.py 的这一行更改

config.readfp(open('%sconfig/clenotes.cfg' % System.getProperty('jython.home')))

config.readfp(open('%sconfig/clenotes.cfg' % System.getProperty('jython.home'), "rU"))

“rU”告诉 Python,即使它在 Unix 系统上运行,它也应该准备好处理包含 DOS 行结尾的文件。请参阅http://docs.python.org/library/functions.html#open -- 向下滚动到以“除了标准 fopen() 模式之外......”开头的段落。

(或者您可以运行此命令:perl -pi.bak -e 's/[ \t\r]+$// clenotes.cfg ——这会将其转换为 Unix 行结尾。我可能会两者都做。)

(如果以上建议都没有帮助,我接下来要尝试的是使用上面的 perl 命令点击 clenotes.py 本身。我不明白这怎么可能是问题所在,但如果 \r 字符不是来自 clenotes.cfg,则 .py 文件是唯一合理的剩余源。)

(编辑:根据您对问题本身的评论,我现在认为它是 clenotes.cmd,shell 脚本包装器,需要从 DOS 转换为 Unix 行结尾。)

I think your problem is that clenotes.cfg has DOS line endings, which Python is misinterpreting. Try changing this line of clenotes.py

config.readfp(open('%sconfig/clenotes.cfg' % System.getProperty('jython.home')))

to read

config.readfp(open('%sconfig/clenotes.cfg' % System.getProperty('jython.home'), "rU"))

The "rU" tells Python that even though it's running on a Unix system it should be prepared to cope with a file containing DOS line endings. See http://docs.python.org/library/functions.html#open -- scroll down to the paragraph that begins "In addition to the standard fopen() modes...".

(Or you could run this command: perl -pi.bak -e 's/[ \t\r]+$// clenotes.cfg -- that will convert it to Unix line endings. In your shoes I would probably do both.)

(If neither of the above suggestions helps, the next thing I would try is hitting clenotes.py itself with the above perl command. I don't see how that could be the problem, but if the \r characters are not coming from clenotes.cfg, the .py file is the only plausible remaining source.)

(EDIT: Based on your comments on the question itself, I now think it's clenotes.cmd, the shell script wrapper, that needs to be converted from DOS to Unix line endings.)

眼中杀气 2024-10-23 04:55:24

我必须继续寻找 \r 来自哪里。但与此同时,这个问题却变得简单多了。一旦解析了参数,请执行以下操作:

args = [arg.strip() for arg in args]

这将摆脱 \r

编辑:但是等等 - 这只是部分解决方案吗?它仍然无法正确解析选项吗?

EDIT2:似乎需要提前删除 \r 。当没有命令时,/r永远不会被剥离,因为上面的代码仅在getopt完成后剥离\r。这对我来说应该是显而易见的——而不是先在此处传递 sys.argv[1:]

opts, args = getopt.getopt(sys.argv[1:], '', cliOptions2)

进行修改,

argv = [arg.strip() for arg in sys.argv[1:]]
opts, args = getopt.getopt(argv, '', cliOptions2)

您也可以只执行 sys.argv[-1] = sys.argv [-1].strip()...但是我这个 C 程序员看到这个开始感到有点恶心。我知道这可能是不合理的。

或者只是按照 Zack 所说的那样将 clenotes.cmd 转换为 Linux 格式——但是,请注意,此处的剥离将确保其他人不必再次解决相同的问题。 (另一方面,这有点丑陋,或者至少对于那些没有预料到此类问题的人来说是神秘的。)

I'll have to keep looking to figure out where that \r is coming from. But in the meanwhile, this problem has become much simpler. Once the args are parsed, do this:

args = [arg.strip() for arg in args]

That will get rid of the \r

EDIT: But wait -- is this only a partial solution? Is it still not parsing options correctly?

EDIT2: Seems like the \r needs to be stripped earlier. When there's no command, the /r never gets stripped, because the above only strips \r after getopt is done. This should have been obvious to me before -- instead of passing sys.argv[1:] here

opts, args = getopt.getopt(sys.argv[1:], '', cliOptions2)

modify it first

argv = [arg.strip() for arg in sys.argv[1:]]
opts, args = getopt.getopt(argv, '', cliOptions2)

You could also just do sys.argv[-1] = sys.argv[-1].strip()... but the c programmer in me starts to feel a bit queasy looking at that. Probably irrational, I know.

Or just do what Zack said and convert clenotes.cmd to linux format -- however, note that stripping here will ensure that other people will not have to solve the same problem over again. (On the other hand, it's a little ugly, or at least mysterious to people not expecting such problems.)

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