I'm using the subprocess.Popen
call, and in another question I found out that I had been misunderstanding how Python was generating arguments for the command line.
My Question
Is there a way to find out what the actual command line was?
Example Code :-
proc = subprocess.popen(....)
print "the commandline is %s" % proc.getCommandLine()
How would you write getCommandLine
?
Beautiful and scalable method
I have been using something like this:
Sample run:
Output:
Feature summary:
+
to commands likesh -x
so users can differentiate commands from their output easilycd
, and extra environment variables if they are given to the command. These only printed if given, generating a minimal shell command.All of this allows users to easily copy the commands manually to run them if something fails, or to see what is going on.
Tested on Python 3.5.2, Ubuntu 16.04. GitHub upstream.
The correct answer to my question is actually that there IS no command line. The point of subprocess is that it does everything through IPC. The list2cmdline does as close as can be expected, but in reality the best thing to do is look at the "args" list, and just know that that will be argv in the called program.
It depends on the version of Python you are using. In Python3.3, the arg is saved in
proc.args
:In Python2.7, the
args
not saved, it is just passed on to other functions like_execute_child
. So, in that case, the best way to get the command line is to save it when you have it:Note that if you have the list of arguments (such as the type of thing returned by
shlex.split(cmd)
, then you can recover the command-line string,cmd
using the undocumented functionsubprocess.list2cmdline
: