getopt and options with multiple arguments

Discussion in 'Python' started by pinkfloydhomer@gmail.com, Dec 19, 2005.

  1. Guest

    I want to be able to do something like:

    myscript.py * -o outputfile

    and then have the shell expand the * as usual, perhaps to hundreds of
    filenames. But as far as I can see, getopt can only get one argument
    with each option. In the above case, there isn't even an option string
    before the *, but even if there was, I don't know how to get getopt to
    give me all the expanded filenames in an option.

    Help! :)

    /David
     
    , Dec 19, 2005
    #1
    1. Advertising

  2. On 19 Dec 2005 02:29:41 -0800,
    <> wrote:
    > I want to be able to do something like:
    >
    > myscript.py * -o outputfile
    >
    > and then have the shell expand the * as usual, perhaps to hundreds of
    > filenames. But as far as I can see, getopt can only get one argument
    > with each option. In the above case, there isn't even an option string
    > before the *, but even if there was, I don't know how to get getopt to
    > give me all the expanded filenames in an option.
    >
    > Help! :)


    You could use the glob module to expand the asterisk yourself.

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Dec 19, 2005
    #2
    1. Advertising

  3. PoD Guest

    On Mon, 19 Dec 2005 02:29:41 -0800, wrote:

    > I want to be able to do something like:
    >
    > myscript.py * -o outputfile
    >
    > and then have the shell expand the * as usual, perhaps to hundreds of
    > filenames. But as far as I can see, getopt can only get one argument
    > with each option. In the above case, there isn't even an option string
    > before the *, but even if there was, I don't know how to get getopt to
    > give me all the expanded filenames in an option.
    >
    > Help! :)
    >
    > /David


    The convention is to put options first and then any other stuff such as
    filenames:-
    myscript.py -o outputfile *

    In this case getopt will return the options as one list and the rest as
    another list.
     
    PoD, Dec 19, 2005
    #3
  4. On Mon, 19 Dec 2005 02:29:41 -0800, wrote:

    > I want to be able to do something like:
    >
    > myscript.py * -o outputfile
    >
    > and then have the shell expand the * as usual, perhaps to hundreds of
    > filenames.


    If you are calling this from most Linux and Unix shells, the shell will
    already have expanded the * and myscript.py will never see the asterisk.


    > But as far as I can see, getopt can only get one argument
    > with each option.


    The getopt module has two main functions you probably want to call.

    getopt.getopt expects options like -o arg followed by any remaining args.

    getopt.gnu_getopt allows mixed args and options.

    But frankly, the easiest way of dealing with your problem is just to
    change your user-interface. What is wrong with using

    myscript.py -o outputfile *

    instead?

    > In the above case, there isn't even an option string
    > before the *, but even if there was, I don't know how to get getopt to
    > give me all the expanded filenames in an option.


    If you use a shell that doesn't expand the filenames, you can use the glob
    module to expand it yourself.



    --
    Steven.
     
    Steven D'Aprano, Dec 19, 2005
    #4
  5. Tom Anderson Guest

    On Mon, 19 Dec 2005, wrote:

    > I want to be able to do something like:
    >
    > myscript.py * -o outputfile
    >
    > and then have the shell expand the * as usual, perhaps to hundreds of
    > filenames. But as far as I can see, getopt can only get one argument
    > with each option. In the above case, there isn't even an option string
    > before the *, but even if there was, I don't know how to get getopt to
    > give me all the expanded filenames in an option.


    I'm really surprised that getopt doesn't handle this properly by default
    (so getopt.getopt mimics unices with crappy getopts - since when was that
    a feature?), but as Steven pointed out, getopt.gnu_getopt will float your
    boat.

    I have an irrational superstitious fear of getopt, so this is what i use
    (it returns a list of arguments, followed by a dict mapping flags to
    values; it only handles long options, but uses a single dash for them, as
    is, for some reason, the tradition in java, where i grew up):

    def arguments(argv, expand=True):
    argv = list(argv)
    args = []
    flags = {}
    while (len(argv) > 0):
    arg = argv.pop(0)
    if (arg == "--"):
    args.extend(argv)
    break
    elif (expand and arg.startswith("@")):
    if (len(arg) > 1):
    arg = arg[1:]
    else:
    arg = argv.pop(0)
    argv[0:0] = list(stripped(file(arg)))
    elif (arg.startswith("-") and (len(arg) > 1)):
    arg = arg[1:]
    if (":" in arg):
    key, value = arg.split(":")
    else:
    key = arg
    value = ""
    flags[key] = value
    else:
    args.append(arg)
    return args, flags

    def stripped(f):
    """Return an iterator over the strings in the iterable f in which
    strings are stripped of #-delimited comments and leading and
    trailing whitespace, and blank strings are skipped.

    """
    for line in f:
    if ("#" in line): line = line[:line.index("#")]
    line = line.strip()
    if (line == ""): continue
    yield line
    raise StopIteration

    As a bonus, you can say @foo or @ foo to mean "insert the lines contained
    in file foo in the command line here", which is handy if, say, you have a
    file containing a list of files to be processed, and you want to invoke a
    script to process them, or if you want to put some standard flags in a
    file and pull them in on the command line. Yes, you could use xargs for
    this, but this is a bit easier. If you don't want this, delete the elif
    block mentioning the @, and the stripped function. A slightly neater
    implementation not involving list.pop also then becomes possible.

    tom

    --
    Hit to death in the future head
     
    Tom Anderson, Dec 21, 2005
    #5
    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. Shaun Jackman

    Calling getopt with new arguments

    Shaun Jackman, Jul 23, 2005, in forum: C Programming
    Replies:
    1
    Views:
    396
    Chris Croughton
    Jul 23, 2005
  2. Rocky Zhou
    Replies:
    3
    Views:
    316
    Steven Bethard
    Mar 18, 2007
  3. Sijo Kg
    Replies:
    1
    Views:
    102
    Justin Collins
    Jan 7, 2009
  4. mothra
    Replies:
    3
    Views:
    345
    Shawn Corey
    Dec 2, 2004
  5. nahum_barnea
    Replies:
    3
    Views:
    128
    Peter Makholm
    Sep 21, 2010
Loading...

Share This Page