make help detection slightly more readable

This commit is contained in:
Schrottkatze 2025-06-18 15:41:06 +02:00
parent 3157af2c2b
commit 4b71994b6b
Signed by: schrottkatze
SSH key fingerprint: SHA256:FPOYVeBy3QP20FEM42uWF1Wa/Qhlk+L3S2+Wuau/Auo

View file

@ -3,13 +3,18 @@ use clap::{Parser, Subcommand};
use crate::game::debug::DebugEvent;
pub(super) fn respond(line: &str) -> Result<DebugEvent, String> {
let might_be_help_call = line.contains("help") || line.contains("-h");
let args = shlex::split(line).ok_or("Invalid Quoting")?;
let cli = Cli::try_parse_from(args).map_err(|e| e.to_string());
if might_be_help_call && cli.as_ref().is_err_and(|item| item.starts_with("Usage: ")) {
let Err(s) = cli else { unreachable!() };
return Ok(DebugEvent::PrintToConsole(s));
// help detection
let command_contains_help = (line.contains("help") || line.contains("-h"));
let some_line_starts_with_usage = cli
.as_ref()
.is_err_and(|item| item.lines().any(|line| line.starts_with("Usage: ")));
if command_contains_help && some_line_starts_with_usage {
let Err(text) = cli else { unreachable!() };
return Ok(DebugEvent::PrintToConsole { text });
}
Ok(cli?.cmd)