Skip to content

Commit 8f2db86

Browse files
authored
Fixed linting issues and improved CLI error messages.
This update fixes long lines to follow the 88-character limit and formats the code properly. It also improves error messages in the serve command to make them clearer. The changes ensure the code passes linting checks and works smoothly with ruff and make lint
1 parent e120378 commit 8f2db86

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

libs/cli/langchain_cli/cli.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,32 @@
1010
from langchain_cli.namespaces.migrate import main as migrate_namespace
1111
from langchain_cli.utils.packages import get_langserve_export, get_package_root
1212

13+
TEMPLATE_CMD = "template"
14+
APP_CMD = "app"
15+
INTEGRATION_CMD = "integration"
16+
1317
app = typer.Typer(no_args_is_help=True, add_completion=False)
1418
app.add_typer(
15-
template_namespace.package_cli, name="template", help=template_namespace.__doc__
19+
template_namespace.package_cli, name=TEMPLATE_CMD, help=template_namespace.__doc__
1620
)
17-
app.add_typer(app_namespace.app_cli, name="app", help=app_namespace.__doc__)
21+
app.add_typer(app_namespace.app_cli, name=APP_CMD, help=app_namespace.__doc__)
1822
app.add_typer(
1923
integration_namespace.integration_cli,
20-
name="integration",
24+
name=INTEGRATION_CMD,
2125
help=integration_namespace.__doc__,
2226
)
2327

2428
app.command(
2529
name="migrate",
2630
context_settings={
27-
# Let Grit handle the arguments
2831
"allow_extra_args": True,
2932
"ignore_unknown_options": True,
3033
},
31-
)(
32-
migrate_namespace.migrate,
33-
)
34+
)(migrate_namespace.migrate)
3435

3536

3637
def version_callback(show_version: bool) -> None:
38+
"""Displays the CLI version and exits."""
3739
if show_version:
3840
typer.echo(f"langchain-cli {__version__}")
3941
raise typer.Exit()
@@ -49,34 +51,41 @@ def main(
4951
callback=version_callback,
5052
is_eager=True,
5153
),
52-
):
54+
) -> None:
55+
"""CLI entry point."""
5356
pass
5457

5558

5659
@app.command()
5760
def serve(
5861
*,
5962
port: Annotated[
60-
Optional[int], typer.Option(help="The port to run the server on")
63+
Optional[int],
64+
typer.Option(help="The port to run the server on"),
6165
] = None,
6266
host: Annotated[
63-
Optional[str], typer.Option(help="The host to run the server on")
67+
Optional[str],
68+
typer.Option(help="The host to run the server on"),
6469
] = None,
6570
) -> None:
66-
"""
67-
Start the LangServe app, whether it's a template or an app.
68-
"""
69-
70-
# see if is a template
71+
"""Start the LangServe app, either as a template or an app."""
7172
try:
7273
project_dir = get_package_root()
7374
pyproject = project_dir / "pyproject.toml"
7475
get_langserve_export(pyproject)
75-
except KeyError:
76-
# not a template
76+
except KeyError as e:
77+
typer.secho(
78+
f"Error: {e}. This project does not seem to be a template.",
79+
fg=typer.colors.RED,
80+
)
7781
app_namespace.serve(port=port, host=host)
82+
except FileNotFoundError:
83+
typer.secho(
84+
"Error: `pyproject.toml` not found. "
85+
"Ensure you are in a valid project directory.",
86+
fg=typer.colors.RED,
87+
)
7888
else:
79-
# is a template
8089
template_namespace.serve(port=port, host=host)
8190

8291

0 commit comments

Comments
 (0)