| 178 | |
| 179 | |
| 180 | class Option(LeafPattern): |
| 181 | |
| 182 | def __init__(self, short=None, long=None, argcount=0, value=False): |
| 183 | assert argcount in (0, 1) |
| 184 | self.short, self.long, self.argcount = short, long, argcount |
| 185 | self.value = None if value is False and argcount else value |
| 186 | |
| 187 | @classmethod |
| 188 | def parse(class_, option_description): |
| 189 | short, long, argcount, value = None, None, 0, False |
| 190 | options, _, description = option_description.strip().partition(' ') |
| 191 | options = options.replace(',', ' ').replace('=', ' ') |
| 192 | for s in options.split(): |
| 193 | if s.startswith('--'): |
| 194 | long = s |
| 195 | elif s.startswith('-'): |
| 196 | short = s |
| 197 | else: |
| 198 | argcount = 1 |
| 199 | if argcount: |
| 200 | matched = re.findall('\[default: (.*)\]', description, flags=re.I) |
| 201 | value = matched[0] if matched else None |
| 202 | return class_(short, long, argcount, value) |
| 203 | |
| 204 | def single_match(self, left): |
| 205 | for n, pattern in enumerate(left): |
| 206 | if self.name == pattern.name: |
| 207 | return n, pattern |
| 208 | return None, None |
| 209 | |
| 210 | @property |
| 211 | def name(self): |
| 212 | return self.long or self.short |
| 213 | |
| 214 | def __repr__(self): |
| 215 | return 'Option(%r, %r, %r, %r)' % (self.short, self.long, |
| 216 | self.argcount, self.value) |
| 217 | |
| 218 | |
| 219 | class Required(BranchPattern): |
no outgoing calls
no test coverage detected