468,161 Members | 2,019 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,161 developers. It's quick & easy.

Get the complete command line as-is

Hello,

I'm passing command line parameters to my browser, I need to pass the
complete command line as-is, for example:

test.py "abc def" xyz

If I use ' '.join(sys.argv[1:]), then the double quotes around "abc
def" is gone, but I need to pass the complete command line ("abc def"
xyz) to the browser, how can I do this?

I'm on Windows.

Thanks.

Sep 12 '07 #1
11 6047
wangzq wrote:
Hello,

I'm passing command line parameters to my browser, I need to pass the
complete command line as-is, for example:

test.py "abc def" xyz

If I use ' '.join(sys.argv[1:]), then the double quotes around "abc
def" is gone, but I need to pass the complete command line ("abc def"
xyz) to the browser, how can I do this?

I'm on Windows.

Thanks.
You can't. The quotes are gobbled by the command-line processor before
your program ever gets the chance to see them. You could try writing
your own command shell ... or learn how to quote the quotes.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Sep 12 '07 #2
On Sep 11, 8:00 pm, wangzq <wan...@gmail.comwrote:
Hello,

I'm passing command line parameters to my browser, I need to pass the
complete command line as-is, for example:

test.py "abc def" xyz

If I use ' '.join(sys.argv[1:]), then the double quotes around "abc
def" is gone, but I need to pass the complete command line ("abc def"
xyz) to the browser, how can I do this?

I'm on Windows.

Thanks.
I'm on Solaris and this works for me:

test.py '"abc def" xyz'

print sys.arv[1]

"abc def" xyz

Sep 12 '07 #3
On Sep 11, 8:33 pm, TheFlyingDutchman <zzbba...@aol.comwrote:
On Sep 11, 8:00 pm, wangzq <wan...@gmail.comwrote:
Hello,
I'm passing command line parameters to my browser, I need to pass the
complete command line as-is, for example:
test.py "abc def" xyz
If I use ' '.join(sys.argv[1:]), then the double quotes around "abc
def" is gone, but I need to pass the complete command line ("abc def"
xyz) to the browser, how can I do this?
I'm on Windows.
Thanks.

I'm on Solaris and this works for me:

test.py '"abc def" xyz'

print sys.arv[1]

"abc def" xyz
OK, now I'm on Windows XP and things aren't looking too good.

It seems that \" will retain the quote marks but then the spaces get
gobbled.

But if you replace the spaces with another character:

python.exe test.py \"abc#def\"#123

then:

import sys
commandLine = "".join(sys.argv[1:])

prints commandLine.replace('#',' ')

gives:

"abc def" 123

Don't know if you can use that technique or not.

Sep 12 '07 #4
>
It seems that \" will retain the quote marks but then the spaces get
gobbled.

But if you replace the spaces with another character:

python.exe test.py \"abc#def\"#123

then:

import sys
commandLine = "".join(sys.argv[1:])

prints commandLine.replace('#',' ')

gives:

"abc def" 123

Don't know if you can use that technique or not.
Came back for a second try and found out that:

python.exe test.py "\"abc def\" 123"

import sys
commandLine = "".join(sys.argv[1:])

print commandLine

gives:

"abc def" 123

Sep 12 '07 #5
>
python.exe test.py "\"abc def\" 123"

import sys
commandLine = "".join(sys.argv[1:])

print commandLine

gives:

"abc def" 123
With the surrounding quotes you actually only need:

commandLine = sys.argv[1]

Sep 12 '07 #6
wangzq a écrit :
Hello,

I'm passing command line parameters to my browser, I need to pass the
complete command line as-is, for example:

test.py "abc def" xyz

If I use ' '.join(sys.argv[1:]), then the double quotes around "abc
def" is gone, but I need to pass the complete command line ("abc def"
xyz) to the browser, how can I do this?

I'm on Windows.
As Windows command-line parsing seem to remove some chars, maybe you can
try to use the GetCommandLine() function from Win32 API (I dont know if
it is available in pywin32 package - you may need to write a wrapper
with ctypes).

See http://msdn2.microsoft.com/en-us/library/ms683156.aspx
A+

Laurent.
Sep 12 '07 #7
wangzq wrote:
Hello,

I'm passing command line parameters to my browser, I need to pass the
complete command line as-is, for example:

test.py "abc def" xyz

If I use ' '.join(sys.argv[1:]), then the double quotes around "abc
def" is gone, but I need to pass the complete command line ("abc def"
xyz) to the browser, how can I do this?

