extern crate scp; use scp::{Command, CommandLine, ExecResult, ParseError}; use scp::{OptionAccessor, ParamAccessor}; fn main() { let mut command_line = CommandLine::new(); let greet_command = Command::new(vec!["greet"]).set_syntax_format("s-(f, from)s(w, welcome)"); let buy_command = Command::new(vec!["buy"]).set_syntax_format("s["); command_line.register(greet_command).register(buy_command); match command_line.run("greet \"John Doe\" -f Mrs.\\ Doe -w") { ExecResult::Err(e) => handle_error(e), ExecResult::Ok { command, subcommand: _, parameters, options, } => { let mut params = parameters.iter(); match command { "greet" => { print!("Hello, {}!", params.poll()); if let Some(o) = options.by_long_flag("from") { print!(" From {}", o.parameter()) } println!(); if options.by_long_flag("welcome").is_some() { println!("Welcome to Rust"); } } "buy" => { let what_to_buy = params.poll_multi_str(); print!("Buying "); for (idx, product) in what_to_buy.iter().enumerate() { print!("{}", product); if idx == what_to_buy.len() - 2 { print!(" and "); } else if idx != what_to_buy.len() - 1 { print!(", "); } } println!(" now"); } _ => {} } } } } 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), } }