On 9/17/07, Ron Adam <[EMAIL PROTECTED]> wrote:
> Greg Ewing wrote:
> > Thomas Wouters wrote:
> >> If you want to put more meaning in the argv list, use an option
> >> parser.
> >
> > I want to put *less* meaning in it, not more. :-)
> > And using an argument parser is often overkill for
> > simple programs.
>
> Would it be possible to split out the (pre) parsing from optparse so that
> instead of returning a list, it returns a dictionary of attributes and values?
>
> This would only contain what was given in the command line as a first
> "lighter weight" step to parsing the command line.
>
>     opts = opt_parser(argv)
>     command_name = opts['argv0']   # better name for argv0?

You might look at argparse_ which allows you to treat positional
arguments just like optional ones.  So you'd write::

    parser = argparse.ArgumentParser()
    parser.add_argument('command') # positional argument
    parser.add_argument('--option') # optional argument
    args = parser.parse_args()
    ... args.command ...
    ... args.option ...

If you're really insistent on a dict interface instead of an attribute
interface, the object returned by parse_args() is just a simple
namespace, so vars(args) will give you a dict.

.. _argparse: http://argparse.python-hosting.com/

STeVe
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy
_______________________________________________
Python-3000 mailing list
Python-3000@python.org
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to