Skip to content

Commit

Permalink
Add CLI support for init_params (#3)
Browse files Browse the repository at this point in the history
* add init_params

* readme, formatting
  • Loading branch information
vachillo committed Dec 15, 2023
1 parent b5d8cc1 commit 20d5c55
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ poetry run gt app new --directory ~/workplace demo_app
Test an App locally:

```shell
poetry run gt app run --directory ~/workplace/demo_app --arg "what is griptape?"
poetry run gt app run --directory ~/workplace/demo_app --arg "what is griptape?" --init-param "key" "value"
```

Create an App on the Cloud:
Expand Down
20 changes: 17 additions & 3 deletions griptape/cli/commands/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def app(ctx):
@click.option(
"--package_manager",
"-p",
type=click.Choice(["pip", "poetry"]),
type=click.Choice(["pip"]),
help="Package manager to use for Griptape app.",
default="pip",
show_default=True,
Expand Down Expand Up @@ -55,6 +55,15 @@ def new(name: str, package_manager: str, directory: str, griptape_version: str)

@app.command(name="run")
@click.option("--arg", "-a", multiple=True, type=str, required=True)
@click.option(
"--init-param",
"-i",
"init_params",
type=(str, str),
multiple=True,
help="Initialization parameters for the app in the format 'key value'.",
required=False,
)
@click.option(
"--directory",
"-d",
Expand All @@ -63,12 +72,17 @@ def new(name: str, package_manager: str, directory: str, griptape_version: str)
default=os.getcwd(),
show_default=True,
)
def run(arg: list[str], directory: str) -> TextArtifact:
def run(
arg: list[str], init_params: list[tuple[str, str]], directory: str
) -> TextArtifact:
"""
Run a Griptape app.
"""

echo(f"Running app")
params = {k: v for k, v in init_params}

structure_runner = StructureRunner(args=arg, app_directory=directory)
structure_runner = StructureRunner(
args=arg, init_params=params, app_directory=directory
)
structure_runner.run()
5 changes: 4 additions & 1 deletion griptape/cli/core/structure_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
@define
class StructureRunner:
args: list[str] = field(kw_only=True)
init_params: Optional[dict[str, str]] = field(
kw_only=True, default=Factory(lambda: dict())
)
app_directory: Optional[str] = field(
kw_only=True, default=Factory(lambda: os.getcwd())
)
Expand All @@ -25,7 +28,7 @@ def run(self):
from app import init_structure

try:
return init_structure(*self.args).run(*self.args)
return init_structure(*self.args, **self.init_params).run(*self.args)
except Exception as e:
raise Exception(f"Error running app: {e}")
except Exception as e:
Expand Down
Loading

0 comments on commit 20d5c55

Please sign in to comment.