Skip to content

Cli

python_project_foundry.cli

Command-line interface for Python Project Foundry.

main

main(argv: Sequence[str] | None = None) -> int

Run the Python Project Foundry command-line interface.

Source code in src/python_project_foundry/cli.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
def main(argv: Sequence[str] | None = None) -> int:
    """Run the Python Project Foundry command-line interface."""
    arguments = list(sys.argv[1:] if argv is None else argv)
    if arguments and arguments[0] not in {"template", "update", "publish", "-h", "--help", "--version"}:
        arguments.insert(0, "template")

    args = _build_parser().parse_args(arguments)

    if args.command == "template":
        tracking_data = _template_tracking_data()
        with _template_source() as template_source:
            # Copier exports run_copy dynamically via __getattr__; BasedPyright cannot resolve it.
            copier.run_copy(  # pyright: ignore[reportAttributeAccessIssue]
                src_path=str(template_source),
                dst_path=args.destination,
                data=tracking_data,
                defaults=args.defaults,
                overwrite=args.overwrite,
                pretend=args.pretend,
                skip_tasks=args.skip_tasks,
                unsafe=True,  # Run tasks
            )

    if args.command == "update":
        tracking_data = _template_tracking_data()
        try:
            # Copier exports run_update dynamically via __getattr__; BasedPyright cannot resolve it.
            copier.run_update(  # pyright: ignore[reportAttributeAccessIssue]
                dst_path=args.project,
                data=tracking_data,
                answers_file=ANSWERS_FILE,
                vcs_ref=f"v{tracking_data['_ppf_template_version']}",
                defaults=args.defaults,
                overwrite=True,
                pretend=args.pretend,
                conflict=args.conflict,
                unsafe=True,  # Run trusted template migrations and tasks
                skip_answered=True,
                skip_tasks=args.skip_tasks,
            )
        except CopierError as error:
            print(f"error: {error}", file=sys.stderr)
            return 2

    if args.command == "publish":
        try:
            publish_project(
                args.project,
                visibility=args.visibility,
                remote=args.remote,
                configure_pages=not args.skip_pages,
                dry_run=args.dry_run,
            )
        except PublishError as error:
            print(f"error: {error}", file=sys.stderr)
            return 2

    return 0