From fe9728e379b7945422e94b1a5d0ffafe4fe98429 Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 12 Sep 2020 17:44:42 +0800 Subject: [PATCH] fix no subcommand panic --- src/subcommand.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/subcommand.rs b/src/subcommand.rs index 28f7fb3..3d7264c 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -108,7 +108,6 @@ impl SubCommand { args: &'b [String], ) -> (String, Vec, Option) { let is_dashed = self.name.starts_with('-'); // SubCommand of pacman is dashed - let name; let mut options: Vec = vec![]; let mut operands: Vec = vec![]; for arg in args.iter() { @@ -124,11 +123,22 @@ impl SubCommand { operands.push(arg.to_string()); } } - if is_dashed { - name = options.remove(0); - } else { - name = operands.remove(0); - } + let name = match is_dashed { + true => { + if options.len() == 0 { + String::new() + } else { + options.remove(0) + } + }, + false => { + if operands.len() == 0 { + String::new() + } else { + operands.remove(0) + } + } + }; let pkg = if operands.is_empty() { None } else {