From 36cc72b29acba516bca2014642b6b540dc3a4458 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Mon, 13 Feb 2023 10:47:20 +0800 Subject: [PATCH 1/2] feat: add option --lib to init command to create a library project --- src/pdm/cli/commands/init.py | 11 ++++++----- src/pdm/cli/completions/pdm.bash | 2 +- src/pdm/cli/completions/pdm.fish | 1 + src/pdm/cli/completions/pdm.ps1 | 2 +- src/pdm/cli/completions/pdm.zsh | 1 + src/pdm/termui.py | 2 +- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/pdm/cli/commands/init.py b/src/pdm/cli/commands/init.py index 61606debc3..7a6efcf81f 100644 --- a/src/pdm/cli/commands/init.py +++ b/src/pdm/cli/commands/init.py @@ -40,6 +40,9 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None: parser.add_argument( "--backend", choices=list(_BACKENDS), help="Specify the build backend" ) + parser.add_argument( + "--lib", action="store_true", help="Create a library project" + ) parser.set_defaults(search_parent=False) def handle(self, project: Project, options: argparse.Namespace) -> None: @@ -102,15 +105,13 @@ def handle(self, project: Project, options: argparse.Namespace) -> None: "For more info, please visit https://peps.python.org/pep-0582/", style="success", ) - is_library = ( - termui.confirm( + is_library = options.lib + if not is_library and self.interactive: + is_library = termui.confirm( "Is the project a library that is installable?\n" "A few more questions will be asked to include a project name " "and build backend" ) - if self.interactive - else False - ) build_backend: type[BuildBackend] | None = None if is_library: name = self.ask("Project name", project.root.name) diff --git a/src/pdm/cli/completions/pdm.bash b/src/pdm/cli/completions/pdm.bash index de8b9c41ae..d478f2a054 100644 --- a/src/pdm/cli/completions/pdm.bash +++ b/src/pdm/cli/completions/pdm.bash @@ -61,7 +61,7 @@ _pdm_a919b69078acdf0a_complete() ;; (init) - opts="--backend --global --help --non-interactive --project --python --skip --verbose" + opts="--backend --global --help --lib --non-interactive --project --python --skip --verbose" ;; (install) diff --git a/src/pdm/cli/completions/pdm.fish b/src/pdm/cli/completions/pdm.fish index cebf746a51..d9aa4de1d9 100644 --- a/src/pdm/cli/completions/pdm.fish +++ b/src/pdm/cli/completions/pdm.fish @@ -136,6 +136,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from info' -l where -d 'Show the p complete -c pdm -A -n '__fish_seen_subcommand_from init' -l backend -d 'Specify the build backend' complete -c pdm -A -n '__fish_seen_subcommand_from init' -l global -d 'Use the global project, supply the project root with `-p` option' complete -c pdm -A -n '__fish_seen_subcommand_from init' -l help -d 'show this help message and exit' +complete -c pdm -A -n '__fish_seen_subcommand_from init' -l lib -d 'Create a library project' complete -c pdm -A -n '__fish_seen_subcommand_from init' -l non-interactive -d 'Don\'t ask questions but use default values' complete -c pdm -A -n '__fish_seen_subcommand_from init' -l project -d 'Specify another path as the project root, which changes the base of pyproject.toml and __pypackages__' complete -c pdm -A -n '__fish_seen_subcommand_from init' -l python -d 'Specify the Python version/path to use' diff --git a/src/pdm/cli/completions/pdm.ps1 b/src/pdm/cli/completions/pdm.ps1 index 6bdb351754..0921f236e2 100644 --- a/src/pdm/cli/completions/pdm.ps1 +++ b/src/pdm/cli/completions/pdm.ps1 @@ -272,7 +272,7 @@ function TabExpansion($line, $lastWord) { "init" { $completer.AddOpts( @( - [Option]::new(@("-g", "--global", "--non-interactive", "-n", "--python")), + [Option]::new(@("-g", "--global", "--non-interactive", "-n", "--python", "--lib")), $projectOption, $skipOption, [Option]::new(@("--backend")).WithValues(@("pdm-backend", "setuptools", "flit", "hatching", "pdm-pep517")) diff --git a/src/pdm/cli/completions/pdm.zsh b/src/pdm/cli/completions/pdm.zsh index cc73d6869e..e2ab8c56a5 100644 --- a/src/pdm/cli/completions/pdm.zsh +++ b/src/pdm/cli/completions/pdm.zsh @@ -173,6 +173,7 @@ _pdm() { {-n,--non-interactive}"[Don't ask questions but use default values]" {-k,--skip}'[Skip some tasks and/or hooks by their comma-separated names]' '--backend[Specify the build backend]:backend:(pdm-backend setuptools hatchling flit pdm-pep517)' + '--lib[Create a library project]' '--python[Specify the Python version/path to use]:python:' ) ;; diff --git a/src/pdm/termui.py b/src/pdm/termui.py index e3146411b2..5fc83d458d 100644 --- a/src/pdm/termui.py +++ b/src/pdm/termui.py @@ -61,7 +61,7 @@ def style(text: str, *args: str, style: str | None = None, **kwargs: Any) -> str return capture.get() -def confirm(*args: str, **kwargs: Any) -> str: +def confirm(*args: str, **kwargs: Any) -> bool: kwargs.setdefault("default", False) return Confirm.ask(*args, **kwargs) From da5e8fabc7f7980922b93c699fbd9242f325afd8 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Mon, 13 Feb 2023 10:56:18 +0800 Subject: [PATCH 2/2] add news --- news/1708.feature.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/1708.feature.md diff --git a/news/1708.feature.md b/news/1708.feature.md new file mode 100644 index 0000000000..b46a1fa10d --- /dev/null +++ b/news/1708.feature.md @@ -0,0 +1 @@ +Introduce `--lib` option to `init` command to create a library project without prompting.