[Python-3000] Move argv[0]? (Re: Unicode and OS strings)
Steven Bethard
steven.bethard at gmail.com
Tue Sep 18 08:40:47 CEST 2007
On 9/17/07, Ron Adam <rrr at ronadam.com> 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
More information about the Python-3000
mailing list