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

A.T.Hofkamp hat at se-162.se.wtb.tue.nl
Thu Jan 31 08:56:55 EST 2008


On 2008-01-30, grflanagan <grflanagan 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



More information about the Python-list mailing list