~crunch.io/ubuntu/precise/codespeak-lib/unstable

« back to all changes in this revision

Viewing changes to py/_test/parseopt.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-08-01 16:24:01 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100801162401-g37v49d1p148alpm
Tags: 1.3.3-1
* New upstream release.
* Bump Standards-Version to 3.9.1.
* Fix typo in py.test manpage.
* Prefer Breaks: over Conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""
2
 
thin wrapper around Python's optparse.py  
 
2
thin wrapper around Python's optparse.py
3
3
adding some extra checks and ways to systematically
4
4
have Environment variables provide default values
5
5
for options.  basic usage:
7
7
   >>> parser = Parser()
8
8
   >>> parser.addoption("--hello", action="store_true", dest="hello")
9
9
   >>> option, args = parser.parse(['--hello'])
10
 
   >>> option.hello 
 
10
   >>> option.hello
11
11
   True
12
12
   >>> args
13
13
   []
14
 
    
 
14
 
15
15
"""
16
16
import py
17
 
import optparse 
 
17
import optparse
18
18
 
19
19
class Parser:
20
 
    """ Parser for command line arguments. """ 
 
20
    """ Parser for command line arguments. """
21
21
 
22
22
    def __init__(self, usage=None, processopt=None):
23
23
        self._anonymous = OptionGroup("custom options", parser=self)
24
24
        self._groups = []
25
25
        self._processopt = processopt
26
 
        self._usage = usage 
 
26
        self._usage = usage
27
27
        self.hints = []
28
28
 
29
29
    def processoption(self, option):
44
44
            if grp.name == after:
45
45
                break
46
46
        self._groups.insert(i+1, group)
47
 
        return group 
 
47
        return group
48
48
 
49
 
    addgroup = getgroup 
 
49
    addgroup = getgroup
50
50
    def addgroup(self, name, description=""):
51
51
        py.log._apiwarn("1.1", "use getgroup() which gets-or-creates")
52
52
        return self.getgroup(name, description)
60
60
        groups = self._groups + [self._anonymous]
61
61
        for group in groups:
62
62
            if group.options:
63
 
                desc = group.description or group.name 
 
63
                desc = group.description or group.name
64
64
                optgroup = optparse.OptionGroup(optparser, desc)
65
65
                optgroup.add_options(group.options)
66
66
                optparser.add_option_group(optgroup)
78
78
        self.name = name
79
79
        self.description = description
80
80
        self.options = []
81
 
        self.parser = parser 
 
81
        self.parser = parser
82
82
 
83
83
    def addoption(self, *optnames, **attrs):
84
84
        """ add an option to this group. """
92
92
    def _addoption_instance(self, option, shortupper=False):
93
93
        if not shortupper:
94
94
            for opt in option._short_opts:
95
 
                if opt[0] == '-' and opt[1].islower(): 
 
95
                if opt[0] == '-' and opt[1].islower():
96
96
                    raise ValueError("lowercase shortoptions reserved")
97
97
        if self.parser:
98
98
            self.parser.processoption(option)
101
101
 
102
102
class MyOptionParser(optparse.OptionParser):
103
103
    def __init__(self, parser):
104
 
        self._parser = parser 
 
104
        self._parser = parser
105
105
        optparse.OptionParser.__init__(self, usage=parser._usage)
106
106
    def format_epilog(self, formatter):
107
 
        hints = self._parser.hints 
 
107
        hints = self._parser.hints
108
108
        if hints:
109
109
            s = "\n".join(["hint: " + x for x in hints]) + "\n"
110
110
            s = "\n" + s + "\n"