From f82bd9544262a7a44a598854c57b53a33d5ebaec Mon Sep 17 00:00:00 2001 From: Alexander Neff Date: Sat, 16 Dec 2023 15:40:56 +0100 Subject: [PATCH] Add argcomplete support for modules --- nxc/cli.py | 23 +++++++++++++++++++++-- nxc/loaders/moduleloader.py | 3 +++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/nxc/cli.py b/nxc/cli.py index 67a3cd32..bed36308 100755 --- a/nxc/cli.py +++ b/nxc/cli.py @@ -1,6 +1,12 @@ -import argparse, argcomplete +import argparse +import argcomplete import sys from argparse import RawTextHelpFormatter +from os import listdir +from os.path import dirname +from os.path import join as path_join +import nxc +from nxc.paths import NXC_PATH from nxc.loaders.protocolloader import ProtocolLoader from nxc.helpers.logger import highlight from nxc.logger import nxc_logger @@ -41,7 +47,7 @@ def gen_cli_args(): # we do module arg parsing here so we can reference the module_list attribute below module_parser = argparse.ArgumentParser(add_help=False) mgroup = module_parser.add_mutually_exclusive_group() - mgroup.add_argument("-M", "--module", action="append", metavar="MODULE", help="module to use") + mgroup.add_argument("-M", "--module", choices=get_module_names(), action="append", metavar="MODULE", help="module to use") module_parser.add_argument("-o", metavar="MODULE_OPTION", nargs="+", default=[], dest="module_options", help="module options") module_parser.add_argument("-L", "--list-modules", action="store_true", help="list available modules") module_parser.add_argument("--options", dest="show_module_options", action="store_true", help="display module options") @@ -93,3 +99,16 @@ def gen_cli_args(): sys.exit(1) return args + + +def get_module_names(): + """Get module names without initializing them""" + modules = [] + modules_paths = [ + path_join(dirname(nxc.__file__), "modules"), + path_join(NXC_PATH, "modules"), + ] + + for path in modules_paths: + modules.extend([module[:-3] for module in listdir(path) if module[-3:] == ".py" and module != "example_module.py"]) + return sorted(modules, key=str.casefold) diff --git a/nxc/loaders/moduleloader.py b/nxc/loaders/moduleloader.py index 43433da4..02c98150 100755 --- a/nxc/loaders/moduleloader.py +++ b/nxc/loaders/moduleloader.py @@ -24,6 +24,9 @@ class ModuleLoader: if not hasattr(module, "name"): self.logger.fail(f"{module_path} missing the name variable") module_error = True + elif hasattr(module, "name") and module.name != module_path.split("/")[-1][:-3]: + self.logger.fail(f"{module_path} name variable must match the filename") + module_error = True elif not hasattr(module, "description"): self.logger.fail(f"{module_path} missing the description variable") module_error = True