-
Notifications
You must be signed in to change notification settings - Fork 718
FIX: CLI bug fixes and minor updates #1559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jsong468
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
jsong468:cli_updates_pt2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
af8f8e0
updates
jsong468 806e7be
Merge branch 'main' into cli_updates_pt2
jsong468 642c0d2
fix bug
jsong468 105ef1f
copilot doc suggestion
jsong468 b632eb6
Merge branch 'main' into cli_updates_pt2
jsong468 8e90714
small check for not None
jsong468 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ class PyRITShell(cmd.Cmd): | |
| Commands: | ||
| list-scenarios - List all available scenarios | ||
| list-initializers - List all available initializers | ||
| list-targets - List all available targets from the registry | ||
| list-targets [opts] - List all available targets from the registry | ||
| run <scenario> [opts] - Run a scenario with optional parameters | ||
| scenario-history - List all previous scenario runs | ||
| print-scenario [N] - Print detailed results for scenario run(s) | ||
|
|
@@ -166,6 +166,44 @@ def _ensure_initialized(self) -> None: | |
| self._init_complete.wait() | ||
| self._raise_init_error() | ||
|
|
||
| def _rebuild_context( | ||
| self, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add most of this into Right now we're setting a bunch of private variables and whatnot. I think |
||
| *, | ||
| initializer_names: Optional[list[Any]] = None, | ||
| initialization_scripts: Optional[list[Path]] = None, | ||
| log_level: Optional[int] = None, | ||
| ) -> frontend_core.FrontendCore: | ||
| """ | ||
| Create a per-command FrontendCore that inherits the shell's startup config. | ||
|
|
||
| Propagates config_file, database, and env_files from the shell's startup | ||
| kwargs, then overrides initializer_names, initialization_scripts, and | ||
| log_level for the current command. Shares registries with the shell | ||
| context to avoid redundant re-discovery. | ||
|
|
||
| Args: | ||
| initializer_names (Optional[list[Any]]): Per-command initializer overrides. | ||
| initialization_scripts (Optional[list[Path]]): Per-command script overrides. | ||
| log_level (Optional[int]): Per-command log level override. | ||
|
|
||
| Returns: | ||
| frontend_core.FrontendCore: A new context ready for use in a command. | ||
| """ | ||
| cmd_kwargs = dict(self._context_kwargs) | ||
| if initializer_names is not None: | ||
| cmd_kwargs["initializer_names"] = initializer_names | ||
| if initialization_scripts is not None: | ||
| cmd_kwargs["initialization_scripts"] = initialization_scripts | ||
| cmd_kwargs["log_level"] = log_level if log_level is not None else self.default_log_level | ||
|
|
||
| cmd_context = self._fc.FrontendCore(**cmd_kwargs) | ||
| cmd_context._scenario_registry = self.context._scenario_registry | ||
| cmd_context._initializer_registry = self.context._initializer_registry | ||
| cmd_context._initialized = True | ||
| cmd_context._silent_reinit = True | ||
|
|
||
| return cmd_context | ||
|
|
||
| def cmdloop(self, intro: Optional[str] = None) -> None: | ||
| """Override cmdloop to play animated banner before starting the REPL.""" | ||
| if intro is None: | ||
|
|
@@ -189,6 +227,9 @@ def cmdloop(self, intro: Optional[str] = None) -> None: | |
|
|
||
| def do_list_scenarios(self, arg: str) -> None: | ||
| """List all available scenarios.""" | ||
| if arg.strip(): | ||
| print(f"Error: list-scenarios does not accept arguments, got: {arg.strip()}") | ||
| return | ||
| self._ensure_initialized() | ||
| try: | ||
| asyncio.run(self._fc.print_scenarios_list_async(context=self.context)) | ||
|
|
@@ -197,17 +238,53 @@ def do_list_scenarios(self, arg: str) -> None: | |
|
|
||
| def do_list_initializers(self, arg: str) -> None: | ||
| """List all available initializers.""" | ||
| if arg.strip(): | ||
| print(f"Error: list-initializers does not accept arguments, got: {arg.strip()}") | ||
| return | ||
| self._ensure_initialized() | ||
| try: | ||
| asyncio.run(self._fc.print_initializers_list_async(context=self.context)) | ||
| except Exception as e: | ||
| print(f"Error listing initializers: {e}") | ||
|
|
||
| def do_list_targets(self, arg: str) -> None: | ||
| """List all available targets from the TargetRegistry.""" | ||
| """ | ||
| List all available targets from the TargetRegistry. | ||
|
|
||
| Usage: | ||
| list-targets | ||
| list-targets --initializers <name> [<name> ...] | ||
| list-targets --initialization-scripts <path> [<path> ...] | ||
|
|
||
| Options: | ||
| --initializers <name> ... Built-in initializers to run first | ||
| --initialization-scripts <...> Custom Python scripts to run first | ||
|
|
||
| Examples: | ||
| list-targets --initializers target | ||
| list-targets --initializers target:tags=default,scorer | ||
| """ | ||
| self._ensure_initialized() | ||
| try: | ||
| asyncio.run(self._fc.print_targets_list_async(context=self.context)) | ||
| list_targets_context = self.context | ||
| if arg.strip(): | ||
| args = self._fc.parse_list_targets_arguments(args_string=arg) | ||
|
|
||
| resolved_scripts = None | ||
| if args["initialization_scripts"]: | ||
| resolved_scripts = self._fc.resolve_initialization_scripts( | ||
| script_paths=args["initialization_scripts"] | ||
| ) | ||
| list_targets_context = self._rebuild_context( | ||
| initialization_scripts=resolved_scripts, | ||
| initializer_names=args["initializers"], | ||
| ) | ||
|
|
||
| asyncio.run(self._fc.print_targets_list_async(context=list_targets_context)) | ||
| except ValueError as e: | ||
| print(f"Error: {e}") | ||
| except FileNotFoundError as e: | ||
| print(f"Error: {e}") | ||
| except Exception as e: | ||
| print(f"Error listing targets: {e}") | ||
|
|
||
|
|
@@ -292,16 +369,13 @@ def do_run(self, line: str) -> None: | |
| print(f"Error: {e}") | ||
| return | ||
|
|
||
| # Create a context for this run with overrides | ||
| run_context = self._fc.FrontendCore( | ||
| initialization_scripts=resolved_scripts, | ||
| # Create a context for this run with per-command overrides, | ||
| # inheriting config_file, database, and env_files from startup. | ||
| run_context = self._rebuild_context( | ||
| initializer_names=args["initializers"], | ||
| log_level=args["log_level"] if args["log_level"] else self.default_log_level, | ||
| initialization_scripts=resolved_scripts, | ||
| log_level=args["log_level"], | ||
| ) | ||
| # Use the existing registries (don't reinitialize) | ||
| run_context._scenario_registry = self.context._scenario_registry | ||
| run_context._initializer_registry = self.context._initializer_registry | ||
| run_context._initialized = True | ||
|
|
||
| try: | ||
| result = asyncio.run( | ||
|
|
@@ -338,6 +412,9 @@ def do_scenario_history(self, arg: str) -> None: | |
|
|
||
| Shows a numbered list of all scenario runs with the commands used. | ||
| """ | ||
| if arg.strip(): | ||
| print(f"Error: scenario-history does not accept arguments, got: {arg.strip()}") | ||
| return | ||
| if not self._scenario_history: | ||
| print("No scenario runs in history.") | ||
| return | ||
|
|
@@ -467,8 +544,9 @@ def do_help(self, arg: str) -> None: | |
| print(" pyrit_shell") | ||
| print(" pyrit_shell --config-file ./my_config.yaml --log-level DEBUG") | ||
| else: | ||
| # Show help for specific command | ||
| super().do_help(arg) | ||
| # Convert hyphens to underscores (e.g. help list-targets -> help list_targets) for command lookup | ||
| normalized_arg = arg.replace("-", "_") | ||
| super().do_help(normalized_arg) | ||
|
|
||
| def do_exit(self, arg: str) -> bool: | ||
| """ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's some opportunity to share a lot of code here. One idea claude helped with might be something like this: