File: cli.py

package info (click to toggle)
python-userpath 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: python: 771; makefile: 21; sh: 16
file content (174 lines) | stat: -rw-r--r-- 6,255 bytes parent folder | download | duplicates (3)
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
import sys

import click

import userpath as up
from userpath.shells import DEFAULT_SHELLS, SHELLS


CONTEXT_SETTINGS = {'help_option_names': ['-h', '--help']}


def echo_success(text, nl=True):
    click.secho(text, fg='cyan', bold=True, nl=nl)


def echo_failure(text, nl=True):
    click.secho(text, fg='red', bold=True, nl=nl, err=True)


def echo_warning(text, nl=True):
    click.secho(text, fg='yellow', bold=True, nl=nl)


@click.group(context_settings=CONTEXT_SETTINGS)
@click.version_option()
def userpath():
    pass


@userpath.command(context_settings=CONTEXT_SETTINGS, short_help='Prepends to the user PATH')
@click.argument('locations', required=True, nargs=-1)
@click.option(
    '-s',
    '--shell',
    'shells',
    multiple=True,
    type=click.Choice(sorted(SHELLS)),
    help=(
        'The shell in which PATH will be modified. This can be selected multiple times and has no '
        'effect on Windows. The default shells are: {}'.format(', '.join(sorted(DEFAULT_SHELLS)))
    ),
)
@click.option(
    '-a',
    '--all-shells',
    is_flag=True,
    help=(
        'Update PATH of all supported shells. This has no effect on Windows as environment settings are already global.'
    ),
)
@click.option('--home', help='Explicitly set the home directory.')
@click.option('-f', '--force', is_flag=True, help='Update PATH even if it appears to be correct.')
@click.option('-q', '--quiet', is_flag=True, help='Suppress output for successful invocations.')
def prepend(locations, shells, all_shells, home, force, quiet):
    """Prepends to the user PATH. The shell must be restarted for the update to
    take effect.
    """
    if not force:
        for location in locations:
            if up.in_current_path(location):
                echo_warning((
                    'The directory `{}` is already in PATH! If you '
                    'are sure you want to proceed, try again with '
                    'the -f/--force flag.'.format(location)
                ))
                sys.exit(2)
            elif up.in_new_path(location, shells=shells, all_shells=all_shells, home=home):
                echo_warning((
                    'The directory `{}` is already in PATH, pending a shell '
                    'restart! If you are sure you want to proceed, try again '
                    'with the -f/--force flag.'.format(location)
                ))
                sys.exit(2)

    try:
        up.prepend(locations, shells=shells, all_shells=all_shells, home=home, check=True)
    except Exception as e:
        echo_failure(str(e))
        sys.exit(1)
    else:
        if not quiet:
            echo_success('Success!')


@userpath.command(context_settings=CONTEXT_SETTINGS, short_help='Appends to the user PATH')
@click.argument('locations', required=True, nargs=-1)
@click.option(
    '-s',
    '--shell',
    'shells',
    multiple=True,
    type=click.Choice(sorted(SHELLS)),
    help=(
        'The shell in which PATH will be modified. This can be selected multiple times and has no '
        'effect on Windows. The default shells are: {}'.format(', '.join(sorted(DEFAULT_SHELLS)))
    ),
)
@click.option(
    '-a',
    '--all-shells',
    is_flag=True,
    help=(
        'Update PATH of all supported shells. This has no effect on Windows as environment settings are already global.'
    ),
)
@click.option('--home', help='Explicitly set the home directory.')
@click.option('-f', '--force', is_flag=True, help='Update PATH even if it appears to be correct.')
@click.option('-q', '--quiet', is_flag=True, help='Suppress output for successful invocations.')
def append(locations, shells, all_shells, home, force, quiet):
    """Appends to the user PATH. The shell must be restarted for the update to
    take effect.
    """
    if not force:
        for location in locations:
            if up.in_current_path(location):
                echo_warning((
                    'The directory `{}` is already in PATH! If you '
                    'are sure you want to proceed, try again with '
                    'the -f/--force flag.'.format(location)
                ))
                sys.exit(2)
            elif up.in_new_path(location, shells=shells, all_shells=all_shells, home=home):
                echo_warning((
                    'The directory `{}` is already in PATH, pending a shell '
                    'restart! If you are sure you want to proceed, try again '
                    'with the -f/--force flag.'.format(location)
                ))
                sys.exit(2)

    try:
        up.append(locations, shells=shells, all_shells=all_shells, home=home, check=True)
    except Exception as e:
        echo_failure(str(e))
        sys.exit(1)
    else:
        if not quiet:
            echo_success('Success!')


@userpath.command(context_settings=CONTEXT_SETTINGS, short_help='Checks if locations are in the user PATH')
@click.argument('locations', required=True, nargs=-1)
@click.option(
    '-s',
    '--shell',
    'shells',
    multiple=True,
    type=click.Choice(sorted(SHELLS)),
    help=(
        'The shell in which PATH will be modified. This can be selected multiple times and has no '
        'effect on Windows. The default shells are: {}'.format(', '.join(sorted(DEFAULT_SHELLS)))
    ),
)
@click.option(
    '-a',
    '--all-shells',
    is_flag=True,
    help=(
        'Update PATH of all supported shells. This has no effect on Windows as environment settings are already global.'
    ),
)
@click.option('--home', help='Explicitly set the home directory.')
@click.option('-q', '--quiet', is_flag=True, help='Suppress output for successful invocations.')
def verify(locations, shells, all_shells, home, quiet):
    """Checks if locations are in the user PATH."""
    for location in locations:
        if up.in_current_path(location):
            if not quiet:
                echo_success('The directory `{}` is in PATH!'.format(location))
        elif up.in_new_path(location, shells=shells, all_shells=all_shells, home=home):
            echo_warning('The directory `{}` is in PATH, pending a shell restart!'.format(location))
            sys.exit(2)
        else:
            echo_failure('The directory `{}` is not in PATH!'.format(location))
            sys.exit(1)