| 264 | |
| 265 | # one argument to a given command |
| 266 | class Arg(object): |
| 267 | def __init__(self, arg, desc=""): |
| 268 | self.name = arg |
| 269 | self.desc = desc |
| 270 | |
| 271 | # finish initialization (no more calls to add_desc after this) |
| 272 | def finish_init(self): |
| 273 | match = re.match("(-[^ ]*)( (-[^ ]*))?( (.+?))?( \(default: (.*)\))?$", self.name) |
| 274 | if match == None: |
| 275 | print "Error parsing argument: " + self.name |
| 276 | sys.exit(1) |
| 277 | self.params = [match.group(1)] |
| 278 | if match.group(3) != None: |
| 279 | self.params.append(match.group(3)) |
| 280 | self.usage = match.group(5) |
| 281 | self.default = match.group(7) |
| 282 | |
| 283 | filters = [ |
| 284 | "Required argument", |
| 285 | "Default: use --debug_args to see this default" |
| 286 | ] |
| 287 | match = re.search("Default: (.+)$", self.desc) |
| 288 | if match != None: |
| 289 | filters.append(match.group(0)) |
| 290 | self.default = match.group(1) |
| 291 | for f in filters: |
| 292 | if f in self.desc: |
| 293 | self.desc = self.desc.replace(f, "").strip() |
| 294 | |
| 295 | # extend the description as necessary |
| 296 | def add_desc(self, desc): |
| 297 | self.desc += " " + desc.strip() |
| 298 | self.desc = self.desc.strip() |
| 299 | |
| 300 | # get the list of parameter names (one or two) |
| 301 | def get_parameters(self): |
| 302 | return self.params |
| 303 | |
| 304 | # return the usage part of the parameter |
| 305 | def get_usage(self): |
| 306 | return self.usage |
| 307 | |
| 308 | def has_default(self): |
| 309 | return self.default != None |
| 310 | |
| 311 | def get_default(self): |
| 312 | return self.default |
| 313 | |
| 314 | # simple string representation |
| 315 | def __repr__(self): |
| 316 | return self.name |
| 317 | |
| 318 | # a stoke command (which has either a list of subcommands, or a list of arguments) |
| 319 | class Command(object): |