NewBie - GetOpts

Alexander Williams thantos at brimstone.mecha
Mon Sep 27 22:30:24 EDT 1999


On Tue, 28 Sep 1999 01:40:23 GMT, George Russell
<george.russell at clara.net> wrote:

>print optlist gives
>([('-f','filename')],[])
>
>I think this has two lists, one empty. Inside what data structure?

Specifically, its two lists inside a two element tuple.  See the
getopt library help and I believe you'll find that getopt returns a
tuple consisting of the first element which contains tuples of
parameters and the values passed for them, and the second element
which consists of the leftover parts of the command line.

To extract your filename then, do:

optionTuple, leftovers = optlist
filename = optionTuple[0][1]

Since you know you only have the single option, it can only have one
element tuple and you know the second element of /that/ one must be
the filename.  More complex option lists require more parsing,
generally looping over optionTuple.

-- 
Alexander Williams (thantos at gw.total-web.net)
"In the end ... Oblivion Always Wins."




More information about the Python-list mailing list