Skip to content

Commit

Permalink
release
Browse files Browse the repository at this point in the history
  • Loading branch information
sagiegurari committed Jul 21, 2022
1 parent 2366a22 commit 7d70f7e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cliparser"
version = "0.1.0"
version = "0.1.1"
authors = ["Sagie Gur-Ari <[email protected]>"]
description = "Simple command line parser."
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/api/cliparser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</a><h2 class="location"></h2>
</nav>
<nav class="sidebar"><a class="sidebar-logo" href="../cliparser/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div>
</a><h2 class="location"><a href="#">Crate cliparser</a></h2><div class="sidebar-elems"><div class="block"><ul><li class="version">Version 0.1.0</li><li><a id="all-types" href="all.html">All Items</a></li></ul></div><section><div class="block"><ul><li><a href="#modules">Modules</a></li><li><a href="#functions">Functions</a></li></ul></div></section><div id="sidebar-vars" data-name="cliparser" data-ty="mod" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../cliparser/index.html"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1">
</a><h2 class="location"><a href="#">Crate cliparser</a></h2><div class="sidebar-elems"><div class="block"><ul><li class="version">Version 0.1.1</li><li><a id="all-types" href="all.html">All Items</a></li></ul></div><section><div class="block"><ul><li><a href="#modules">Modules</a></li><li><a href="#functions">Functions</a></li></ul></div></section><div id="sidebar-vars" data-name="cliparser" data-ty="mod" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../cliparser/index.html"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1">
<a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div>
</div></form></nav></div><section id="main-content" class="content"><div class="main-heading">
<h1 class="fqn"><span class="in-band">Crate <a class="mod" href="#">cliparser</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../src/cliparser/lib.rs.html#1-563">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><h2 id="cliparser"><a href="#cliparser">cliparser</a></h2>
Expand Down
20 changes: 18 additions & 2 deletions docs/api/src/cliparser/parser.rs.html
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,14 @@
<span id="408">408</span>
<span id="409">409</span>
<span id="410">410</span>
<span id="411">411</span>
<span id="412">412</span>
<span id="413">413</span>
<span id="414">414</span>
<span id="415">415</span>
<span id="416">416</span>
<span id="417">417</span>
<span id="418">418</span>
</pre><pre class="rust"><code><span class="doccomment">//! # parser</span>
<span class="doccomment">//!</span>
<span class="doccomment">//! Parsers arguments line based on given spec and returned parsed data.</span>
Expand All @@ -428,7 +436,7 @@
<span class="ident">Argument</span>, <span class="ident">ArgumentOccurrence</span>, <span class="ident">ArgumentValueType</span>, <span class="ident">CliParsed</span>, <span class="ident">CliSpec</span>, <span class="ident">Command</span>, <span class="ident">ParserError</span>,
};
<span class="kw">use</span> <span class="ident">std::env</span>;
<span class="kw">use</span> <span class="ident">std::ffi::OsStr</span>;
<span class="kw">use</span> <span class="ident">std::ffi</span>::{<span class="ident">OsStr</span>, <span class="ident">OsString</span>};
<span class="kw">use</span> <span class="ident">std::path::Path</span>;

<span class="doccomment">/// Parsers the given command line based on the given spec and returns the result.&lt;br&gt;</span>
Expand Down Expand Up @@ -536,13 +544,21 @@
}
}

<span class="kw">fn</span> <span class="ident">get_filename_without_extension</span>(<span class="ident">command</span>: <span class="kw-2">&amp;</span><span class="ident">str</span>) -&gt; <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">OsString</span><span class="op">&gt;</span> {
<span class="kw">let</span> <span class="ident">path_value</span> <span class="op">=</span> <span class="ident">Path::new</span>(<span class="ident">command</span>);
<span class="kw">match</span> <span class="ident">path_value</span>.<span class="ident">file_stem</span>() {
<span class="prelude-val">Some</span>(<span class="ident">filename</span>) =&gt; <span class="prelude-val">Some</span>(<span class="ident">filename</span>.<span class="ident">to_os_string</span>()),
<span class="prelude-val">None</span> =&gt; <span class="prelude-val">None</span>,
}
}

<span class="kw">fn</span> <span class="ident">parse_command</span>(<span class="ident">command_line</span>: <span class="kw-2">&amp;</span><span class="ident">Vec</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="ident">str</span><span class="op">&gt;</span>, <span class="ident">spec</span>: <span class="kw-2">&amp;</span><span class="ident">CliSpec</span>) -&gt; (<span class="ident">bool</span>, <span class="ident">usize</span>) {
<span class="kw">if</span> <span class="ident">spec</span>.<span class="ident">command</span>.<span class="ident">is_empty</span>() {
<span class="kw">return</span> (<span class="bool-val">true</span>, <span class="number">0</span>);
}

<span class="kw">let</span> <span class="ident">root_command_with_path</span> <span class="op">=</span> <span class="ident">command_line</span>[<span class="number">0</span>];
<span class="kw">match</span> <span class="ident">Path::new</span>(<span class="ident">root_command_with_path</span>).<span class="ident">file_name</span>() {
<span class="kw">match</span> <span class="ident">get_filename_without_extension</span>(<span class="ident">root_command_with_path</span>) {
<span class="prelude-val">Some</span>(<span class="ident">root_command_file_name</span>) =&gt; {
<span class="kw">for</span> <span class="ident">command</span> <span class="kw">in</span> <span class="kw-2">&amp;</span><span class="ident">spec</span>.<span class="ident">command</span> {
<span class="kw">match</span> <span class="ident">command</span> {
Expand Down
12 changes: 10 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>
Expand Down Expand Up @@ -119,13 +119,21 @@ fn insert_default_values(spec: &CliSpec, cli_parsed: &mut CliParsed) {
}
}

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 {
Expand Down
23 changes: 23 additions & 0 deletions src/parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 7d70f7e

Please sign in to comment.