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.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
fix(cli): refuse a missing or non-numeric flag value in `xerj` and `xerj index` (#1435)
xerj-org7cac2ff93b7b
diff --git a/engine/crates/xerj-server/src/main.rs b/engine/crates/xerj-server/src/main.rsindex bcf72ca6f..8552bece2 100644--- a/engine/crates/xerj-server/src/main.rs+++ b/engine/crates/xerj-server/src/main.rs@@ -121,14 +121,30 @@ struct CliArgs {}fn parse_args() -> CliArgs {- parse_args_from(std::env::args().skip(1))+ parse_args_from(std::env::args().skip(1)).unwrap_or_else(|e| {+ eprintln!("error: {e}. Use --help for usage.");+ std::process::exit(2);+ })+}++/// The value that must follow `flag`. A flag given last on the line, or+/// followed straight by another `--flag`, has no value: that is an error, not+/// a silent "unset" — `xerj --insecure --data-dir` used to boot on the default+/// data directory, and `xerj --data-dir --insecure` used a directory literally+/// named `--insecure` and left auth on (#1435).+fn flag_value(flag: &str, next: Option<String>) -> Result<String, String> {+ match next {+ Some(v) if !v.starts_with("--") => Ok(v),+ Some(v) => Err(format!("{flag} needs a value, got the flag {v}")),+ None => Err(format!("{flag} needs a value")),+ }}/// The body of [`parse_args`], over any iterator of arguments — the same/// testable-parse shape as `brain::parse` and `share::parse`, so the CLI/// grammar is unit-tested without touching process-wide `argv` (which every/// test in the same binary shares).-fn parse_args_from<I>(args: I) -> CliArgs+fn parse_args_from<I>(args: I) -> Result<CliArgs, String>whereI: Iterator<Item = String>,{@@ -149,29 +165,29 @@ wherewhile let Some(arg) = args.next() {match arg.as_str() {"--config" | "-c" => {- config = args.next().map(PathBuf::from);+ config = Some(PathBuf::from(flag_value(&arg, args.next())?));}"--data-dir" | "-d" => {- data_dir = args.next();+ data_dir = Some(flag_value(&arg, args.next())?);}"--bind" | "-b" => {- bind = args.next();+ bind = Some(flag_value(&arg, args.next())?);}"--port" => {- port = args.next();+ port = Some(flag_value(&arg, args.next())?);}"--insecure" | "-k" => {insecure = true;}"--embed-mode" => {- embed_mode = args.next();+ embed_mode = Some(flag_value(&arg, args.next())?);}- "--onnx-model" => onnx_model = args.next(),- "--onnx-tokenizer" => onnx_tokenizer = args.next(),- "--compat-distribution" => compat_distribution = args.next(),- "--compat-version" => compat_version = args.next(),- "--decide-mode" => decide_mode = args.next(),- "--decide-model-dir" => decide_model_dir = args.next(),+ "--onnx-model" => onnx_model = Some(flag_value(&arg, args.next())?),+ "--onnx-tokenizer" => onnx_tokenizer = Some(flag_value(&arg, args.next())?),+ "--compat-distribution" => compat_distribution = Some(flag_value(&arg, args.next())?),+ "--compat-version" => compat_version = Some(flag_value(&arg, args.next())?),+ "--decide-mode" => decide_mode = Some(flag_value(&arg, args.next())?),+ "--decide-model-dir" => decide_model_dir = Some(flag_value(&arg, args.next())?),// Read out of band by `xerj_common::feedback` (which scans the// whole argument list, so its position never matters); accepted// here only so it is not an "unknown argument".@@ -191,7 +207,7 @@ where}}- CliArgs {+ Ok(CliArgs {config,data_dir,bind,@@ -204,7 +220,7 @@ wherecompat_version,decide_mode,decide_model_dir,- }+ })}fn print_help() {@@ -1236,6 +1252,29 @@ struct IndexCmdArgs {data_dir: Option<String>,}+/// `xerj index`'s string flag value; exits 2 on a missing one (see [`flag_value`]).+fn index_flag_value(flag: &str, next: Option<String>) -> String {+ flag_value(flag, next).unwrap_or_else(|e| {+ eprintln!("error: {e}. Use --help for usage.");+ std::process::exit(2);+ })+}++/// `xerj index`'s numeric flag value. `--batch abc` used to fall back to the+/// default without a word, so a typo silently changed the run (#1435).+fn index_flag_count(flag: &str, next: Option<String>) -> usize {+ parse_count(flag, next).unwrap_or_else(|e| {+ eprintln!("error: {e}. Use --help for usage.");+ std::process::exit(2);+ })+}++fn parse_count(flag: &str, next: Option<String>) -> Result<usize, String> {+ let v = flag_value(flag, next)?;+ v.parse()+ .map_err(|_| format!("{flag} must be a non-negative whole number, got {v:?}"))+}+fn parse_index_args() -> IndexCmdArgs {// Skip argv[0] (binary) + argv[1] ("index").let mut args = std::env::args().skip(2);@@ -1249,19 +1288,13 @@ fn parse_index_args() -> IndexCmdArgs {while let Some(arg) = args.next() {match arg.as_str() {- "--index" | "-i" => index = args.next(),- "--file" | "-f" => file = args.next().map(PathBuf::from),- "--batch" | "-b" => {- batch = args.next().and_then(|s| s.parse().ok()).unwrap_or(5000);- }- "--workers" | "-w" => {- workers = args.next().and_then(|s| s.parse().ok()).unwrap_or(0);- }- "--limit" | "-l" => {- limit = args.next().and_then(|s| s.parse().ok()).unwrap_or(0);- }- "--config" | "-c" => config = args.next().map(PathBuf::from),- "--data-dir" | "-d" => data_dir = args.next(),+ "--index" | "-i" => index = Some(index_flag_value(&arg, args.next())),+ "--file" | "-f" => file = Some(PathBuf::from(index_flag_value(&arg, args.next()))),+ "--batch" | "-b" => batch = index_flag_count(&arg, args.next()),+ "--workers" | "-w" => workers = index_flag_count(&arg, args.next()),+ "--limit" | "-l" => limit = index_flag_count(&arg, args.next()),+ "--config" | "-c" => config = Some(PathBuf::from(index_flag_value(&arg, args.next()))),+ "--data-dir" | "-d" => data_dir = Some(index_flag_value(&arg, args.next())),xerj_common::feedback::DISABLE_FLAG => {}"--help" | "-h" => {print_index_help();@@ -2007,7 +2040,8 @@ mod decide_flag_tests {"local","--decide-model-dir","/models/xerj-decide",- ]));+ ]))+ .unwrap();assert_eq!(args.decide_mode.as_deref(), Some("local"));assert_eq!(args.decide_model_dir.as_deref(),@@ -2015,9 +2049,46 @@ mod decide_flag_tests {);}+ #[test]+ fn a_value_flag_without_its_value_is_an_error_not_unset() {+ // #1435: given last, or followed by another flag, the value is+ // missing — this used to boot on the default silently.+ for line in [+ &["--insecure", "--data-dir"][..],+ &["--port"][..],+ &["-c"][..],+ &["--data-dir", "--insecure"][..],+ &["--bind", "--port", "9300"][..],+ &["--decide-model-dir"][..],+ ] {+ let err = parse_args_from(argv(line)).err();+ assert!(+ err.as_deref().is_some_and(|e| e.contains("needs a value")),+ "{line:?} must be refused, got {err:?}"+ );+ }+ // A value that merely contains or starts with a single dash is fine.+ let args = parse_args_from(argv(&["-d", "-data", "--bind", "::1"])).unwrap();+ assert_eq!(args.data_dir.as_deref(), Some("-data"));+ assert_eq!(args.bind.as_deref(), Some("::1"));+ }++ #[test]+ fn index_counts_refuse_a_non_number_instead_of_defaulting() {+ use super::parse_count;+ assert_eq!(parse_count("--batch", Some("200".into())), Ok(200));+ assert!(parse_count("--batch", Some("abc".into()))+ .unwrap_err()+ .contains("whole number"));+ assert!(parse_count("--workers", Some("-1".into())).is_err());+ assert!(parse_count("--limit", None)+ .unwrap_err()+ .contains("needs a value"));+ }+#[test]fn decide_flags_default_to_none() {- let args = parse_args_from(argv(&[]));+ let args = parse_args_from(argv(&[])).unwrap();assert_eq!(args.decide_mode, None);assert_eq!(args.decide_model_dir, None);}@@ -2038,7 +2109,8 @@ mod decide_flag_tests {"opensearch","-d","./data",- ]));+ ]))+ .unwrap();assert!(args.insecure);assert_eq!(args.embed_mode.as_deref(), Some("neural"));assert_eq!(args.compat_distribution.as_deref(), Some("opensearch"));