How to Loop over a optparse.OptionGroup values in python

1.8k views Asked by At

I'm trying to use optparse to make a command line tool in python I have a group of options optparse.OptionGroup that I wan't to loop over to do whatever... but how do I do that ? I have:

usage = ("polotools [options]")
parser = optparse.OptionParser(version="polotools %s" % version, usage=usage)
parser.add_option('--amrsim', dest='amrsim', action='store_true',
    help=('Set amr simulation mode, skips if not present'))    

groupAMR = optparse.OptionGroup(parser,'AMR simulation:',
                "ATENTION: use these options only with --amrsim")
groupAMR.add_option('--Utility', dest='Utility', action='store',
    help=('Set utility rate for AMR simulation, accept dictionary'))

parser.add_option_group(groupAMR)

(options, args) = parser.parse_args()

But in options all options get grouped togheter.. and I wan't to filter only the ones in groupAMR.

1

There are 1 answers

0
arussell84 On BEST ANSWER

You probably want something like this after you set options:

for groupAMR_arg in groupAMR.option_list:
    print getattr(options, groupAMR_arg.dest)