Better command parser soon

This commit is contained in:
Gregor Reitzenstein 2020-11-03 20:17:06 +01:00
parent 0a68ea405c
commit 21550b411d
3 changed files with 61 additions and 8 deletions

View File

@ -20,7 +20,7 @@ uuid = "0.8"
tui = "0.11" tui = "0.11"
termion = "1.5" termion = "1.5"
clap = "2.33" clap = "3.0.0-beta.2"
smol = "1.2" smol = "1.2"

54
src/commands.rs Normal file
View File

@ -0,0 +1,54 @@
use clap::{
App,
AppSettings,
ArgMatches,
Error,
};
enum Commands {
}
struct CommandParser<'help> {
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)
.subcommand(App::new("connect")),
}
}
pub fn parse<I: IntoIterator<Item = String>>(&mut self, iter: I) -> Result<ArgMatches, Error> {
self.app.try_get_matches_from_mut(iter)
}
}
#[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())
}
}

View File

@ -38,17 +38,16 @@ fn main() -> Result<(), io::Error> {
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS")) .author(env!("CARGO_PKG_AUTHORS"))
.about(env!("CARGO_PKG_DESCRIPTION")) .about(env!("CARGO_PKG_DESCRIPTION"))
.usage("Press `?` in the GUI to see keybindings")
.before_help(BANNER) .before_help(BANNER)
.arg(Arg::with_name("config") .arg(Arg::new("config")
.short("c") .short('c')
.long("config") .long("config")
.help("Specify configuration file path") .about("Specify configuration file path")
.takes_value(true)) .takes_value(true))
.arg(Arg::with_name("server") .arg(Arg::new("server")
.short("s") .short('s')
.long("server") .long("server")
.help("Connect to the specified address[:port] as server") .about("Connect to the specified address[:port] as server")
.takes_value(true)) .takes_value(true))
.get_matches(); .get_matches();