FIX: CLI bug fixes and minor updates#1559
Open
jsong468 wants to merge 9 commits intomicrosoft:mainfrom
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the PyRIT CLI/shell UX by expanding list-targets support (initializers + initialization scripts), tightening argument validation for no-arg commands, fixing help lookup for hyphenated commands, and cleaning up unused CLI plumbing.
Changes:
- Add
list-targetsoption parsing inpyrit_shelland enable--initialization-scriptshandling inpyrit_scan. - Reject unexpected args for commands that should not take arguments and normalize hyphenated command names for
help. - Refactor target listing initialization flow (remove unused
initializer_namesparam) and add unit tests covering new behaviors.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
pyrit/cli/pyrit_shell.py |
Adds arg rejection for no-arg commands; adds option parsing + context override for list-targets; fixes help for hyphenated commands. |
pyrit/cli/pyrit_scan.py |
Resolves --initialization-scripts for --list-targets and surfaces missing-script errors. |
pyrit/cli/frontend_core.py |
Removes unused initializer_names param from list_targets_async and supports initialization-scripts-only population. |
pyrit/cli/_cli_args.py |
Introduces parse_list_targets_arguments for shell-mode parsing. |
pyrit/cli/_banner.py |
Updates banner text to reflect list-targets [opts]. |
tests/unit/cli/test_pyrit_shell.py |
Adds coverage for arg rejection, list-targets option flow, and hyphenated help. |
tests/unit/cli/test_pyrit_scan.py |
Adds coverage for --list-targets with initializers and initialization scripts. |
tests/unit/cli/test_frontend_core.py |
Adds coverage for parse_list_targets_arguments and scripts-only target listing initialization. |
rlundeen2
reviewed
Apr 8, 2026
| return result | ||
|
|
||
|
|
||
| def parse_list_targets_arguments(*, args_string: str) -> dict[str, Any]: |
Contributor
There was a problem hiding this comment.
There's some opportunity to share a lot of code here. One idea claude helped with might be something like this:
from dataclasses import dataclass
from enum import Enum, auto
from typing import Callable
class _FlagKind(Enum):
MULTI = auto() # collect values until next --flag
SINGLE = auto() # consume exactly one value
POSITIONAL = auto() # first non-flag token (scenario_name)
@dataclass(frozen=True)
class _FlagSpec:
flag: str | tuple[str, ...] # e.g. "--initializers" or ("--strategies", "-s")
key: str # result dict key
kind: _FlagKind
transform: Callable | None = None # per-value transform (e.g. _parse_initializer_arg, validate_integer)
# ---- shared flag definitions (define once, reuse everywhere) ----
_INITIALIZERS = _FlagSpec("--initializers", "initializers", _FlagKind.MULTI, _parse_initializer_arg)
_INIT_SCRIPTS = _FlagSpec("--initialization-scripts", "initialization_scripts", _FlagKind.MULTI)
_LOG_LEVEL = _FlagSpec("--log-level", "log_level", _FlagKind.SINGLE, validate_log_level)
# ... etc for each flag
# ---- command specs built from shared pieces ----
_RUN_SPEC = [
_FlagSpec(None, "scenario_name", _FlagKind.POSITIONAL), # required first arg
_INITIALIZERS,
_INIT_SCRIPTS,
_FlagSpec(("--strategies", "-s"), "scenario_strategies", _FlagKind.MULTI),
_FlagSpec("--max-concurrency", "max_concurrency", _FlagKind.SINGLE,
lambda v: validate_integer(v, name="--max-concurrency", min_value=1)),
# ... remaining flags
]
_LIST_TARGETS_SPEC = [
_INITIALIZERS,
_INIT_SCRIPTS,
]
Then one generic parse function drives everything:
def _parse_args(*, args_string: str, spec: list[_FlagSpec]) -> dict[str, Any]:
"""Generic table-driven parser used by all commands."""
parts = args_string.split()
# build flag→spec lookup, init result dict, walk parts once
...
def parse_run_arguments(*, args_string: str) -> dict[str, Any]:
return _parse_args(args_string=args_string, spec=_RUN_SPEC)
def parse_list_targets_arguments(*, args_string: str) -> dict[str, Any]:
return _parse_args(args_string=args_string, spec=_LIST_TARGETS_SPEC)
rlundeen2
reviewed
Apr 8, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR makes minor updates to improve the CLI experience, including the following:
list-targetslist_targetslist-scenarios)help commanddid not work properly for hyphenated commands such aslist-targetsorlist-initializersinitializer_namesinlist_targets_asyncpyrit_shell, whendo_runanddo_list_targets(with args) create a per-commandFrontendCore, they didn't propagate the shell's startup config (config_file, database, env_files). The new context fell back to the default ~/.pyrit/.pyrit_conf, silently losing any custom config passed via --config-file. This caused per-command contexts to use the wrong database type, env files, operator/operation labels, etc. The fix implemented here is to use awith_overrides()method inFrontendCoreto create a derived instance without re-loading and reading config files (usingobject.__new()__and not__init__), copy over inherited state (which includes _database, _env_files, _operator, _operation, _config) as the base, applies overrides on the per-command fields (initializer_names,initialization_scripts,log_level), and shares the parent's registries. Bothdo_runanddo_list_targetsnow use this method. Also sets_silent_reinit = Trueon the derived context to suppress redundant initialization output.._configattribute inFrontEndCoresince it is dead state and not referenced anywhere beyond being set.Tests and Documentation
Unit tests added to ensure behavior of all of the above changes