英文:
Program invoked with subprocess.Popen() does not recognize two arguments passed as one
问题
我有一个在WSL2上运行的Python脚本中的函数,并且以超级用户身份运行它:
import subprocess
def flash() -> None:
p = subprocess.Popen(
["JLinkExe", ""],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
text=True
)
# 从JLinkExe读取响应
response = p.stdout.read()
print(response)
如你所见,我尝试运行JLinkExe
程序,它是一个带有自己的Shell J-Link>
的命令行实用程序。出于某种原因,我的print()
函数输出如下:
SEGGER J-Link Commander V7.60(编译于2021年12月14日11:40:17)
DLL版本V7.60,编译于2021年12月14日11:40:00
未知的命令行选项。
前两行表明应用程序JLinkExe
确实运行了,但由于某种原因,在参数的末尾附加了.
,或者不识别空参数,这很奇怪。
如果我更改此行:
["JLinkExe", "-nogui 1"]
响应也会更改:
SEGGER J-Link Commander V7.60(编译于2021年12月14日11:40:17)
DLL版本V7.60,编译于2021年12月14日11:40:00
未知的命令行选项 -nogui 1。
再次出现.
附加在参数的末尾...
如果我以超级用户身份直接在WSL中运行相同的命令,一切都正常:
$ sudo JLinkExe -nogui 1
SEGGER J-Link Commander V7.60(编译于2021年12月14日11:40:17)
DLL版本V7.60,编译于2021年12月14日11:40:00
通过USB连接到J-Link...O.K.
固件:J-Link STLink V21(编译于2019年8月12日10:29:20)
硬件版本:V1.00
S/N:775087052
VTref=3.300V
输入“connect”以建立目标连接,输入“?”以获取帮助
J-Link>
为什么我的参数未被识别?
英文:
I have this function in python script on WSL2 and I run it as a super user:
import subprocess
def flash() -> None:
p = subprocess.Popen(
["JLinkExe", ""],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
text=True
)
# Read the response from JLinkExe
response = p.stdout.read()
print(response)
As you can see I try to run program JLinkExe
which is a command line utility with it's own shell J-Link>
. For some reason my print()
function prints this:
SEGGER J-Link Commander V7.60 (Compiled Dec 14 2021 11:40:17)
DLL version V7.60, compiled Dec 14 2021 11:40:00
Unknown command line option .
First two lines indicate that application JLinkExe
did run, but for some reason there is a .
appended at the back of the arguments or empty argument is not recognized which is weird.
If I change this line:
["JLinkExe", "-nogui 1"]
The response also changes:
SEGGER J-Link Commander V7.60 (Compiled Dec 14 2021 11:40:17)
DLL version V7.60, compiled Dec 14 2021 11:40:00
Unknown command line option -nogui 1.
Again .
is appended to the back of the arguments...
If I run the same command as a super user directly in WSL everything looks fine:
$ sudo JLinkExe -nogui 1
SEGGER J-Link Commander V7.60 (Compiled Dec 14 2021 11:40:17)
DLL version V7.60, compiled Dec 14 2021 11:40:00
Connecting to J-Link via USB...O.K.
Firmware: J-Link STLink V21 compiled Aug 12 2019 10:29:20
Hardware version: V1.00
S/N: 775087052
VTref=3.300V
Type "connect" to establish a target connection, '?' for help
J-Link>
Why are my arguments not recognized?
答案1
得分: 1
没有将.
附加到参数中,这是JLinkExe
命令的输出,它是一个以.
结尾的英语句子:
Unknown command line option <argument>.
""
和"-nogui 1"
都不是JLinkExe
的有效参数,但"-nogui"
是有效参数,它期望另一个参数"1"
。
你需要两个单独的参数:
["JLinkExe", "-nogui", "1"]
如果你只想调用JLinkExe
而不传递任何参数,不要将空字符串作为参数传递:
["JLinkExe"]
英文:
There is no .
appended to the arguments, that's the output of the JLinkExe
command which is an English sentence which ends with a .
:
Unknown command line option <argument>.
Neither ""
nor "-nogui 1"
are valid arguments for JLinkExe
, but "-nogui"
is, which expects another one, "1"
.
You need two separate arguments:
["JLinkExe", "-nogui", "1"]
If you want to invoke just JLinkExe
with no arguments, don't pass an empty string as an argument:
["JLinkExe"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论