-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_tool.py
50 lines (43 loc) · 1.24 KB
/
cli_tool.py
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
import click
@click.group()
def cli():
pass
@cli.command()
@click.argument('name')
@click.option('--greeting', default='Hello', help='Greeting to use')
def greet(name, greeting):
click.echo(f"{greeting}, {name}!")
@cli.command()
@click.argument('a', type=int)
@click.argument('b', type=int)
def add(a, b):
result = a + b
click.echo(f"The sum of {a} and {b} is {result}")
@cli.command()
@click.argument('a', type=int)
@click.argument('b', type=int)
def multiply(a, b):
result = a * b
click.echo(f"The product of {a} and {b} is {result}")
@cli.command()
@click.argument('filename')
def read_file(filename):
try:
with open(filename, 'r') as file:
contents = file.read()
click.echo(contents)
except FileNotFoundError:
click.echo(f"Error: File '{filename}' not found or cannot be opened.")
except Exception as e:
click.echo(f"Error: An unexpected error occurred: {str(e)}")
@cli.command()
@click.option('--uppercase', '-u', is_flag=True, help='Convert text to uppercase.')
@click.argument('text')
def manipulate_text(text, uppercase):
if uppercase:
result = text.upper()
else:
result = text.lower()
click.echo(result)
if __name__ == '__main__':
cli()