Raw command line arguments

E

Enchanter

How to pass the raw command line arguments to the python?

Such as:

mypython.py txt -c "Test Only" {Help}


The arguments I hope to get is:

txt -c "Test Only" {Help} -- Keep the
quotation marks in the arguments.
 
C

Chris Rebert

How to pass the raw command line arguments to the python?

Such as:

    mypython.py  txt -c "Test Only" {Help}


The arguments I hope to get is:

             txt -c "Test Only" {Help}               -- Keep the
quotation marks in the arguments.

Remember that the quotes are parsed by the shell before python ever
receives the arguments, so you have to deal with it in whatever is
calling python.

Recall/note the differences between:
rm foo bar #delete foo and delete bar
rm "foo bar" #delete one file with a space in its name but no quotes in its name
rm 'foo"bar"baz' #delete one file with double-quotes in its name
rm "foo\"bar\"baz" #delete the same file as in the previous example

Therefore, you need to invoke the script differently to take account
of the shell's quote processing by adding another set of quotes, like
so:
mypython.py txt -c '"Test Only"' {Help}
#or alternatively:
mypython.py txt -c "\"Test Only\"" {Help}

Cheers,
Chris
 
D

Dave Angel

Enchanter said:
How to pass the raw command line arguments to the python?

Such as:

mypython.py txt -c "Test Only" {Help}


The arguments I hope to get is:

txt -c "Test Only" {Help} -- Keep the
quotation marks in the arguments.
As Chris has said, the shell (and the C runtime) preprocesses the line
before python ever sees it. However, if you're on Windows, there is a
function GetCommandLine, located in Kernel32.dll, which I *think* keeps
the raw bytes typed by the user. Clearly, that would be a non-portable
answer, but here's some samples if you want to try it:

import sys
print sys.argv #this one loses the quotes

import ctypes
#print ctypes.windll.kernel32.GetModuleHandleA
#hdl = ctypes.windll.kernel32.GetModuleHandleA(0)
#print hdl

func = ctypes.windll.kernel32.GetCommandLineA
print func
func.restype = ctypes.c_char_p

cmd = ctypes.windll.kernel32.GetCommandLineA()
print cmd #this one gives a single string, such as the one below:

"C:\ProgFiles\Python26\python.exe"
"M:\Programming\Python\sources\dummy\args.py" txt -c "Test Only" (Help)

If you do go this route, but still want to be portable, you could make
this code conditional on platform, then document the user to use
escaping on Linux and Apple, while you do this approach for Windows.
 
G

Gabriel Genellina

How to pass the raw command line arguments to the python?

That depends on the OS or the shell you're using.
Such as:

mypython.py txt -c "Test Only" {Help}


The arguments I hope to get is:

txt -c "Test Only" {Help} -- Keep the
quotation marks in the arguments.

I guess you're using Windows:

gg>type show.py
import sys
print sys.argv

gg>python show.py one txt -c """Test Only""" {Help}
['show.py', 'one', 'txt', '-c', '"Test Only"', '{Help}']

Two double quotes represent a single one. And you have to enclose the
whole argument in quotes too because of the space character.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top