len(sys.argv) in (3,4)

John Machin sjmachin at lexicon.net
Thu Aug 11 17:47:53 EDT 2005


bruno modulix wrote:
> Daniel Schüle wrote:
> 
>>Hello
>>
>>I wrote a simple module, which is also supposed to be used as standalone 
>>program
>>after considering how to avoid multiple if's I came up with this idea
>>
>>if __name__ == "__main__":
>>    if len(sys.argv) not in (3,4):
>>        print "usage: prog arg1 argv2 [-x]"
>>    # etc ...
>>
>>while develeoping I had my interpeter running and reloaded module
>>now since I am ready, I wanted to run it from cmd windows shell
>>but it always prints "usage ..."
>>I added print len(sys.arg) and it prints 1 though I am provinding more
>>than 1 value
>>
>>
>>C:\pool\vhd2h\python>vhd2h.py vhd.vhd vhd.h -o
>>1
>>usage: vhd2h.py vhdlFile hFile [-o]
>>
>>Someone got an idea what may be wrong?
> 
> 
> Nope. But since you're running this on a very peculiar OS, I just can
> guess that this very peculiar OS consider all args to be one same string...

NOT SO:

C:\junk>python -i - foo /bar -zot
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> import sys
 >>> sys.argv
['-', 'foo', '/bar', '-zot']

NO REASON TO GUESS SO:

For *any* OS: More than one CLI (command line interpreter) a.k.a. shell 
may be available. What they do with the remainder of the command line 
after the pathname of the executable (binary program or script or 
whatever) is up to them, but I have never seen any which would lump 
space-separated tokens into one arg without some quoting convention 
having to be used.

> 
> Try this:
> if __name__ == "__main__":
>   for num, arg in enumerate(sys.argv):
>      print "arg %d is %s" % (num, arg)
> 
> This should art least give you some hints...
> 
> BTW, isn't the DOS syntax for command line args something like :
> 
>>myprog /arg1 /arg2

Which DOS do you mean? IBM DOS/VS? AmigaDOS? MS-DOS?

The likelihood is that the OP is not running any of these, but is 
running a recent version of Windows. The standard CLI (cmd.exe) does use 
a syntax for built-in commands where '/' is used for options where a *x 
shell would use '-'; however this is sublimely irrelevant.

Python scripts get their args on Windows just like anywhere else. C 
programs (like the excellent collection of GnuWin32 utilities) likewise 
just do main(argc, argv) {blah; blah} and carry on regardless just as 
K&R intended.



More information about the Python-list mailing list