diff --git a/explorer/src/main.rs b/explorer/src/main.rs index c37e6a4..bf688d1 100644 --- a/explorer/src/main.rs +++ b/explorer/src/main.rs @@ -63,6 +63,17 @@ fn command_line() -> Command { .arg(Arg::new("square").required(true)) .about("Show moves of a piece on a square"), ) + .subcommand( + Command::new("reset") + .subcommand(Command::new("clear").about("Reset to a cleared board")) + .subcommand(Command::new("starting").about("Reset to the starting position")) + .subcommand( + Command::new("fen") + .arg(Arg::new("fen").required(true)) + .about("Reset to a position described by a FEN string"), + ) + .about("Reset the board"), + ) .subcommand(Command::new("print").about("Print the board")) .subcommand(Command::new("quit").alias("exit").about("Quit the program")) .subcommand(Command::new("starting").about("Reset the board to the starting position")) @@ -171,6 +182,7 @@ fn respond(line: &str, state: &mut State) -> Result { let starting_position = Position::starting(); state.position = starting_position; } + Some(("reset", matches)) => do_reset_command(state, matches)?, Some((name, _matches)) => unimplemented!("{name}"), None => unreachable!("Subcommand required"), } @@ -178,6 +190,21 @@ fn respond(line: &str, state: &mut State) -> Result { Ok(result) } +fn do_reset_command(state: &mut State, matches: &clap::ArgMatches) -> Result<(), String> { + match matches.subcommand() { + None | Some(("clear", _)) => state.position = Position::empty(), + Some(("starting", _)) => state.position = Position::starting(), + Some(("fen", matches)) => { + let fen = matches.get_one::("fen").ok_or("Missing FEN")?; + let board = Board::from_fen_str(fen).map_err(|err| format!("{err}"))?; + state.position = Position::new(board); + } + Some((name, _matches)) => unimplemented!("{name}"), + } + + Ok(()) +} + fn main() -> Result<(), String> { let mut editor = DefaultEditor::new().map_err(|err| format!("Error: {err}"))?;