optparse commandline

V

vvikram

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.

Regards
Vikram
 
S

Steven D'Aprano

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top