-
Notifications
You must be signed in to change notification settings - Fork 0
/
arg_parsing.py
54 lines (42 loc) · 1.55 KB
/
arg_parsing.py
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
""" Functions related to CLI argument parsing """
def parse_args(args, arg_parsers):
for k, v in args.items():
if k in arg_parsers:
parsed_val = arg_parsers[k](k, v)
if parsed_val is None:
exit(1)
args[k] = parsed_val
def int_parser(minv=None, maxv=None):
return lambda arg, val: num_parser_impl(arg, val, minv=minv, maxv=maxv, cast=int)
def float_parser(minv=None, maxv=None):
return lambda arg, val: num_parser_impl(arg, val, minv=minv, maxv=maxv, cast=float)
def num_parser_impl(arg, val, minv=None, maxv=None, cast=None):
cast_str = {int: "an integer", float: "a number"}
try:
cast_val = cast(val)
except:
print(f"{arg} must be {cast_str[cast]}.")
return None
if minv is not None:
if cast_val < minv:
print(f"{arg} must be >= {minv}")
return None
if maxv is not None:
if cast_val > maxv:
print(f"{arg} must be <= {maxv}")
return None
return cast_val
def note_parser():
return lambda arg, val: note_parser_impl(arg, val)
def note_parser_impl(arg, val):
letter, *rest = val
if rest[0] in ["#", "b"]:
letter += rest[0]
octave = int(''.join(rest[1:]))
else:
octave = int(''.join(rest))
pitches = {"C": 0, "C#": 1, "Db": 1, "D": 2, "D#": 3, "Eb": 3, "E": 4, "F": 5, "F#": 6, "Gb": 6, "G": 7, "G#": 8,
"Ab": 8, "A": 9, "A#": 10, "Bb": 10, "B": 11}
pitch = pitches[letter]
midi_note = (octave + 1) * 12 + pitch
return midi_note