-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScripting_Automation.py
More file actions
65 lines (61 loc) · 1.97 KB
/
Copy pathScripting_Automation.py
File metadata and controls
65 lines (61 loc) · 1.97 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
# Scripting_Automation.py
# Reference Guide: Subprocess execution and Command-line parsing with click and typer
import subprocess
# ==========================================
# 1. SUBPROCESS MODULE
# ==========================================
# subprocess runs operating system shell commands and collects outputs.
print("--- 1. SUBPROCESS SHELL RUN ---")
try:
# run command, check=True raises CalledProcessError if return code is non-zero
result = subprocess.run(
["echo", "Hello from Subprocess!"],
capture_output=True,
text=True,
check=True,
shell=True # needed for windows shell command matching
)
print(f"Stdout: {result.stdout.strip()}")
print(f"Status Code: {result.returncode}")
except subprocess.CalledProcessError as e:
print(f"Process failed with error: {e}")
except Exception as e:
print(f"Unexpected execution issue: {e}")
print()
# ==========================================
# 2. CLICK (Command line interface toolkit)
# ==========================================
# Click allows building command line applications using decorators.
# 1. Install: `pip install click`
#
# --- Click Script Example ---
# import click
#
# @click.command()
# @click.option('--count', default=1, help='Number of greetings.')
# @click.option('--name', prompt='Your name', help='The person to greet.')
# def hello(count, name):
# for x in range(count):
# click.echo(f"Hello {name}!")
#
# if __name__ == '__main__':
# hello()
# ==========================================
# 3. TYPER (Modern CLI builder)
# ==========================================
# Typer uses type hints to build CLIs (built by the creator of FastAPI).
# 1. Install: `pip install typer`
#
# --- Typer Script Example ---
# import typer
#
# app = typer.Typer()
#
# @app.command()
# def main(name: str, age: int = 18):
# print(f"Hello {name}, you are {age} years old.")
#
# if __name__ == "__main__":
# app()
print("Scripting_Automation reference configured!")
print()