aboutsummaryrefslogtreecommitdiff
path: root/cli/new/reader.py
blob: eacd70b088945c3f9389b015ad9412458caff8a5 (plain)
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
def required(label):
    value = input(f'{label}: ').strip()
    if value:
        print()
        return value
    else:
        return required(label)

def multi_line(label):
    lines = ''
    print(f'{label}, type [end] to finish:\n')
    while True:
        value = input()
        if value.strip() == '[end]':
            break
        elif value.strip():
            lines += f'{value.strip()}\n'
    print()
    return lines.strip()

def optional(label):
    value = input(f'{label} (optional): ').strip()
    print()
    return value

def non_empty_list(label):
    value = input(f'{label} (separated by commas): ')
    values = [x.strip() for x in value.split(',') if x.strip()]
    if len(values) > 0:
        print()
        return values
    else:
        return non_empty_list(label)

def integer(label):
    value = input(f'{label}: ').strip()
    if value.isdigit():
        print()
        return int(value)
    else:
        return integer(label)

def choices(label, xs):
    pp_choices = '/'.join(xs)
    value = input(f'{label} [{pp_choices}] ')
    if value in xs:
        print()
        return value
    else:
        return choices(label, xs)

def confirm(message):
    if choices(message, ['y', 'n']) == 'n':
        print('\nStopping.')
        exit(1)