Search code examples
pythonsubprocess

Program invoked with subprocess.Popen() does not recognize two arguments passed as one


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?


Solution

  • 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"]