Tim Roberts <t...@probo.com> wrote:

> Bill Janssen wrote:
> > I'm actually not passing file names.  I'm passing argument strings,
> > which may contain spaces, quotes, and other things.  For instance,
> >
> >    myprogram --title="That's the game! says Mike "Hammer" Brotsky" 
> > --file=...
> >
> > "myprogram" is a Python program, and expects to get the whole "--title"
> > argument as a single element of sys.argv.
> >
> > On Unix, I use
> >
> >    arg = "--title=%s" % pipes.quote(title)
> >
> > to achieve that effect.  What's the equivalent on Windows?  Since
> > cmd.exe also supports pipelines, I'd sort of expect it to do the right
> > thing on Windows, too.
> >   
> 
> Ah, foolish mortal.

Quixotic, perhaps, to attempt to achieve consistency on Windows...
Actually, my last sentence there was more about, "Why doesn't the pipes
module also work on Windows?", than it was about quoting.

> The problem with Windows is that, as I said, there is no central
> command-line parser, so there is no single set of rules.  Cmd.exe does
> not parse the command line.  Cmd.exe does not create argc/argv.  Cmd.exe
> just passes the whole command line as a string.  For C programs, the C
> run-time library will crack that string into argc/argv.  Python on
> Windows has its own parser.

The subprocess module has an idea of how it should work.  Whether or not
it's correct is a different matter.  So, is the algorithm the same for
the visual studio C runtime's parser, and Python's parser?  I'd expect
that, since CPython is a C program that uses VS C.  If so, I'd be happy
to settle for that "standard".

The algorithm in the subprocess module is documented thusly:

    Translate a sequence of arguments into a command line
    string, using the same rules as the MS C runtime:

    1) Arguments are delimited by white space, which is either a
       space or a tab.

    2) A string surrounded by double quotation marks is
       interpreted as a single argument, regardless of white space
       or pipe characters contained within.  A quoted string can be
       embedded in an argument.

    3) A double quotation mark preceded by a backslash is
       interpreted as a literal double quotation mark.

    4) Backslashes are interpreted literally, unless they
       immediately precede a double quotation mark.

    5) If backslashes immediately precede a double quotation mark,
       every pair of backslashes is interpreted as a literal
       backslash.  If the number of backslashes is odd, the last
       backslash escapes the next double quotation mark as
       described in rule 3.
    """

    # See
    # http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
    # or search http://msdn.microsoft.com for
    # "Parsing C++ Command-Line Arguments"

Bill
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to