extern crate scp; use scp::{Command, CommandLine, ExecResult, OptionAccessor, ParseError}; fn main() { let mut command_line = CommandLine::new(); let spaceport_command = Command::new(vec!["spaceport"]).add_subcommand( Command::new(vec!["ships", "sh"]) .add_subcommand(Command::new(vec!["destroy", "des"])) .add_subcommand(Command::new(vec!["alert", "a"])), ); command_line.register(spaceport_command); match command_line.run("spaceport sh a") { ExecResult::Err(e) => handle_error(e), ExecResult::Ok { command, subcommand, parameters: _, options, } => { if command == "spaceport" { match subcommand { "ships.destroy" => { if options.by_short_flag('n').is_some() { println!("Near ships have been destroyed :("); } else { println!("All ships have been destroyed :("); } } "ships.alert" => { println!("Ships alerted"); } _ => {} } } } } } fn handle_error(error: ParseError) { match error { ParseError::InvalidSyntax(e) => eprintln!("{}", e), ParseError::UnknownCommand => eprintln!("Unknown command"), ParseError::MissingSubcommand => eprintln!("Missing subcommand"), ParseError::MissingParameter(kind) => { eprintln!("Missing parameter of type {}", kind.to_string()) } ParseError::InvalidParameter(s) => eprintln!("Invalid parameter: {}", s), ParseError::UnnecessaryFlag(s) => eprintln!("Unknown flag: {}", s), ParseError::UnnecessaryParameter(s) => eprintln!("Unnecessary parameter: {}", s), } }