Python noob SOS (any [former?] Perlheads out there?)

grflanagan grflanagan at yahoo.co.uk
Thu Jan 31 10:34:00 EST 2008


On Jan 31, 2:56 pm, "A.T.Hofkamp" <h... at se-162.se.wtb.tue.nl> wrote:
> On 2008-01-30, grflanagan <grflana... at yahoo.co.uk> wrote:
>
>
>
> > On Jan 29, 5:39 pm, kj <so... at 987jk.com.invalid> wrote:
> > For command line options I get a long way with this:
>
> > [code python]
> > def _getargs():
> >     allargs = sys.argv[1:]
> >     args = []
> >     kwargs = {}
> >     key = None
> >     while allargs:
> >         arg = allargs.pop(0)
> >         if arg.startswith('--'):
> >             key, arg = arg.split('=', 1)
> >             key = key[2:]
> >         elif arg.startswith('-'):
> >             key = arg[1:]
> >             if not allargs or allargs[0].startswith('-'):
> >                 allargs.insert(0, 'yes')
> >             continue
> >         if key is None:
> >             args.append(arg)
> >         else:
> >             kwargs[key] = arg
> >             key = None
> >     return args, kwargs
>
> > ARGS, KWARGS = _getargs()
> > [/code]
>
> Have a look at getopt (old) or optparse (newer) packages in the Python library
> instead.
>
> Sincerely,
> Albert

Admittedly I haven't used either of them, but getopt and optparse have
always looked like too much work to me. 90% of the time my '_getargs'
does what I need, and when it's insufficient I go straight to a config
file (ConfigParser or ConfigObj).

Best Regards

Gerard



More information about the Python-list mailing list