From 7d70f7e1a6b4f1c2e532719198c6cd3d003aa622 Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Thu, 21 Jul 2022 16:12:07 +0000 Subject: [PATCH] release --- CHANGELOG.md | 4 ++++ Cargo.toml | 2 +- README.md | 2 +- docs/api/cliparser/index.html | 2 +- docs/api/src/cliparser/parser.rs.html | 20 ++++++++++++++++++-- src/parser.rs | 12 ++++++++++-- src/parser_test.rs | 23 +++++++++++++++++++++++ 7 files changed, 58 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7acfcc4..1f72bd8 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## CHANGELOG +### v0.1.1 (2022-07-21) + +* Ignore extensions while scanning for commands + ### v0.1.0 (2022-07-21) * Initial release diff --git a/Cargo.toml b/Cargo.toml index bf74837..fcfe81d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cliparser" -version = "0.1.0" +version = "0.1.1" authors = ["Sagie Gur-Ari "] description = "Simple command line parser." license = "Apache-2.0" diff --git a/README.md b/README.md index b0cb620..d5906f0 100755 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ In order to use this library, just add it as a dependency: ```ini [dependencies] -cliparser = "^0.1.0" +cliparser = "^0.1.1" ``` ## API Documentation diff --git a/docs/api/cliparser/index.html b/docs/api/cliparser/index.html index f3e3c4b..50fb3df 100644 --- a/docs/api/cliparser/index.html +++ b/docs/api/cliparser/index.html @@ -3,7 +3,7 @@

Expand description

cliparser

diff --git a/docs/api/src/cliparser/parser.rs.html b/docs/api/src/cliparser/parser.rs.html index 0b21c0f..db2643c 100644 --- a/docs/api/src/cliparser/parser.rs.html +++ b/docs/api/src/cliparser/parser.rs.html @@ -415,6 +415,14 @@ 408 409 410 +411 +412 +413 +414 +415 +416 +417 +418
//! # parser
 //!
 //! Parsers arguments line based on given spec and returned parsed data.
@@ -428,7 +436,7 @@
     Argument, ArgumentOccurrence, ArgumentValueType, CliParsed, CliSpec, Command, ParserError,
 };
 use std::env;
-use std::ffi::OsStr;
+use std::ffi::{OsStr, OsString};
 use std::path::Path;
 
 /// Parsers the given command line based on the given spec and returns the result.<br>
@@ -536,13 +544,21 @@
     }
 }
 
+fn get_filename_without_extension(command: &str) -> Option<OsString> {
+    let path_value = Path::new(command);
+    match path_value.file_stem() {
+        Some(filename) => Some(filename.to_os_string()),
+        None => None,
+    }
+}
+
 fn parse_command(command_line: &Vec<&str>, spec: &CliSpec) -> (bool, usize) {
     if spec.command.is_empty() {
         return (true, 0);
     }
 
     let root_command_with_path = command_line[0];
-    match Path::new(root_command_with_path).file_name() {
+    match get_filename_without_extension(root_command_with_path) {
         Some(root_command_file_name) => {
             for command in &spec.command {
                 match command {
diff --git a/src/parser.rs b/src/parser.rs
index d1b7146..a5b93ab 100755
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -11,7 +11,7 @@ use crate::types::{
     Argument, ArgumentOccurrence, ArgumentValueType, CliParsed, CliSpec, Command, ParserError,
 };
 use std::env;
-use std::ffi::OsStr;
+use std::ffi::{OsStr, OsString};
 use std::path::Path;
 
 /// Parsers the given command line based on the given spec and returns the result.
@@ -119,13 +119,21 @@ fn insert_default_values(spec: &CliSpec, cli_parsed: &mut CliParsed) { } } +fn get_filename_without_extension(command: &str) -> Option { + let path_value = Path::new(command); + match path_value.file_stem() { + Some(filename) => Some(filename.to_os_string()), + None => None, + } +} + fn parse_command(command_line: &Vec<&str>, spec: &CliSpec) -> (bool, usize) { if spec.command.is_empty() { return (true, 0); } let root_command_with_path = command_line[0]; - match Path::new(root_command_with_path).file_name() { + match get_filename_without_extension(root_command_with_path) { Some(root_command_file_name) => { for command in &spec.command { match command { diff --git a/src/parser_test.rs b/src/parser_test.rs index aef6c21..364d61e 100755 --- a/src/parser_test.rs +++ b/src/parser_test.rs @@ -461,6 +461,16 @@ fn parse_command_match_command_ignore_path() { assert_eq!(index, 1); } +#[test] +fn parse_command_match_command_ignore_extension() { + let mut cli_spec = CliSpec::new(); + cli_spec.command.push(Command::Command("test".to_string())); + let (valid, index) = parse_command(&vec!["./bin/dir/test.exe"], &cli_spec); + + assert!(valid); + assert_eq!(index, 1); +} + #[test] fn parse_command_no_match_command() { let mut cli_spec = CliSpec::new(); @@ -499,6 +509,19 @@ fn parse_command_match_subcommand_ignore_path() { assert_eq!(index, 2); } +#[test] +fn parse_command_match_subcommand_ignore_extension() { + let mut cli_spec = CliSpec::new(); + cli_spec.command.push(Command::SubCommand(vec![ + "cargo".to_string(), + "test".to_string(), + ])); + let (valid, index) = parse_command(&vec!["./bin/dir/cargo.exe", "test"], &cli_spec); + + assert!(valid); + assert_eq!(index, 2); +} + #[test] fn parse_command_no_match_subcommand_root() { let mut cli_spec = CliSpec::new();