import getopt
import sys
def usage():
sys.stdout.write("""prama_config.py usage:\n
prama_config.py usage:\n
-h, --help: print(help message.\n
-a, --get_all: get all module last log\n
-l, --get_module_last: get module last log\n
-m, --get_module_log_by_id: get module log by buildId\n""")
def get_all():
sys.stdout.write('get all module last log')
def get_module_last(module):
sys.stdout.write(f'get module last log:{module}')
def get_module_log_by_id(module, build_id):
sys.stdout.write(f"get module log by buildId:{module}#{build_id}")
def main(argv):
try:
opts, args = getopt.getopt(argv[1:], 'halm:', ['help=', 'get_all=', 'get_module_last=', 'get_module_log_by_id='])
except getopt.GetoptError:
usage()
for o, a in opts:
if o in ('-h', '--help'):
usage()
elif o in ('-a', '--get_all'):
get_all()
elif o in ('-l', '--get_module_last'):
get_module_last(args[0])
elif o in ('-m', '--get_module_log_by_id'):
get_module_log_by_id(a, args[0])
else:
print('unhandled option')
if __name__ == '__main__':
main(sys.argv)
OptionParser
from optparse import OptionParser
class TGetLog(object):
def __init__(self):
self._option = ReaderOption()
def run(self):
if self._option.get_all_log:
print(self._option.tag_args)
self._get_all_log()
if self._option.get_last_log:
self._get_last_log()
if self._option.get_log_by_id:
self._get_log_by_id()
def _get_all_log(self):
print("get all log")
def _get_last_log(self):
moduleName = self._option.tag_args[0]
print(f"get last log:{moduleName}")
def _get_log_by_id(self):
moduleName = self._option.tag_args[0]
buildID = self._option.tag_args[1]
print(f"get module log by id:{moduleName}#{buildID}")
class ReaderOption(object):
def __init__(self):
self._parser = OptionParser()
self._add_parser_options()
self._options, self._options.tag_args = self._get_vaildated_options()
def _add_parser_options(self):
options = [
('-a','--getAll',{'default':True,
'action':'store_true',
'dest':'get_all_log',
'help':'get all module log'}),
('-l','--getLastLog',{'action':'store_true',
'dest':'get_last_log',
'help':'get module last log'}),
('-m','--getLogById',{'action':'store_true',
'dest':'get_log_by_id',
'help':'get module log by id'})
]
for option in options:
self._parser.add_option(option[0],option[1],**option[2])
self._parser.set_usage("\n TGetLog.py -a")
def _get_vaildated_options(self):
option, tag_args = self._parser.parse_args()
return option, tag_args
@property
def get_all_log(self):
return self._options.get_all_log
@property
def get_last_log(self):
return self._options.get_last_log
@property
def get_log_by_id(self):
return self._options.get_log_by_id
@property
def tag_args(self):
return self._options.tag_args
if __name__=="__main__":
t = TGetLog()
t.run()