<div>Simple change from using getopt to using argparse. Also fixes <a href="https://bugs.archlinux.org/task/56995">https://bugs.archlinux.org/task/56995</a><br></div><div class="protonmail_signature_block protonmail_signature_block-empty"><div class="protonmail_signature_block-user protonmail_signature_block-empty"><br></div><div class="protonmail_signature_block-proton protonmail_signature_block-empty"><br></div></div><div><br></div><div>diff --git a/namcap.py b/namcap.py<br>index a7f532a..bb2754e 100755<br>--- a/namcap.py<br>+++ b/namcap.py<br>@@ -19,10 +19,10 @@<br> #<br> # <br> <br>-import getopt<br> import os<br> import sys<br> import tarfile<br>+import argparse<br> <br> import Namcap.depends<br> import Namcap.tags<br>@@ -33,22 +33,6 @@ def get_modules():<br> """Return all possible modules (rules)"""<br> return Namcap.rules.all_rules<br> <br>-def usage():<br>- """Display usage information"""<br>- print("")<br>- print("Usage: " + sys.argv[0] + " [OPTIONS] packages")<br>- print("")<br>- print("Options are:")<br>- print(" -L, --list : list available rules")<br>- print(" -i : prints information (debug) responses from rules")<br>- print(" -m : makes the output parseable (machine-readable)")<br>- print(" -e rulelist, --exclude=rulelist : don't apply RULELIST rules to the package")<br>- print(" -r rulelist, --rules=rulelist : only apply RULELIST rules to the package")<br>- print(" -t tags : use a custom tag file")<br>- print(" -v version : print version and exit")<br>-<br>- sys.exit(2)<br>-<br> def open_package(filename):<br> try:<br> tar = tarfile.open(filename, "r")<br>@@ -61,17 +45,6 @@ def open_package(filename):<br> return None<br> return tar<br> <br>-def check_rules_exclude(optlist): <br>- '''Check if the -r (--rules) and the -r (--exclude) options<br>- are being used at same time'''<br>- args_used = 0<br>- for i in optlist: <br>- if '-r' in i or '-e' in i:<br>- args_used += 1<br>- if '--rules' in i or '--exclude' in i:<br>- args_used += 1 <br>- return args_used<br>-<br> def show_messages(name, key, messages):<br> for msg in messages:<br> print("%s %s: %s" % (name, key, Namcap.tags.format_message(msg)))<br>@@ -162,74 +135,57 @@ def process_pkgbuild(package, modules):<br> <br> # Main<br> modules = get_modules()<br>-info_reporting = 0<br>-machine_readable = False<br>-filename = None<br>-<br>-# get our options and process them<br>-try:<br>- optlist, args = getopt.getopt(sys.argv[1:], "ihmr:e:t:Lv",<br>- ["info", "help", "machine-readable", "rules=",<br>- "exclude=", "tags=", "list", "version"])<br>-except getopt.GetoptError:<br>- usage()<br>-<br>-active_modules = {}<br> <br>-# Verifying if we are using the -r and -r options at same time<br>-if check_rules_exclude(optlist) > 1:<br>- print("You cannot use '-r' (--rules) and '-e' (-exclude) options at same time")<br>- usage()<br>-<br>-for i, k in optlist:<br>- if i in ('-L', '--list'):<br>+# Let's handle those options!<br>+version = Namcap.version.get_version()<br>+<br>+parser = argparse.ArgumentParser()<br>+parser.add_argument('-L', '--list', action='store_true', help='List available rules')<br>+parser.add_argument('-i', '--info', action='store_const', const=1, default=0, help='Prints information (debug) responses from rules')<br>+parser.add_argument('-m', '--machine-readable', action='store_true', help='Makes the output parseable (machine-readable)')<br>+parser.add_argument('-t', '--tags', action='store', help='Use a custom tag file')<br>+parser.add_argument('packages', nargs='+')<br>+pargroup = parser.add_mutually_exclusive_group()<br>+pargroup.add_argument('-e', '--exclude', action='store', metavar='RULELIST', help="Don't apply RULELIST rules to the package")<br>+pargroup.add_argument('-r', '--rules', action='store', metavar='RULELIST', help='Only apply RULELIST rules to the packag')<br>+parser.add_argument('-v', '--version', action='version', version=version)<br>+args = parser.parse_args()<br>+<br>+# Do something with all these options<br>+if (args.list):<br> print("-"*20 + " Namcap rule list " + "-"*20)<br> for j in sorted(modules):<br> print("%-20s: %s" % (j, modules[j].description))<br>- sys.exit(2)<br>-<br>- if i in ('-r', '--rules'):<br>- module_list = k.split(',')<br>- for j in module_list:<br>- if j in modules:<br>- active_modules[j] = modules[j]<br>- else:<br>- print("Error: Rule '%s' does not exist" % j)<br>- usage()<br>-<br>- # Used to exclude some rules from the check<br>- if i in ('-e', '--exclude'):<br>- module_list = k.split(',')<br>- active_modules.update(modules)<br>- for j in module_list:<br>- if j in modules:<br>- active_modules.pop(j)<br>- else:<br>- print("Error: Rule '%s' does not exist" % j)<br>- usage()<br>-<br>- if i in ('-i', '--info'):<br>- info_reporting = 1<br>-<br>- if i in ('-h', '--help'):<br>- usage()<br>- if i in ('-m', '--machine-readable'):<br>- machine_readable = True<br>-<br>- if i in ('-t', '--tags'):<br>- filename = k<br>-<br>- if i in ('-v', '--version'):<br>- print(Namcap.version.get_version())<br>- sys.exit(0)<br>-<br>-# If there are no args, print usage<br>-if (args == []):<br>- usage()<br>+ parser.exit(2)<br> <br>-Namcap.tags.load_tags(filename = filename, machine = machine_readable)<br>+info_reporting = args.info<br>+machine_readable = args.machine_readable<br>+filename = args.tags<br> <br>-packages = args<br>+if (args.rules):<br>+ module_list = args.rules<br>+ for j in module_list:<br>+ if j in modules:<br>+ active_modules[j] = modules[j]<br>+ else:<br>+ print("Error: Rule '%s' does not exist" % j)<br>+ parser.exit(2)<br>+<br>+if (args.exclude):<br>+ module_list = args.exclude<br>+ active_modules.update(modules)<br>+ for j in module_list:<br>+ if j in modules:<br>+ active_modules.pop(j)<br>+ else:<br>+ print("Error: Rule '%s' does not exist" % j)<br>+ parser.exit(2)<br>+<br>+packages = args.packages<br>+<br>+active_modules = {}<br>+<br>+Namcap.tags.load_tags(filename = filename, machine = machine_readable)<br> <br> # No rules selected? Then select them all!<br> if len(active_modules) == 0:<br>@@ -239,7 +195,7 @@ if len(active_modules) == 0:<br> for package in packages:<br> if not os.access(package, os.R_OK):<br> print("Error: Problem reading %s" % package)<br>- usage()<br>+ parser.print_usage()<br> <br> if os.path.isfile(package) and tarfile.is_tarfile(package):<br> process_realpackage(package, active_modules)<br><br></div>