← Runnable Projects

阅读中文版本

Python Argparse CLI

Python Argparse — a runnable python project with source, install and run commands, and expected output.

intermediatepythonunverified

Project

py-argparse-cli

Entry point: cli.py

Install

python cli.py --name Alice --repeat 3

Run

python cli.py --name Alice --repeat 3

Test

pytest cli_test.py

Expected output

Uses argparse to accept --name and --repeat and prints a repeated greeting.

Dependencies

  • python >=3.10

Verification evidence

Source reviewed against python standard library APIs. Commands verified for syntax; execution not yet exercised in CI.

Security note

Reads only the file or arguments passed on the command line; makes no network requests and writes nothing outside stdout.

What it does

Python Argparse CLI is a self-contained python command-line program. Uses argparse to accept –name and –repeat and prints a repeated greeting.

Install and run

python cli.py --name Alice --repeat 3
python cli.py --name Alice --repeat 3

Project structure

py-argparse-cli/
├── cli.py
└── README snippet above

Key source

# cli.py
import argparse
p = argparse.ArgumentParser()
p.add_argument("--name", required=True)
p.add_argument("--repeat", type=int, default=1)
a = p.parse_args()
for _ in range(a.repeat):
    print(f"Hello, {a.name}!")

Testing

pytest cli_test.py

Expected output

When run with the documented command, the program prints:

Hello, Alice!\nHello, Alice!\nHello, Alice!

This expected output describes the verified behavior of the core logic; the surrounding harness (argument parsing, file I/O) is reviewed but not yet executed in CI, hence runnability is marked unverified.

Limitations

  • The program is a single-file CLI with no external service dependencies.
  • Argument validation is minimal; invalid input produces a runtime error rather than a friendly message.
  • Execution has been reviewed for correctness but not yet exercised in an automated CI run.