From: anthraxx Date: Mon, 9 Jul 2018 00:25:23 +0000 (+0200) Subject: argparse: using python module instead of manual getopt X-Git-Tag: v0.5.2~91 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=e31b7f2af048b90bff60d2f4d6fa728f8912595c;p=kconfig-hardened-check.git argparse: using python module instead of manual getopt --- diff --git a/kconfig-hardened-check.py b/kconfig-hardened-check.py index e81cbe9..9b1c066 100755 --- a/kconfig-hardened-check.py +++ b/kconfig-hardened-check.py @@ -16,8 +16,9 @@ # pti=on # kernel.kptr_restrict=1 -import sys, getopt +import sys from collections import namedtuple +from argparse import ArgumentParser import re debug_mode = False # set it to True to print the unknown options from the config @@ -213,33 +214,28 @@ def check_config_file(fname): f.close() -def usage(name): - print('Usage: {} [-p | -c ] '.format(name)) - print(' -p, --print\n\tprint hardening preferences') - print(' -c , --config=\n\tcheck the config_file against these preferences') - sys.exit(1) +if __name__ == '__main__': + parser = ArgumentParser(description='Kconfig hardened check') + parser.add_argument('-p', '--print', default=False, action='store_true', help='print hardening preferences') + parser.add_argument('-c', '--config', help='check the config_file against these preferences') + parser.add_argument('--debug', default=False, action='store_true', help='enable internal debug mode') + args = parser.parse_args() + construct_opt_list() -def main(argv): - try: - opts, args = getopt.getopt(argv[1:], 'pc:', ['print', 'config=']) - except getopt.GetoptError: - usage(argv[0]) - - if not opts: - usage(argv[0]) + if args.print: + print_opt_list() + sys.exit(0) - construct_opt_list() + if args.debug: + debug_mode = True - for opt, arg in opts: - if opt in ('-p', '--print'): - print_opt_list() - sys.exit() - elif opt in ('-c', '--config'): - check_config_file(arg) - if error_count == 0: - print('[+] config check is PASSED') - else: - sys.exit('[-] config check is NOT PASSED: {} errors'.format(error_count)) + if args.config: + check_config_file(args.config) + if error_count == 0: + print('[+] config check is PASSED') + sys.exit(0) + else: + sys.exit('[-] config check is NOT PASSED: {} errors'.format(error_count)) -main(sys.argv) + parser.print_help()