argparse: using python module instead of manual getopt
authoranthraxx <levente@leventepolyak.net>
Mon, 9 Jul 2018 00:25:23 +0000 (02:25 +0200)
committerAlexander Popov <alex.popov@linux.com>
Fri, 13 Jul 2018 23:15:46 +0000 (02:15 +0300)
kconfig-hardened-check.py

index e81cbe9fd2bd22b89be68369c5d42e9a9d8fe7b4..9b1c066927d87354f60a980cf3733dd8bde8464b 100755 (executable)
@@ -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 <config_file>] '.format(name))
-    print(' -p, --print\n\tprint hardening preferences')
-    print(' -c <config_file>, --config=<config_file>\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()