Skip to content

Commit 7527bc8

Browse files
authored
Handle constants in CLI (#99)
1 parent 0a1c5b1 commit 7527bc8

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/nemo_run/cli/api.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ def cli_command(
726726
default_plugins: Optional[List[Plugin]] = None,
727727
type: Literal["task", "experiment"] = "task",
728728
command_kwargs: Dict[str, Any] = {},
729+
is_main: bool = False,
729730
):
730731
"""
731732
Create a CLI command for the given function.
@@ -788,8 +789,10 @@ def command(
788789
if default_plugins:
789790
self.plugins = default_plugins
790791

791-
_load_entrypoints()
792+
if not is_main:
793+
_load_entrypoints()
792794
_load_workspace()
795+
793796
self.cli_execute(fn, ctx.args, type)
794797

795798
return command
@@ -1181,13 +1184,13 @@ def parse_partial(self, args: List[str], **default_args) -> Partial[T]:
11811184
def cli(self, parent: typer.Typer):
11821185
self._add_command(parent)
11831186

1184-
def _add_command(self, typer_instance: typer.Typer):
1187+
def _add_command(self, typer_instance: typer.Typer, is_main: bool = False):
11851188
if self.enable_executor:
1186-
self._add_executor_command(typer_instance)
1189+
self._add_executor_command(typer_instance, is_main=is_main)
11871190
else:
1188-
self._add_simple_command(typer_instance)
1191+
self._add_simple_command(typer_instance, is_main=is_main)
11891192

1190-
def _add_simple_command(self, typer_instance: typer.Typer):
1193+
def _add_simple_command(self, typer_instance: typer.Typer, is_main: bool = False):
11911194
@typer_instance.command(
11921195
self.name,
11931196
help=self.help_str,
@@ -1203,7 +1206,7 @@ def cmd_cli(ctx: typer.Context):
12031206
console.print(f"[bold red]Error: {str(e)}[/bold red]")
12041207
sys.exit(1)
12051208

1206-
def _add_executor_command(self, parent: typer.Typer):
1209+
def _add_executor_command(self, parent: typer.Typer, is_main: bool = False):
12071210
help = self.help_str
12081211
colored_help = None
12091212
if help:
@@ -1224,6 +1227,7 @@ class CLITaskCommand(EntrypointCommand):
12241227
help=colored_help,
12251228
cls=CLITaskCommand,
12261229
),
1230+
is_main=is_main,
12271231
)
12281232

12291233
def _add_options_to_command(self, command: Callable):
@@ -1242,7 +1246,7 @@ def _execute_simple(self, args: List[str], console: Console):
12421246

12431247
def main(self):
12441248
app = typer.Typer(help=self.help_str, pretty_exceptions_enable=False)
1245-
self._add_command(app)
1249+
self._add_command(app, is_main=True)
12461250
app(standalone_mode=False)
12471251

12481252
def help(self, console=Console(), with_docs: bool = True):

src/nemo_run/cli/cli_parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,10 @@ def parse_single_factory(factory_str):
11671167
module_name, function_name = factory_name.rsplit(".", 1)
11681168
module = importlib.import_module(module_name)
11691169
factory_fn = getattr(module, function_name)
1170+
1171+
# Handle constants
1172+
if not callable(factory_fn):
1173+
return factory_fn
11701174
except (ImportError, AttributeError):
11711175
pass # If import fails, continue with other parsing methods
11721176

0 commit comments

Comments
 (0)