I'm on Windows.

Thanks.
Question: If you don't want to parse this as a command line, why don't you read
it as data via raw input or from some configuration file?

something like:

python program (pgm.py):

cmdline=raw_input("enter your parameters")
external file (cmd.txt):

"abc def" xyz

python pgm.py < cmd.txt

-Larry
Sep 12 '07 #8
On Sep 12, 3:20 pm, Laurent Pointal <laurent.poin...@limsi.frwrote:
wangzq a écrit :
Hello,
I'm passing command line parameters to my browser, I need to pass the
complete command line as-is, for example:
test.py "abc def" xyz
If I use ' '.join(sys.argv[1:]), then the double quotes around "abc
def" is gone, but I need to pass the complete command line ("abc def"
xyz) to the browser, how can I do this?
I'm on Windows.

As Windows command-line parsing seem to remove some chars, maybe you can
try to use the GetCommandLine() function from Win32 API (I dont know if
it is available in pywin32 package - you may need to write a wrapper
with ctypes).

Seehttp://msdn2.microsoft.com/en-us/library/ms683156.aspx

A+

Laurent.
Thank you for the tip. It works:

import ctypes

p = ctypes.windll.kernel32.GetCommandLineA()
print ctypes.c_char_p(p).value

Now I only need to split the whole command line into 3 parts and get
the last one.

Thanks for all your input.
Sep 13 '07 #9
wangzq wrote:
On Sep 12, 3:20 pm, Laurent Pointal <laurent.poin...@limsi.frwrote:
>wangzq a écrit :
>>Hello,
I'm passing command line parameters to my browser, I need to pass the
complete command line as-is, for example:
test.py "abc def" xyz
If I use ' '.join(sys.argv[1:]), then the double quotes around "abc
def" is gone, but I need to pass the complete command line ("abc def"
xyz) to the browser, how can I do this?
I'm on Windows.
As Windows command-line parsing seem to remove some chars, maybe you can
try to use the GetCommandLine() function from Win32 API (I dont know if
it is available in pywin32 package - you may need to write a wrapper
with ctypes).

Seehttp://msdn2.microsoft.com/en-us/library/ms683156.aspx

A+

Laurent.

Thank you for the tip. It works:

import ctypes

p = ctypes.windll.kernel32.GetCommandLineA()
print ctypes.c_char_p(p).value

Now I only need to split the whole command line into 3 parts and get
the last one.
Well, FWIW, it is exposed by pywin32:

<code>
import win32api
print win32api.GetCommandLine ()

</code>

TJG
Sep 13 '07 #10
wangzq schrieb:
On Sep 12, 3:20 pm, Laurent Pointal <laurent.poin...@limsi.frwrote:
>wangzq a écrit :
Hello,
I'm passing command line parameters to my browser, I need to pass the
complete command line as-is, for example:
test.py "abc def" xyz
If I use ' '.join(sys.argv[1:]), then the double quotes around "abc
def" is gone, but I need to pass the complete command line ("abc def"
xyz) to the browser, how can I do this?
I'm on Windows.

As Windows command-line parsing seem to remove some chars, maybe you can
try to use the GetCommandLine() function from Win32 API (I dont know if
it is available in pywin32 package - you may need to write a wrapper
with ctypes).

Seehttp://msdn2.microsoft.com/en-us/library/ms683156.aspx

A+

Laurent.

Thank you for the tip. It works:

import ctypes

p = ctypes.windll.kernel32.GetCommandLineA()
print ctypes.c_char_p(p).value
Better would be this code:

import ctypes
ctypes.windll.kernel32.GetCommandLineA.restype = ctypes.c_char_p
print ctypes.windll.kernel32.GetCommandLineA()

Thomas

Sep 13 '07 #11
Thomas Heller wrote:
Better would be this code:

import ctypes
ctypes.windll.kernel32.GetCommandLineA.restype = ctypes.c_char_p
print ctypes.windll.kernel32.GetCommandLineA()
Or you could use pywin32:

<code>
import win32api

print win32api.GetCommandLine ()

</code>

TJG
Sep 13 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Ken Innes | last post: by
6 posts views Thread by leegold2 | last post: by
6 posts views Thread by Hari | last post: by
25 posts views Thread by David Bernier | last post: by
16 posts views Thread by John Salerno | last post: by
2 posts views Thread by Milan | last post: by
reply views Thread by Tharpa Roberts | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.