[Tutor] Command line arguments passing

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Dec 4 02:23:37 CET 2005



> > My question is: when invoking a program with, let's say, a filename
> > containing spaces as a parameter:
> >
> > myprog -file "Long name"
> >
> > What does sys.argv hold in this case? I am specifically interested in
> > whether argv[2]=="\"Long" or argv[2]=="Long name",


Hi Vlad,

What you're asking is a platform-specific thing.  I believe it should do
what you're expecting --- "Long name" should be a pulled together as a
single argument in sys.argv.  But it's not Python that's pulling "Long
name" together: it's your operating system's command line shell that's
doing this.

For example, on Windows, the following pages:

    http://www.microsoft.com/technet/community/columns/scripts/sg0704.mspx
    http://www.microsoft.com/technet/archive/winntas/deploy/shellscr.mspx

talk about how Windows does command line argument parsing.  (Search those
pages for the word "quote", and you'll see a mention of this.) And the
details on the role of quoting arguments is simliar for Unix shells like
'bash' or 'tcsh'.  For example, for the bash shell:

    http://www.gnu.org/software/bash/manual/bashref.html#SEC8


So all Python knows is that it's getting an array of strings: it doesn't
even see the original line that the user typed at the command line prompt;
it instead gets something that has already been partially digested by your
command line shell.


Hope this helps!



More information about the Tutor mailing list