-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsing_Fire.py
More file actions
executable file
·153 lines (108 loc) · 3.17 KB
/
Using_Fire.py
File metadata and controls
executable file
·153 lines (108 loc) · 3.17 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
#Using Fire Package
# fire package provides a way to create CLI with minimal code.
# It uses introspection to generate the interface automatically based on the functions
# and arguments you define.
#Simple Fire example
import fire
def greet(greeting='Hiya', name='Tammy'):
print(f"{greeting} {name}")
def goodbye(goodbye='Bye', name='Tammy'):
print(f"{goodbye} {name}")
if __name__ == '__main__':
fire.Fire()
"""
O/P: NAME
Using_Fire.py
SYNOPSIS
Using_Fire.py GROUP | COMMAND
GROUPS
GROUP is one of the following:
fire
The Python Fire module.
COMMANDS
COMMAND is one of the following:
greet
goodbye
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ ./Using_Fire.py greet --greeting "Hello" --name "Gaurav"
Hello Gaurav
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ ./Using_Fire.py goodbye --goodbye "Farewell" --name "Gaurav"
Farewell Gaurav
"""
#Nested CLI using Fire Package
#!/usr/bin/env python
# Command-line tool using fire
import fire
class Ships:
def sail(self):
ship_name = 'Your ship'
print(f"{ship_name} is setting sail")
def list(self):
ships = ['John B', 'Yankee Clipper', 'Pequod']
print(f"Ships: {', '.join(ships)}")
def sailors(greeting, name):
message = f'{greeting} {name}'
print(message)
class Cli:
def __init__(self):
self.sailors = sailors
self.ships = Ships()
if __name__ == '__main__':
fire.Fire(Cli)
"""
O/P: NAME
Using_Fire.py
SYNOPSIS
Using_Fire.py GROUP | COMMAND
GROUPS
GROUP is one of the following:
ships
COMMANDS
COMMAND is one of the following:
sailors
"""
"""
NAME
Using_Fire.py ships
SYNOPSIS
Using_Fire.py ships COMMAND
COMMANDS
COMMAND is one of the following:
list
sail
"""
"""
NAME
Using_Fire.py sailors
SYNOPSIS
Using_Fire.py sailors GREETING NAME
POSITIONAL ARGUMENTS
GREETING
NAME
NOTES
You can also use flags syntax for POSITIONAL ARGUMENTS
"""
"""
1. Set Sail
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ ./Using_Fire.py ships sail
Your ship is setting sail
2. List Ships
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ ./Using_Fire.py ships list
Ships: John B, Yankee Clipper, Pequod
3. Greet Sailor
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ ./Using_Fire.py sailors --greeting Hello --name Gaurav
Hello Gaurav
"""
#Interactive Mode
"""
O/P:
(.venv) gauravmtwt1@gauravmtwt1-IdeaPad-3-15ADA05:~/Documents/Python for DevOps/github_python_topic_upload$ ./Using_Fire.py sailors Hiya Gaurav -- --interactive
Hiya Gaurav
Fire is starting a Python REPL with the following objects:
Modules: fire
Objects: Cli, Ships, Using_Fire.py, component, result, sailors, self, trace
Python 3.10.12 (main, Mar 22 2024, 16:50:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
"""