Skip to content

Commit ecbab56

Browse files
authored
Add --all-shells flag to ensure_path (#1591)
1 parent 86e0701 commit ecbab56

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

changelog.d/1585.feature.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `--all-shells` flag to `pipx ensurepath`.

src/pipx/commands/ensure_path.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_pipx_user_bin_path() -> Optional[Path]:
5151
return pipx_bin_path
5252

5353

54-
def ensure_path(location: Path, *, force: bool, prepend: bool = False) -> Tuple[bool, bool]:
54+
def ensure_path(location: Path, *, force: bool, prepend: bool = False, all_shells: bool = False) -> Tuple[bool, bool]:
5555
"""Ensure location is in user's PATH or add it to PATH.
5656
If prepend is True, location will be prepended to PATH, else appended.
5757
Returns True if location was added to PATH
@@ -63,9 +63,9 @@ def ensure_path(location: Path, *, force: bool, prepend: bool = False) -> Tuple[
6363

6464
if force or (not in_current_path and not need_shell_restart):
6565
if prepend:
66-
path_added = userpath.prepend(location_str, "pipx")
66+
path_added = userpath.prepend(location_str, "pipx", all_shells=all_shells)
6767
else:
68-
path_added = userpath.append(location_str, "pipx")
68+
path_added = userpath.append(location_str, "pipx", all_shells=all_shells)
6969
if not path_added:
7070
print(
7171
pipx_wrap(
@@ -100,7 +100,7 @@ def ensure_path(location: Path, *, force: bool, prepend: bool = False) -> Tuple[
100100
return (path_added, need_shell_restart)
101101

102102

103-
def ensure_pipx_paths(force: bool, prepend: bool = False) -> ExitCode:
103+
def ensure_pipx_paths(force: bool, prepend: bool = False, all_shells: bool = False) -> ExitCode:
104104
"""Returns pipx exit code."""
105105
bin_paths = {paths.ctx.bin_dir}
106106

@@ -113,7 +113,9 @@ def ensure_pipx_paths(force: bool, prepend: bool = False) -> ExitCode:
113113
path_action_str = "prepended to" if prepend else "appended to"
114114

115115
for bin_path in bin_paths:
116-
(path_added_current, need_shell_restart_current) = ensure_path(bin_path, prepend=prepend, force=force)
116+
(path_added_current, need_shell_restart_current) = ensure_path(
117+
bin_path, prepend=prepend, force=force, all_shells=all_shells
118+
)
117119
path_added |= path_added_current
118120
need_shell_restart |= need_shell_restart_current
119121

src/pipx/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar
414414
return commands.run_pip(package, venv_dir, args.pipargs, args.verbose)
415415
elif args.command == "ensurepath":
416416
try:
417-
return commands.ensure_pipx_paths(prepend=args.prepend, force=args.force)
417+
return commands.ensure_pipx_paths(prepend=args.prepend, force=args.force, all_shells=args.all_shells)
418418
except Exception as e:
419419
logger.debug("Uncaught Exception:", exc_info=True)
420420
raise PipxError(str(e), wrap_message=False) from None
@@ -906,6 +906,11 @@ def _add_ensurepath(subparsers: argparse._SubParsersAction, shared_parser: argpa
906906
"PATH already contains paths to pipx and pipx-install apps."
907907
),
908908
)
909+
p.add_argument(
910+
"--all-shells",
911+
action="store_true",
912+
help=("Add directories to PATH in all shells instead of just the current one."),
913+
)
909914

910915

911916
def _add_environment(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:

0 commit comments

Comments
 (0)