[英]python sh library, commands with hyphen/dash
對於名稱中包含破折號的命令,例如/ usr / bin / google-chrome,請將破折號替換為下划線:
我正在嘗試運行該命令
git rev-parse --abbrev-ref HEAD
當我嘗試運行該命令時,git返回錯誤,指出我的命令錯誤。 有辦法解決這個問題嗎?
>>> from sh import git
>>> git.rev_parse('--abbrev-ref', 'HEAD')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/sh.py", line 769, in __call__
return RunningCommand(cmd, call_args, stdin, stdout, stderr)
File "/Library/Python/2.7/site-packages/sh.py", line 330, in __init__
self.wait()
File "/Library/Python/2.7/site-packages/sh.py", line 334, in wait
self._handle_exit_code(self.process.wait())
File "/Library/Python/2.7/site-packages/sh.py", line 348, in _handle_exit_code
self.process.stderr
sh.ErrorReturnCode_1:
RAN: '/usr/bin/git rev_parse --abbrev-ref HEAD'
STDOUT:
STDERR:
git: 'rev_parse' is not a git command. See 'git --help'.
Did you mean this?
rev-parse
>>>
我不確定為什么它不起作用。
但是,我發現這有效:
git('rev-parse', '--abrev-ref', 'HEAD')
導致:
RAN: '/usr/bin/git rev-parse --abrev-ref HEAD'
替換規則僅適用於命令本身 - git
- 不適用於諸如'rev-parse'
。 這樣做是因為在Python函數名稱中不可能使用破折號,但在選項中完全可能。
@runDOSrun找到了一個解決方案:
git('rev-parse', '--abrev-ref', 'HEAD')
也就是說,假設您使用子命令語法隱式傳遞rev-parse
,如下所示:
git.rev_parse('--abrev-ref', 'HEAD')
在這種情況下,下划線是合適的,因為您將通過Python令牌添加子命令,僅限於通常的字符集(不包括破折號!)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.