~dricottone/filters

ref: f62c909a49e4a579a0b74b9b6632c25e127b3668 filters/rng/cli.py -rw-r--r-- 6.3 KiB
f62c909aDominic Ricottone Version 1.0.1; Cleaned up documentation 4 years ago
                                                                                
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python3

import re

def main(arguments):
	config=dict()
	positional=[]
	pattern=re.compile(r"(?:-(?:d|h|x|i|m|n|o|r|s|v|V)|--(?:delta|distribution|help|initial|list-distributions|mu|number|offset|report|sigma|version))(?:=.*)?$")
	consuming,needing,wanting=None,0,0
	attached_value=None
	while len(arguments) and arguments[0]!="--":
		if consuming is not None:
			if config[consuming] is None:
				config[consuming]=arguments.pop(0)
			else:
				config[consuming].append(arguments.pop(0))
			needing-=1
			wanting-=1
			if wanting==0:
				consuming,needing,wanting=None,0,0
		elif pattern.match(arguments[0]):
			option = arguments.pop(0).lstrip('-')
			if '=' in option:
				option,attached_value=option.split('=',1)
			if option=="delta":
				if attached_value is not None:
					config["delta"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["delta"]=None
					consuming,needing,wanting="delta",1,1
			elif option=="distribution":
				if attached_value is not None:
					config["distribution"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["distribution"]=None
					consuming,needing,wanting="distribution",1,1
			elif option=="help":
				if attached_value is not None:
					message=(
						'unexpected value while parsing "help"'
						' (expected 0 values)'
					)
					raise ValueError(message) from None
				config["help"]=True
			elif option=="initial":
				if attached_value is not None:
					config["initial"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["initial"]=None
					consuming,needing,wanting="initial",1,1
			elif option=="list-distributions":
				if attached_value is not None:
					message=(
						'unexpected value while parsing "list-distributions"'
						' (expected 0 values)'
					)
					raise ValueError(message) from None
				config["list-distributions"]=True
			elif option=="mu":
				if attached_value is not None:
					config["mu"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["mu"]=None
					consuming,needing,wanting="mu",1,1
			elif option=="number":
				if attached_value is not None:
					config["number"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["number"]=None
					consuming,needing,wanting="number",1,1
			elif option=="offset":
				if attached_value is not None:
					config["offset"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["offset"]=None
					consuming,needing,wanting="offset",1,1
			elif option=="report":
				if attached_value is not None:
					message=(
						'unexpected value while parsing "report"'
						' (expected 0 values)'
					)
					raise ValueError(message) from None
				config["report"]=True
			elif option=="sigma":
				if attached_value is not None:
					config["sigma"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["sigma"]=None
					consuming,needing,wanting="sigma",1,1
			elif option=="version":
				if attached_value is not None:
					message=(
						'unexpected value while parsing "version"'
						' (expected 0 values)'
					)
					raise ValueError(message) from None
				config["version"]=True
			elif option=="d":
				if attached_value is not None:
					config["delta"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["delta"]=None
					consuming,needing,wanting="delta",1,1
			elif option=="h":
				if attached_value is not None:
					message=(
						'unexpected value while parsing "help"'
						' (expected 0 values)'
					)
					raise ValueError(message) from None
				config["help"]=True
			elif option=="x":
				if attached_value is not None:
					message=(
						'unexpected value while parsing "help"'
						' (expected 0 values)'
					)
					raise ValueError(message) from None
				config["help"]=True
			elif option=="i":
				if attached_value is not None:
					config["initial"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["initial"]=None
					consuming,needing,wanting="initial",1,1
			elif option=="m":
				if attached_value is not None:
					config["mu"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["mu"]=None
					consuming,needing,wanting="mu",1,1
			elif option=="n":
				if attached_value is not None:
					config["number"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["number"]=None
					consuming,needing,wanting="number",1,1
			elif option=="o":
				if attached_value is not None:
					config["offset"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["offset"]=None
					consuming,needing,wanting="offset",1,1
			elif option=="r":
				if attached_value is not None:
					message=(
						'unexpected value while parsing "report"'
						' (expected 0 values)'
					)
					raise ValueError(message) from None
				config["report"]=True
			elif option=="s":
				if attached_value is not None:
					config["sigma"]=attached_value
					attached_value=None
					consuming,needing,wanting=None,0,0
				else:
					config["sigma"]=None
					consuming,needing,wanting="sigma",1,1
			elif option=="v":
				if attached_value is not None:
					message=(
						'unexpected value while parsing "version"'
						' (expected 0 values)'
					)
					raise ValueError(message) from None
				config["version"]=True
			elif option=="V":
				if attached_value is not None:
					message=(
						'unexpected value while parsing "version"'
						' (expected 0 values)'
					)
					raise ValueError(message) from None
				config["version"]=True
		else:
			positional.append(arguments.pop(0))
	if needing>0:
		message=(
			f'unexpected end while parsing "{consuming}"'
			f' (expected {needing} values)'
		)
		raise ValueError(message) from None
	for argument in arguments[1:]:
		positional.append(argument)
	return config,positional

if __name__=="__main__":
	import sys
	cfg,pos = main(sys.argv[1:])
	cfg = {k:v for k,v in cfg.items() if v is not None}
	if len(cfg):
		print("Options:")
		for k,v in cfg.items():
			print(f"{k:20} = {v}")
	if len(pos):
		print("Positional arguments:", ", ".join(pos))