-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsing_Click_Example_2.py
More file actions
executable file
·66 lines (50 loc) · 2.23 KB
/
Using_Click_Example_2.py
File metadata and controls
executable file
·66 lines (50 loc) · 2.23 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
#!/usr/bin/env python
#Command line tool using tool
#cli: Top Level Group: Act as main entry point for cli
#ships: Subgroups: Contain commands related to ships
#Command under 'ships':
# 1. 'sail': Print a message indicating a ship is setting sail
# 2. 'list_ships': Print a list of predefined ship names
#'sailors': Talks to a sailor with customizable greeting
import click
@click.group()
def cli():
pass
@click.group(help='Ship related commands')
def ships():
pass
cli.add_command(ships)
@ships.command(help='Sail a ship')
def sail():
ship_name = 'Your ship'
print(f"{ship_name} is getting sail")
@ships.command(help='List all the ships')
def list_ships():
ships = ['John B', 'Yankee Clipper', 'Pequod']
print(f"Ships: {', '.join(ships)}")
@cli.command(help='Talk to a sailor')
@click.option('--greeting',default='Ahoy there', help='Greeting for sailor')
@click.argument('name')
def sailors(greeting, name):
message = f'{greeting} {name}'
print(message)
if __name__ == '__main__':
cli()
"""
.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ chmod +x Using_Click_Example_2.py
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ ls -la Using_Click_Example_2.py
-rwxrwxr-x 1 gauravmtwt1 gauravmtwt1 723 Jul 29 06:59 Using_Click_Example_2.py
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ ./Using_Click_Example_2.py --help
Usage: Using_Click_Example_2.py [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
sailors Talk to a sailor
ships Ship related commands
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ ./Using_Click_Example_2.py ships list-ships
Ships: John B, Yankee Clipper, Pequod
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ ./Using_Click_Example_2.py ships sail
Your ship is getting sail
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ ./Using_Click_Example_2.py sailors --greeting "Hello" ROger
Hello ROger
"""