Repository navigation
Expand file tree
/
Copy path02_using_cli_tools.py
More file actions
107 lines (78 loc) · 3.1 KB
/
Copy path02_using_cli_tools.py
File metadata and controls
107 lines (78 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
02_using_cli_tools.py
======================
Comparing Python CLI Tools for Scientific Scripts
-------------------------------------------------
Goal:
-----
- Use CLI tools to parse scientific parameters and solve equations
- Show usage of: argparse, Click, and Typer
How to run:
-----------
(1) Argparse Example – Force
$ python 02_using_cli_tools.py argparse --mass 2.5 --acceleration 9.8
(2) Click Example – Ohm's Law
$ python 02_using_cli_tools.py click --current 2.0 --resistance 5.5
(3) Typer Example – Kinetic Energy
$ python 02_using_cli_tools.py typer solve --mass 70 --velocity 3.2
Notes:
------
- Each subcommand demonstrates a different CLI tool
- Designed for scientific tasks using common equations
"""
import sys
# =======================================================
# 1. ARGPARSE EXAMPLE – F = m * a
# =======================================================
def run_argparse():
import argparse
parser = argparse.ArgumentParser(description="Calculate force using F = m * a")
parser.add_argument("--mass", type=float, required=True, help="Mass in kg")
parser.add_argument("--acceleration", type=float, required=True, help="Acceleration in m/s^2")
args = parser.parse_args(sys.argv[2:]) # Skip first 2 CLI args: script name and 'argparse'
force = args.mass * args.acceleration
print(f"[Argparse] Force = {force:.2f} N")
# =======================================================
# 2. CLICK EXAMPLE – Ohm's Law: V = I * R
# =======================================================
# ...
def run_click():
import click
@click.command()
@click.option("--current", type=float, required=True, help="Current in Amperes (I)")
@click.option("--resistance", type=float, required=True, help="Resistance in Ohms (R)")
def solve(current, resistance):
voltage = current * resistance
print(f"[Click] Voltage = {voltage:.2f} V")
solve(args=sys.argv[2:], standalone_mode=False)
# =======================================================
# 3. TYPER EXAMPLE – Kinetic Energy: KE = 0.5 * m * v^2
# =======================================================
import sys
import typer
def run_typer():
app = typer.Typer()
@app.command()
def solve(
mass: float = typer.Option(..., "--mass", "-m", help="Mass in kg"),
velocity: float = typer.Option(..., "--velocity", "-v", help="Velocity in m/s"),
):
ke = 0.5 * mass * velocity ** 2
typer.echo(f"[Typer] Kinetic Energy = {ke:.2f} J")
app(prog_name="typer", args=sys.argv[3:])
# =======================================================
# MAIN ENTRY – Use CLI Tool Based on First Argument
# =======================================================
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage:\n python 02_using_cli_tools.py [argparse|click|typer] ...")
sys.exit(1)
mode = sys.argv[1].lower()
if mode == "argparse":
run_argparse()
elif mode == "click":
run_click()
elif mode == "typer":
run_typer()
else:
print(f"Unknown mode '{mode}'. Use 'argparse', 'click', or 'typer'.")