File: cmdline.py

package info (click to toggle)
frog 0.12.17-7.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,140 kB
  • ctags: 575
  • sloc: sh: 11,167; cpp: 5,146; python: 765; makefile: 38
file content (110 lines) | stat: -rw-r--r-- 2,468 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
####
#
# Pylet - Python Language Engineering Toolkit
# Written by Sander Canisius <S.V.M.Canisius@uvt.nl>
#

import optparse
import warnings

from itertools import imap


def parseOption(lines, options, optionRefinements):
	line = lines.pop(0)
	switches, helpMessage = map(str.strip, line.split(":", 1))

	metavar = None
	opts = []
	for switch in imap(str.strip, switches.split(",")):
		if switch.startswith("--"):
			if "=" in switch:
				switch, metavar = switch.split("=")
			opts.append(switch)
			
		elif switch.startswith("-"):
			if len(switch) > 2:
				metavar = switch[2:].strip()
				switch = switch[:2]
			opts.append(switch)

		else:
			warnings.warn("Ignoring invalid option specification " + switch,
						  SyntaxWarning, stacklevel=3)

	stop = False
	while lines and not stop:
		nextLine = lines[0]
		text = nextLine.strip()
		if text and nextLine[0].isspace():
			helpMessage += " " + text
			lines.pop(0)
		else:
			stop = True
	
	kwargs = {
		"help": helpMessage
	}

	if metavar:
		kwargs["metavar"] = metavar
	else:
		kwargs["action"] = "store_true"

	if opts[0] in optionRefinements:
		kwargs.update(optionRefinements[opts[0]])

	options.add_option(*opts, **kwargs)


def parseOptionGroup(lines, parser):
	line = lines.pop(0)
	desc = line[:-1].strip()
	return parser.add_option_group(desc)


def parseRefinements(spec):
	lines = spec.splitlines()[2:]   # remove ---\n:Refinements:
	
	refinements = {}
	for line in lines:
		option, attributes = imap(str.strip, line.split(":", 1))
		refinements[option] = eval("dict(%s)" % attributes)

	return refinements


def parse(optionSpec=None, args=None):
	import __main__
	if not optionSpec:
		optionSpec = __main__.__doc__
	
	start = optionSpec.index("usage:")
	try:
		end = optionSpec.index("---\n:Refinements:")
		refinements = parseRefinements(optionSpec[end:]) # NOTE: this should be in its own try block!?
	except:
		end = len(optionSpec)
		refinements = {}

	description = optionSpec[:start].strip()

	lines = optionSpec[start:end].splitlines()
	
	usage = lines.pop(0)[6:].strip()
	parser = optparse.OptionParser(usage=usage,
								   description=description,
								   version=getattr(__main__,
												   "__version__", None))
	options = parser

	while lines:
		nextLine = lines[0]
		if nextLine.startswith("-"):
			parseOption(lines, options, refinements)
		elif nextLine.endswith(":"):
			options = parseOptionGroup(lines, parser)
		else:
			lines.pop(0)

	return parser.parse_args(args)