# -*- coding: utf-8 -*-
# =============================================================================
# FileName: args_demo.py
# Desc: 演示解析命令参数和读取文件
# Author: GGA
# Email:
# HomePage:
# Version: 1.0.1
# LastChange: 2020-12-20
# History:
# =============================================================================
import sys
import argparse
import logging
logger = logging.getLogger()
def init_logger():
logger.setLevel(level=logging.DEBUG)
logger_format = logging.Formatter("[%(asctime)s]-[%(levelname)s]: %(message)s") # output format
sh = logging.StreamHandler(stream=sys.stdout) # output to standard output
sh.setFormatter(logger_format)
logger.addHandler(sh)
def get_parser():
parser = argparse.ArgumentParser(
description='Parse MySQL binlog to SQL you want',
add_help=False)
connect_setting = parser.add_argument_group('connect setting')
parser.add_argument(
'--help',
dest='help',
action='store_true',
help='help information',
default=False
)
connect_setting.add_argument(
'--host_file',
dest='host_file',
type=str,
default=None,
help='the file path which contain host info'
)
return parser
def parse_args(command_args):
need_print_help = False if command_args else True
parser = get_parser()
args = parser.parse_args(command_args)
if args.help or need_print_help:
parser.print_help()
sys.exit(1)
if args.host_file is None:
logger.warning("parameter host_file must input")
parser.print_help()
return args
def main(command_args):
args = parse_args(command_args)
logger.info(args.host_file)
file_handler = open(args.host_file, "r")
for line in file_handler.readlines():
logger.info(line)
if __name__ == '__main__':
init_logger()
main(sys.argv[1:])