Commit e31b7f2a by anthraxx Committed by Alexander Popov

argparse: using python module instead of manual getopt

parent a2b319a3
...@@ -16,8 +16,9 @@ ...@@ -16,8 +16,9 @@
# pti=on # pti=on
# kernel.kptr_restrict=1 # kernel.kptr_restrict=1
import sys, getopt import sys
from collections import namedtuple from collections import namedtuple
from argparse import ArgumentParser
import re import re
debug_mode = False # set it to True to print the unknown options from the config debug_mode = False # set it to True to print the unknown options from the config
...@@ -213,33 +214,28 @@ def check_config_file(fname): ...@@ -213,33 +214,28 @@ def check_config_file(fname):
f.close() f.close()
def usage(name): if __name__ == '__main__':
print('Usage: {} [-p | -c <config_file>] '.format(name)) parser = ArgumentParser(description='Kconfig hardened check')
print(' -p, --print\n\tprint hardening preferences') parser.add_argument('-p', '--print', default=False, action='store_true', help='print hardening preferences')
print(' -c <config_file>, --config=<config_file>\n\tcheck the config_file against these preferences') parser.add_argument('-c', '--config', help='check the config_file against these preferences')
sys.exit(1) parser.add_argument('--debug', default=False, action='store_true', help='enable internal debug mode')
args = parser.parse_args()
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])
construct_opt_list() construct_opt_list()
for opt, arg in opts: if args.print:
if opt in ('-p', '--print'):
print_opt_list() print_opt_list()
sys.exit() sys.exit(0)
elif opt in ('-c', '--config'):
check_config_file(arg) if args.debug:
debug_mode = True
if args.config:
check_config_file(args.config)
if error_count == 0: if error_count == 0:
print('[+] config check is PASSED') print('[+] config check is PASSED')
sys.exit(0)
else: else:
sys.exit('[-] config check is NOT PASSED: {} errors'.format(error_count)) sys.exit('[-] config check is NOT PASSED: {} errors'.format(error_count))
main(sys.argv) parser.print_help()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment