Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add cli option to specify working path #6810

Merged
merged 4 commits into from Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/cli.md
Expand Up @@ -29,6 +29,7 @@ then `--help` combined with any of those can give you more information.
* `--no-interaction (-n)`: Do not ask any interactive question.
* `--no-plugins`: Disables plugins.
* `--no-cache`: Disables Poetry source caches.
* `--directory=DIRECTORY (-C)`: The working directory for the Poetry command (defaults to the current working directory).


## new
Expand Down
19 changes: 18 additions & 1 deletion src/poetry/console/application.py
Expand Up @@ -121,8 +121,13 @@ def poetry(self) -> Poetry:
if self._poetry is not None:
return self._poetry

project_path = Path.cwd()

if self._io and self._io.input.option("directory"):
project_path = self._io.input.option("directory")

self._poetry = Factory().create_poetry(
Path.cwd(),
cwd=project_path,
io=self._io,
disable_plugins=self._disable_plugins,
disable_cache=self._disable_cache,
Expand Down Expand Up @@ -367,6 +372,18 @@ def _default_definition(self) -> Definition:
)
)

definition.add_option(
Option(
"--directory",
"-C",
flag=False,
description=(
"The working directory for the Poetry command (defaults to the"
" current working directory)."
),
)
)

return definition

def _get_solution_provider_repository(self) -> SolutionProviderRepository:
Expand Down
12 changes: 11 additions & 1 deletion src/poetry/console/commands/init.py
Expand Up @@ -77,7 +77,17 @@ def handle(self) -> int:
from poetry.layouts import layout
from poetry.utils.env import SystemEnv

pyproject = PyProjectTOML(Path.cwd() / "pyproject.toml")
project_path = Path.cwd()

if self.io.input.option("directory"):
project_path = Path(self.io.input.option("directory"))
if not project_path.exists() or not project_path.is_dir():
self.line_error(
"<error>The --directory path is not a directory.</error>"
)
return 1

pyproject = PyProjectTOML(project_path / "pyproject.toml")

if pyproject.file.exists():
if pyproject.is_poetry_project():
Expand Down
6 changes: 6 additions & 0 deletions src/poetry/console/commands/new.py
Expand Up @@ -34,6 +34,12 @@ def handle(self) -> int:
from poetry.layouts import layout
from poetry.utils.env import SystemEnv

if self.io.input.option("directory"):
self.line_error(
"<warning>--directory only makes sense with existing projects, and will"
" be ignored. You should consider the option --path instead.</warning>"
)

if self.option("src"):
layout_cls = layout("src")
else:
Expand Down