optparse commandline

Discussion in 'Python' started by vvikram@gmail.com, Jul 8, 2007.

  1. Guest

    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
    , Jul 8, 2007
    #1
    1. Advertising

  2. 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.
    Steven D'Aprano, Jul 8, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NNTP
    Replies:
    5
    Views:
    463
  2. Manish Pansiniya
    Replies:
    6
    Views:
    490
    Natty Gur
    Jul 14, 2003
  3. matt

    Commandline XML parser

    matt, Jul 15, 2005, in forum: XML
    Replies:
    4
    Views:
    6,153
    grouch
    Aug 10, 2005
  4. suresh j
    Replies:
    0
    Views:
    339
    suresh j
    Dec 1, 2003
  5. idle.py commandline

    , Jul 23, 2004, in forum: Python
    Replies:
    1
    Views:
    382
    Christopher T King
    Jul 23, 2004
Loading...

Share This Page