Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass script parameters containing spaces or quotes to PyCharm?

I can run (for the command line)

myscript.py '--pm execute.shell(cmd="ls -l", nonInteractive=True)'

But, suppose I was to debug myscript.py with and set the "Script parameters:" field in the Edit Configurations Python dialog to:

--pm execute.shell(cmd="ls -l", nonInteractive=True)

it barfs at space between "ls" and "-l". Is there a way to workaround this? Using ' or " quotes seems not to do it:

--pm 'execute.shell(cmd="ls -l", nonInteractive=True)'
--pm "execute.shell(cmd='ls -l', nonInteractive=True)"

In both cases, the quotes end up in the arguments.


Update: Running:

import sys
for x in sys.argv:
    print "<%s>"%(x,)

with

Script parameters: a  \ b " c" "\ d" ' e' '\ f' "a 'b' \"c\""

gives:

<cmd.py>
<a>
<\>
<b>
< c>
<\ d>
<'>
<e'>
<'\>
<f'>
<a 'b' "c">

So it looks like:

  • escaping is ignored (\ is treated literally), except for preceding "
  • a non-escaped " starts a block that may contain spaces
  • ' is treated literally

So, the magic answer is:

Script parameters:  --pm "execute.shell(cmd='ls -l', nonInteractive=True)"

or

Script parameters:  --pm "execute.shell(cmd=\"ls -l\", nonInteractive=True)"
like image 534
user48956 Avatar asked Oct 30 '22 23:10

user48956


1 Answers

I am not 100% sure about the issue. Since I don't have commenting privileges, I am writing it as answer.

Changing "ls -l" to "ls-\ l" should work, at least on *nix systems.

The relevant documentation/bug report is at https://youtrack.jetbrains.com/issue/PY-4715 and https://youtrack.jetbrains.com/issue/IDEA-72632 which states that you need to escape spaces using system dependent methods, and not using a generic method. The proposal for a generic system independent method was apparently denied.

EDIT: --pm" "execute.shell(cmd=\"ls" "-l\"," "nonInteractive=True) worked.

like image 63
Akshat Harit Avatar answered Nov 08 '22 06:11

Akshat Harit