view nagslang/options.py @ 98:93256a0987a2

Fix newer pep8 continuation complaint
author Neil Muller <drnlmuller@gmail.com>
date Mon, 02 Sep 2013 11:50:04 +0200
parents 17b233a54651
children 7b121ed73b95
line wrap: on
line source

import optparse
import os

from nagslang.constants import DEFAULTS


class AttrDict(dict):
    '''A dict with attribute access'''
    def __getattr__(self, attr):
        return self[attr]


options = AttrDict()


def parse_args(args):
    '''
    Parse arguments and store them in the options dictionary.

    Note: If you add arguments, you need to add an appropriate default to the
    DEFAULTS dict.
    '''
    options.update(DEFAULTS)

    options.debug = 'DEBUG' in os.environ

    parser = optparse.OptionParser()
    parser.add_option('--no-sound',
                      dest='sound', action='store_false', default=True,
                      help='Disable sound')
    if options.debug:
        parser.add_option('--area', help='Initial area')

    opts, _ = parser.parse_args(args)

    for k in DEFAULTS:
        if getattr(opts, k, None) is not None:
            options[k] = getattr(opts, k)