-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli-aid.py
executable file
·99 lines (83 loc) · 2.65 KB
/
cli-aid.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
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
#!/usr/bin/env python
"""This module is a CLI Application to build help and snippets
for you to remember while in terminal.
"""
from __future__ import print_function
import os
import sys
# Current Version
VERSION=0.01
# Path to topic files
PATH_TOPICS = 'topics'
# Get the CLI Args
if len(sys.argv) == 1:
print("""
Please pass two (2) arguments:
- arg1: name of a topic file (try: demo).
- arg2: name of a method within the topic file (try: basic)
Usage $ ./ci_help.py <command> <argument>
Example $ ./cli.py demo basic
""")
sys.exit()
elif len(sys.argv) == 2:
"""
@TODO: Allow one arg only, and list out the method names
for the topic.
"""
elif len(sys.argv) <= 2:
# We want three arguments contain [scriptname, arg1, arg2]
print('Please provide two arguments: <service> <command>')
sys.exit()
# Assign Service and Command, lowercase is safecase!
ARG_SERVICE = sys.argv[1].lower()
ARG_COMMAND = sys.argv[2].lower()
# The module we hope to import
IMPORT_MODULE = '%s.%s' % (PATH_TOPICS, ARG_SERVICE)
# List Topics In Order: [__init__, git, etc]
AVAILABLE_TOPICS = sorted([
# Strip the *.py Extensions for our list
os.path.splitext(x)[0] for x in os.listdir(PATH_TOPICS)
])
# Make sure the TOPIC exists
if ARG_SERVICE not in AVAILABLE_TOPICS:
print("Sorry, the topic %s is not available." % ARG_SERVICE)
sys.exit()
if ARG_SERVICE in AVAILABLE_TOPICS:
REAL_SERVICE = __import__(IMPORT_MODULE, fromlist=[''])
# Make sure we have the ARG_COMMAND
if not hasattr(REAL_SERVICE, ARG_COMMAND):
print('Sorry, the topic %s has no command: %s' % ARG_COMMAND)
sys.exit()
# Process the Command
RUN_METHOD = getattr(REAL_SERVICE, ARG_COMMAND)
RESULT = RUN_METHOD()
# Get Result Type
result_type = None
if isinstance(RESULT, dict):
result_type = 'dict'
elif isinstance(RESULT, str) and len(RESULT) != 0:
result_type = 'str'
# Set Flags
ran_flags = False
ran_commands = False
ran_plain = False
# Display based on type:
if result_type is 'dict':
# Print Out: flags
if 'flags' in RESULT:
ran_flags = True
print("Flags:" + RESULT['flags'])
# Print Out: commands
if 'commands' in RESULT:
ran_commands = True
print("Commands:" + RESULT['commands'])
sys.exit()
elif result_type is 'str':
# Print Out: Plain text
if len(RESULT) != 0:
ran_plain = True
print(RESULT)
sys.exit()
# No Results
print('No information returned from: $%s.%s' % (ARG_SERVICE, ARG_COMMAND))
sys.exit()