def add_subcommands(): for subcommand_name in sorted(Subcommand.list_available()): if subcommand_name in subcommands: continue subcommands.add(subcommand_name) subcommand_class = Subcommand.by_name(subcommand_name) subcommand = subcommand_class() subparser = subcommand.add_subparser(subparsers) if subcommand_class.requires_plugins: subparser.add_argument( "--include-package", type=str, action="append", default=[], help="additional packages to include", )
def parse_args( prog: Optional[str] = None ) -> Tuple[argparse.ArgumentParser, argparse.Namespace]: """ Creates the argument parser for the main program and uses it to parse the args. """ parser = ArgumentParserWithDefaults(description="Run AllenNLP", prog=prog) parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}") subparsers = parser.add_subparsers(title="Commands", metavar="") subcommands: Set[str] = set() def add_subcommands(): for subcommand_name in sorted(Subcommand.list_available()): if subcommand_name in subcommands: continue subcommands.add(subcommand_name) subcommand_class = Subcommand.by_name(subcommand_name) subcommand = subcommand_class() subparser = subcommand.add_subparser(subparsers) if subcommand_class.requires_plugins: subparser.add_argument( "--include-package", type=str, action="append", default=[], help="additional packages to include", ) # Add all default registered subcommands first. add_subcommands() # If we need to print the usage/help, or the subcommand is unknown, # we'll call `import_plugins()` to register any plugin subcommands first. argv = sys.argv[1:] plugins_imported: bool = False if not argv or argv == ["--help"] or argv[0] not in subcommands: import_plugins() plugins_imported = True # Add subcommands again in case one of the plugins has a registered subcommand. add_subcommands() # Now we can parse the arguments. args = parser.parse_args() if not plugins_imported and Subcommand.by_name( argv[0]).requires_plugins: # type: ignore import_plugins() return parser, args
def test_file_plugin_loaded(self): plugins_root = self.FIXTURES_ROOT / "plugins" sys.argv = ["allennlp"] available_plugins = set(discover_plugins()) self.assertSetEqual(set(), available_plugins) with pushd(plugins_root): main() subcommands_available = Subcommand.list_available() self.assertIn("d", subcommands_available)
def test_file_plugin_loaded(self): plugins_root = self.FIXTURES_ROOT / "plugins" sys.argv = ["allennlp"] available_plugins = set(discover_plugins()) assert available_plugins == set() with pushd(plugins_root): main() subcommands_available = Subcommand.list_available() assert "d" in subcommands_available
def create_parser(prog: Optional[str] = None) -> argparse.ArgumentParser: """ Creates the argument parser for the main program. """ parser = ArgumentParserWithDefaults(description="Run AllenNLP", prog=prog) parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}") subparsers = parser.add_subparsers(title="Commands", metavar="") for subcommand_name in sorted(Subcommand.list_available()): subcommand_class = Subcommand.by_name(subcommand_name) subcommand = subcommand_class() subparser = subcommand.add_subparser(subparsers) subparser.add_argument( "--include-package", type=str, action="append", default=[], help="additional packages to include", ) return parser
def test_namespace_plugin_loaded(self): plugins_root = self.FIXTURES_ROOT / "plugins" # "a" sets a "global" namespace plugin, because it's gonna be installed with pip. project_a_fixtures_root = plugins_root / "project_a" sys.argv = ["allennlp"] available_plugins = set(discover_plugins()) self.assertSetEqual(set(), available_plugins) with pip_install(project_a_fixtures_root, "a"): main() subcommands_available = Subcommand.list_available() self.assertIn("a", subcommands_available)
def test_file_plugin_loaded(self): plugins_root = self.FIXTURES_ROOT / "plugins" # "d" sets a "local" file plugin, because it's supposed to be run from that directory # and has a ".allennlp_plugins" file in it. project_d_fixtures_root = plugins_root / "project_d" sys.argv = ["allennlp"] available_plugins = set(discover_plugins()) self.assertSetEqual(set(), available_plugins) with pushd(project_d_fixtures_root): main() subcommands_available = Subcommand.list_available() self.assertIn("d", subcommands_available)