Advertisement
gene_wood

Untitled

Jul 6th, 2011
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 1.23 KB | None | 0 0
  1. --- a   2011-07-06 15:13:22.806839373 -0700
  2. +++ b   2011-07-06 15:12:42.690701304 -0700
  3. @@ -1,6 +1,7 @@
  4.  #! /usr/bin/env python
  5.  __doc__ = """
  6.  example of cascading options with ConfigParser and OptionParser
  7. +from : http://thoughtsbyclayg.blogspot.com/2009/10/python-over-riding-options-with.html
  8.  """
  9.  
  10.  # imports
  11. @@ -33,18 +34,17 @@
  12.      # read config objects defaults section into a dictionary
  13.      config_options = config.defaults()
  14.      # config_options is dictionary of strings, over-ride toggle to bool
  15. -    config_options['toggle'] = config.getboolean('DEFAULT', 'toggle')
  16. +    for dest in [option.dest for option in parser.option_list if option.action.lower() in ('store_true', 'store_false')]:
  17. +        if config.has_option('DEFAULT', dest) and not isinstance(config_options[dest], bool):
  18. +            config_options[dest] = config.getboolean('DEFAULT',dest)
  19. +
  20.      # feed dictionary of defaults into parser object
  21.      parser.set_defaults(**config_options)
  22.  
  23.      # parse command line options
  24.      (options, args) = parser.parse_args()
  25.  
  26. -    print "option: %s" % options.option
  27. -    if options.toggle:
  28. -        print "toggle is ON"
  29. -    else:
  30. -        print "toggle is OFF"
  31. +    print parser.values
  32.  
  33.      return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement