forked from AbeTavarez/Python_DevOps_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
43 lines (29 loc) Β· 1.12 KB
/
Copy pathparse.py
File metadata and controls
43 lines (29 loc) Β· 1.12 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
#!/usr/bin/env python3
# * Getting arguments from the terminal in a vanilla way(no modules).
# We need the sys module
import sys
def user_greeting(greeting, name):
message = f'{greeting} {name}'
print(message)
# The expression (if __name__ == '__main__':) will test if we are running the script from the terminal.
if __name__ == '__main__':
greeting = 'Hello'
name = 'User'
if '--help' in sys.argv:
help_message = f'Usage: {sys.argv[0]} --name <NAME> --greeting <GREETING>'
print(help_message)
sys.exit()
if '--name' in sys.argv:
# get position after name flag
name_index = sys.argv.index('--name') + 1
if name_index < len(sys.argv):
name = sys.argv[name_index]
if '--greeting' in sys.argv:
# get position after greeting flag
greeting_index = sys.argv.index('--greeting') + 1
if greeting_index < len(sys.argv):
greeting = sys.argv[greeting_index]
user_greeting(greeting, name)
# * First make the script executable
# * Now you can run the script:
# ./parse.py --greeting 'Welcome to the Matrix' --name "Neo"