Files
sute/src/commands.rs
T

70 lines
1.8 KiB
Rust
Raw Normal View History

2020-11-03 20:17:06 +01:00
use clap::{
App,
AppSettings,
2020-11-04 15:41:49 +01:00
Arg,
2020-11-03 20:17:06 +01:00
ArgMatches,
Error,
};
2020-11-04 13:07:31 +01:00
pub enum Commands {
2020-11-03 20:17:06 +01:00
}
2020-11-04 13:07:31 +01:00
pub struct CommandParser<'help> {
2020-11-03 20:17:06 +01:00
app: App<'help>,
}
impl<'help> CommandParser<'help> {
pub fn new() -> Self {
Self {
app: App::new("Commands")
.setting(AppSettings::DisableVersion)
.setting(AppSettings::StrictUtf8)
.setting(AppSettings::ColorAlways)
.setting(AppSettings::NoBinaryName)
2020-11-04 13:07:31 +01:00
.subcommand(App::new("quit"))
.subcommand(App::new("connect"))
.subcommand(App::new("authenticate")
2020-11-04 15:41:49 +01:00
.arg(Arg::new("user")
.takes_value(true))
.arg(Arg::new("pass")
2020-11-20 14:03:16 +01:00
.takes_value(true)))
.subcommand(App::new("minfo")
2020-12-16 11:59:40 +01:00
.arg(Arg::new("uid")
2020-11-20 14:03:16 +01:00
.takes_value(true)))
2021-02-08 18:39:26 +00:00
.subcommand(App::new("list"))
2021-02-09 17:41:32 +00:00
.subcommand(App::new("use")
.arg(Arg::new("uid")
.takes_value(true)))
2020-11-04 13:07:31 +01:00
,
2020-11-03 20:17:06 +01:00
}
}
pub fn parse<I: IntoIterator<Item = String>>(&mut self, iter: I) -> Result<ArgMatches, Error> {
2020-11-04 15:41:49 +01:00
self.app.clone().try_get_matches_from(iter)
2020-11-03 20:17:06 +01:00
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_commands_test() {
let mut cmds = CommandParser::new();
let p = vec!["connect".to_string()];
let matches = cmds.parse(p).unwrap();
assert_eq!(matches.subcommand_name(), Some("connect"));
}
#[test]
fn fail_parsing_test() {
let mut cmds = CommandParser::new();
let p = vec!["invalid".to_string()];
let matches = cmds.parse(p);
assert!(matches.is_err())
}
}