Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(search): name an unknown flag instead of dumping usage (#2458)

Term566cbe0d541e61
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bb692b8fc..158232abb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -112,6 +112,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- `xerj search` names an unknown flag on one line and exits 2 instead of
+ following "unexpected argument" with the whole usage text (#2458). An
+ agent that guessed `--index` and `--max-hits` read the help dump as the
+ answer and gave up on the CLI. Those guesses, and `--size`, `--limit`,
+ `--top`, `--top-k`, `-n` and `-i`, now point at `--prefix <P>` or
+ `-k <N>`.
+
- `xerj code` shows the part of a long definition that matched the query,
not its first `--full` characters (#2464). The matching text could sit
thousands of characters below the signature, so a hit named the right
diff --git a/engine/crates/xerj-autoindex/src/search.rs b/engine/crates/xerj-autoindex/src/search.rs
index c707ba946..f5464f42b 100644
--- a/engine/crates/xerj-autoindex/src/search.rs
+++ b/engine/crates/xerj-autoindex/src/search.rs
@@ -93,6 +93,23 @@ pub(crate) fn search_mode_refusal(asked: &str) -> String {
)
}
+/// #2458: an unknown flag got "unexpected argument" followed by the whole
+/// USAGE, and an agent read the help dump as the answer and gave up. Name the
+/// flag on one line, and point the names agents guess first at the real one.
+pub(crate) fn unknown_flag_refusal(flag: &str) -> String {
+ let name = flag.split_once('=').map_or(flag, |(name, _)| name);
+ let hint = match name {
+ "--index" | "--indices" | "-i" => {
+ " Use --prefix <P> to choose the indices: it searches <P>-* (default \"ax\")."
+ }
+ "--max-hits" | "--size" | "--limit" | "--top" | "--top-k" | "-n" => {
+ " Use -k <N> for the number of passages (default 5)."
+ }
+ _ => "",
+ };
+ format!("xerj search: unknown flag '{name}'.{hint} Run `xerj search --help` for every option.")
+}
+
/// One line for the human printer when the node says this lexical query
/// scored a `semantic_text` field and did not consult the embedding.
///
@@ -787,6 +804,10 @@ fn parse_search_cli(
return Ok(SearchCli::Refuse);
}
other if !other.starts_with('-') && query.is_none() => query = Some(other.to_string()),
+ other if other.starts_with('-') => {
+ eprintln!("{}", unknown_flag_refusal(other));
+ return Ok(SearchCli::Refuse);
+ }
other => return Err(format!("unexpected argument '{other}'")),
}
}
@@ -1236,6 +1257,36 @@ mod tests {
}
}
+ /// #2458: a guessed flag is refused by name, with the real flag when the
+ /// guess is a common one, not answered with the USAGE dump.
+ #[test]
+ fn an_unknown_flag_is_named_not_answered_with_usage() {
+ for args in [
+ vec!["--index", "docs", "needle"],
+ vec!["needle", "--max-hits", "5"],
+ vec!["--max-hits=5", "needle"],
+ vec!["--bogus", "needle"],
+ ] {
+ let parsed = parse_search_cli(words(&args).into_iter(), String::new(), None)
+ .unwrap_or_else(|e| panic!("{args:?} took the USAGE path: {e}"));
+ assert!(matches!(parsed, SearchCli::Refuse), "{args:?}: {parsed:?}");
+ }
+ let index = unknown_flag_refusal("--index");
+ assert!(index.contains("unknown flag '--index'"), "{index}");
+ assert!(index.contains("--prefix <P>"), "{index}");
+ let hits = unknown_flag_refusal("--max-hits=5");
+ assert!(hits.contains("unknown flag '--max-hits'"), "{hits}");
+ assert!(hits.contains("-k <N>"), "{hits}");
+ let other = unknown_flag_refusal("--bogus");
+ assert!(other.contains("unknown flag '--bogus'"), "{other}");
+ assert!(other.contains("xerj search --help"), "{other}");
+ assert!(!other.contains("USAGE"), "{other}");
+ // A second positional is still a usage error, not a flag refusal.
+ let err = parse_search_cli(words(&["one", "two"]).into_iter(), String::new(), None)
+ .expect_err("two queries");
+ assert!(err.contains("unexpected argument 'two'"), "{err}");
+ }
+
#[test]
fn a_non_numeric_k_is_a_usage_error() {
let err = parse_search_cli(