style: Make clippy happy

This commit is contained in:
Ed Page 2022-04-15 13:48:58 -05:00
parent 2f0d91688a
commit 15d8b3b0cd
6 changed files with 48 additions and 44 deletions

View file

@ -52,7 +52,7 @@
//! verbosity: 0,
//! };
//!
//! let raw = clap_lex::RawArgs::from_iter(raw);
//! let raw = clap_lex::RawArgs::new(raw);
//! let mut cursor = raw.cursor();
//! raw.next(&mut cursor); // Skip the bin
//! while let Some(arg) = raw.next(&mut cursor) {
@ -124,6 +124,8 @@ pub struct RawArgs {
impl RawArgs {
//// Create an argument list to parse
///
/// **NOTE:** The argument returned will be the current binary.
///
/// # Example
///
/// ```rust,no_run
@ -136,7 +138,7 @@ impl RawArgs {
/// println!("{:?}", paths);
/// ```
pub fn from_args() -> Self {
Self::from_iter(std::env::args_os())
Self::new(std::env::args_os())
}
//// Create an argument list to parse
@ -145,14 +147,14 @@ impl RawArgs {
///
/// ```rust,no_run
/// # use std::path::PathBuf;
/// let raw = clap_lex::RawArgs::from_iter(["bin", "foo.txt"]);
/// let raw = clap_lex::RawArgs::new(["bin", "foo.txt"]);
/// let mut cursor = raw.cursor();
/// let _bin = raw.next_os(&mut cursor);
///
/// let mut paths = raw.remaining(&mut cursor).map(PathBuf::from).collect::<Vec<_>>();
/// println!("{:?}", paths);
/// ```
pub fn from_iter(iter: impl IntoIterator<Item = impl Into<std::ffi::OsString>>) -> Self {
pub fn new(iter: impl IntoIterator<Item = impl Into<std::ffi::OsString>>) -> Self {
let iter = iter.into_iter();
Self::from(iter)
}
@ -163,7 +165,7 @@ impl RawArgs {
///
/// ```rust,no_run
/// # use std::path::PathBuf;
/// let raw = clap_lex::RawArgs::from_iter(["bin", "foo.txt"]);
/// let raw = clap_lex::RawArgs::new(["bin", "foo.txt"]);
/// let mut cursor = raw.cursor();
/// let _bin = raw.next_os(&mut cursor);
///
@ -202,7 +204,7 @@ impl RawArgs {
///
/// ```rust,no_run
/// # use std::path::PathBuf;
/// let raw = clap_lex::RawArgs::from_iter(["bin", "foo.txt"]);
/// let raw = clap_lex::RawArgs::new(["bin", "foo.txt"]);
/// let mut cursor = raw.cursor();
/// let _bin = raw.next_os(&mut cursor);
///
@ -299,7 +301,7 @@ impl<'s> ParsedArg<'s> {
pub fn to_long(&self) -> Option<(Result<&str, &RawOsStr>, Option<&RawOsStr>)> {
if let Some(raw) = self.utf8 {
let remainder = raw.strip_prefix("--")?;
let (flag, value) = if let Some((p0, p1)) = remainder.split_once("=") {
let (flag, value) = if let Some((p0, p1)) = remainder.split_once('=') {
(p0, Some(p1))
} else {
(remainder, None)
@ -310,12 +312,12 @@ impl<'s> ParsedArg<'s> {
} else {
let raw = self.inner.as_ref();
let remainder = raw.strip_prefix("--")?;
let (flag, value) = if let Some((p0, p1)) = remainder.split_once("=") {
let (flag, value) = if let Some((p0, p1)) = remainder.split_once('=') {
(p0, Some(p1))
} else {
(remainder, None)
};
let flag = flag.to_str().ok_or_else(|| flag);
let flag = flag.to_str().ok_or(flag);
Some((flag, value))
}
}
@ -363,7 +365,7 @@ impl<'s> ParsedArg<'s> {
///
/// **NOTE:** May return a flag or an escape.
pub fn to_value(&self) -> Result<&str, &RawOsStr> {
self.utf8.ok_or(self.inner.as_ref())
self.utf8.ok_or_else(|| self.inner.as_ref())
}
/// Safely print an argument that may contain non-UTF8 content

View file

@ -1,11 +1,11 @@
#[test]
fn insert() {
let mut raw = clap_lex::RawArgs::from_iter(["bin", "a", "b", "c"]);
let mut raw = clap_lex::RawArgs::new(["bin", "a", "b", "c"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("a")));
raw.insert(&mut cursor, &["1", "2", "3"]);
raw.insert(&cursor, &["1", "2", "3"]);
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("1")));
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("2")));
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("3")));

View file

@ -2,7 +2,7 @@
// the start of a long because there is no valid value to return.
#[test]
fn to_long_stdio() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -14,7 +14,7 @@ fn to_long_stdio() {
#[test]
fn to_long_escape() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -28,7 +28,7 @@ fn to_long_escape() {
#[test]
fn to_long_no_value() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--long"]);
let raw = clap_lex::RawArgs::new(["bin", "--long"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -42,7 +42,7 @@ fn to_long_no_value() {
#[test]
fn to_long_with_empty_value() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--long="]);
let raw = clap_lex::RawArgs::new(["bin", "--long="]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -56,7 +56,7 @@ fn to_long_with_empty_value() {
#[test]
fn to_long_with_value() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--long=hello"]);
let raw = clap_lex::RawArgs::new(["bin", "--long=hello"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -70,7 +70,7 @@ fn to_long_with_value() {
#[test]
fn to_short_stdio() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -83,7 +83,7 @@ fn to_short_stdio() {
#[test]
fn to_short_escape() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -95,7 +95,7 @@ fn to_short_escape() {
#[test]
fn to_short_long() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--long"]);
let raw = clap_lex::RawArgs::new(["bin", "--long"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -107,7 +107,7 @@ fn to_short_long() {
#[test]
fn to_short() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -121,7 +121,7 @@ fn to_short() {
#[test]
fn is_negative_number() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-10.0"]);
let raw = clap_lex::RawArgs::new(["bin", "-10.0"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -131,7 +131,7 @@ fn is_negative_number() {
#[test]
fn is_positive_number() {
let raw = clap_lex::RawArgs::from_iter(["bin", "10.0"]);
let raw = clap_lex::RawArgs::new(["bin", "10.0"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -141,7 +141,7 @@ fn is_positive_number() {
#[test]
fn is_not_number() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--10.0"]);
let raw = clap_lex::RawArgs::new(["bin", "--10.0"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -151,7 +151,7 @@ fn is_not_number() {
#[test]
fn is_stdio() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -161,7 +161,7 @@ fn is_stdio() {
#[test]
fn is_not_stdio() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -171,7 +171,7 @@ fn is_not_stdio() {
#[test]
fn is_escape() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -181,7 +181,7 @@ fn is_escape() {
#[test]
fn is_not_escape() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();

View file

@ -1,6 +1,6 @@
#[test]
fn iter() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -12,7 +12,7 @@ fn iter() {
#[test]
fn next_flag() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -31,7 +31,7 @@ fn next_flag() {
#[test]
fn next_value_os() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -44,7 +44,7 @@ fn next_value_os() {
#[test]
fn next_flag_with_value() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -58,7 +58,7 @@ fn next_flag_with_value() {
#[test]
fn next_flag_with_no_value() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -75,7 +75,7 @@ fn next_flag_with_no_value() {
#[test]
fn advance_by_nothing() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -89,7 +89,7 @@ fn advance_by_nothing() {
#[test]
fn advance_by_nothing_with_nothing() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -103,7 +103,7 @@ fn advance_by_nothing_with_nothing() {
#[test]
fn advance_by_something() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -117,7 +117,7 @@ fn advance_by_something() {
#[test]
fn advance_by_out_of_bounds() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -131,7 +131,7 @@ fn advance_by_out_of_bounds() {
#[test]
fn is_empty() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -142,7 +142,7 @@ fn is_empty() {
#[test]
fn is_not_empty() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-hello"]);
let raw = clap_lex::RawArgs::new(["bin", "-hello"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -153,7 +153,7 @@ fn is_not_empty() {
#[test]
fn is_partial_not_empty() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-hello"]);
let raw = clap_lex::RawArgs::new(["bin", "-hello"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -165,7 +165,7 @@ fn is_partial_not_empty() {
#[test]
fn is_exhausted_empty() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -177,7 +177,7 @@ fn is_exhausted_empty() {
#[test]
fn is_number() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-1.0"]);
let raw = clap_lex::RawArgs::new(["bin", "-1.0"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
@ -188,7 +188,7 @@ fn is_number() {
#[test]
fn is_not_number() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-hello"]);
let raw = clap_lex::RawArgs::new(["bin", "-hello"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();

View file

@ -631,7 +631,7 @@ impl<'help> App<'help> {
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let mut raw_args = clap_lex::RawArgs::from(itr.into_iter());
let mut raw_args = clap_lex::RawArgs::new(itr.into_iter());
let mut cursor = raw_args.cursor();
#[cfg(feature = "unstable-multicall")]

View file

@ -651,6 +651,8 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
}
fn is_new_arg(&self, next: &clap_lex::ParsedArg<'_>, current_positional: &Arg) -> bool {
#![allow(clippy::needless_bool)] // Prefer consistent if/else-if ladder
debug!(
"Parser::is_new_arg: {:?}:{:?}",
next.to_value_os(),