Skip to content

Commit 4cc08ea

Browse files
committed
Add 'remove' command as an alias to 'uninstall'
1 parent 6a7bf94 commit 4cc08ea

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

news/8130.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add aliases 'add' and 'remove' aliases for 'install' and 'uninstall',
2+
respectively.

src/pip/_internal/cli/main_parser.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
ConfigOptionParser,
1010
UpdatingDefaultsHelpFormatter,
1111
)
12-
from pip._internal.commands import commands_dict, get_similar_commands
12+
from pip._internal.commands import (
13+
aliases_dict,
14+
aliases_of_commands,
15+
commands_dict,
16+
get_similar_commands,
17+
)
1318
from pip._internal.exceptions import CommandError
1419
from pip._internal.utils.misc import get_pip_version, get_prog
1520
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
@@ -47,10 +52,10 @@ def create_main_parser():
4752
parser.main = True # type: ignore
4853

4954
# create command listing for description
50-
description = [''] + [
51-
'{name:27} {command_info.summary}'.format(**locals())
52-
for name, command_info in commands_dict.items()
53-
]
55+
description = ['']
56+
for name, info in commands_dict.items():
57+
names = ', '.join(aliases_of_commands[name])
58+
description.append('{:27} {.summary}'.format(names, info))
5459
parser.description = '\n'.join(description)
5560

5661
return parser
@@ -81,9 +86,11 @@ def parse_command(args):
8186
sys.exit()
8287

8388
# the subcommand name
84-
cmd_name = args_else[0]
89+
cmd_alias = cmd_name = args_else[0]
8590

86-
if cmd_name not in commands_dict:
91+
if cmd_alias in aliases_dict:
92+
cmd_name = aliases_dict[cmd_alias]
93+
elif cmd_name not in commands_dict:
8794
guess = get_similar_commands(cmd_name)
8895

8996
msg = ['unknown command "{}"'.format(cmd_name)]
@@ -94,6 +101,6 @@ def parse_command(args):
94101

95102
# all the args without the subcommand
96103
cmd_args = args[:]
97-
cmd_args.remove(cmd_name)
104+
cmd_args.remove(cmd_alias)
98105

99-
return cmd_name, cmd_args
106+
return cmd_alias, cmd_args

src/pip/_internal/commands/__init__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
1818

1919
if MYPY_CHECK_RUNNING:
20-
from typing import Any
20+
from typing import Any, Dict, List
2121
from pip._internal.cli.base_command import Command
2222

2323

@@ -94,13 +94,26 @@
9494
)),
9595
]) # type: OrderedDict[str, CommandInfo]
9696

97+
aliases_dict = {
98+
'add': 'install',
99+
'remove': 'uninstall',
100+
} # type: Dict[str, str]
101+
102+
aliases_of_commands = {
103+
name: [name] for name in commands_dict} # type: Dict[str, List[str]]
104+
for alias, name in aliases_dict.items():
105+
aliases_of_commands[name].append(alias)
106+
97107

98108
def create_command(name, **kwargs):
99109
# type: (str, **Any) -> Command
100110
"""
101111
Create an instance of the Command class with the given name.
102112
"""
103-
module_path, class_name, summary = commands_dict[name]
113+
try:
114+
module_path, class_name, summary = commands_dict[name]
115+
except KeyError:
116+
module_path, class_name, summary = commands_dict[aliases_dict[name]]
104117
module = importlib.import_module(module_path)
105118
command_class = getattr(module, class_name)
106119
command = command_class(name=name, summary=summary, **kwargs)
@@ -114,7 +127,8 @@ def get_similar_commands(name):
114127

115128
name = name.lower()
116129

117-
close_commands = get_close_matches(name, commands_dict.keys())
130+
commands = {c for a in aliases_of_commands.values() for c in a}
131+
close_commands = get_close_matches(name, commands)
118132

119133
if close_commands:
120134
return close_commands[0]

0 commit comments

Comments
 (0)