optparse commandline

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sun Jul 8 06:49:32 EDT 2007


On Sun, 08 Jul 2007 09:59:29 +0000, vvikram wrote:

> 
> I want to call some function from my program and just pass it a
> commandline. It should parse the commandline and return back a list of
> parsed arguments (just like the sys.argv list)
> 
> Example:
> foo.parse_cmdline("/usr/bin/foorun -v -d -h")
> ==> ['/usr/bin/foorun', '-v', '-d','-h']
> 
> Any suggestions on how to do this are appreciated.

def parse_cmdline(s):
    """Split a command line string s into words."""
    import shlex
    return shlex.split(s)




-- 
Steven.




More information about the Python-list mailing list