Re: NOT handling command-line arguments

Guido.van.Rossum@cwi.nl
Tue, 26 Jul 1994 13:25:04 +0200

> I would like to be able to pass command-line arguments to a Python
> script for interpretation within the script (rather than by Python
> itself), so that I can use #!/usr/local/bin/python line at the top
> of the script file, and have the whole thing in a single file. Is
> there some way to stop Python interpreting command-line arguments,
> or some way to pass those arguments to a #!'ed script without them
> being examined by Python? A clean way would be preferred, but I'm
> willing to get grubby :-).

Am I correct that if you execute the following executable script

#! /usr/local/bin/python
import sys
print sys.argv

with a command line argument of -a, say, you get a usage message from
Python? You should get this output (assuming the script was called
t.py):

['./t.py', '-a']

If you don't, you have probably linked with the GNU getopt, which
scans the entire command line for options rather than stopping at the
first non-option. There's a getopt in the distribution
(Python/getopt.c) which you may add to the Makefile. You can also
change the first line of the script into

#! /usr/local/bin/python --

which should tell getopt to stop parsing.

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
<URL:http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>