diff --git a/docs/cli.md b/docs/cli.md index dee328483da..9a71e0e1b92 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -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 diff --git a/src/poetry/console/application.py b/src/poetry/console/application.py index e54c679be9c..533233cadf3 100644 --- a/src/poetry/console/application.py +++ b/src/poetry/console/application.py @@ -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, @@ -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: diff --git a/src/poetry/console/commands/init.py b/src/poetry/console/commands/init.py index af259f5c059..e34fa2a7d36 100644 --- a/src/poetry/console/commands/init.py +++ b/src/poetry/console/commands/init.py @@ -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( + "The --directory path is not a directory." + ) + return 1 + + pyproject = PyProjectTOML(project_path / "pyproject.toml") if pyproject.file.exists(): if pyproject.is_poetry_project(): diff --git a/src/poetry/console/commands/new.py b/src/poetry/console/commands/new.py index cde571aa202..968a3a2a90b 100644 --- a/src/poetry/console/commands/new.py +++ b/src/poetry/console/commands/new.py @@ -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( + "--directory only makes sense with existing projects, and will" + " be ignored. You should consider the option --path instead." + ) + if self.option("src"): layout_cls = layout("src") else: