Skip to content

Commit

Permalink
style: update to Rust 2018 edition and clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Nov 30, 2021
1 parent 1920b53 commit 2bb9180
Show file tree
Hide file tree
Showing 32 changed files with 374 additions and 356 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["command-line-interface"]
description = """
A simple to use, efficient, and full-featured Command Line Argument Parser
"""
edition = "2018"

[badges]
travis-ci = { repository = "clap-rs/clap" }
Expand Down Expand Up @@ -49,7 +50,6 @@ wrap_help = ["term_size", "textwrap/term_size"]
yaml = ["yaml-rust"]
unstable = [] # for building with unstable clap features (doesn't require nightly Rust) (currently none)
nightly = [] # for building with unstable Rust features (currently none)
lints = ["clippy"] # Requires nightly Rust
debug = [] # Enables debug messages
no_cargo = [] # Enable if you're not using Cargo, disables Cargo-env-var-dependent macros
doc = ["yaml"] # All the features which add to documentation
Expand Down
55 changes: 28 additions & 27 deletions src/app/help.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
// Std
use std::borrow::Cow;
use std::cmp;
use std::collections::BTreeMap;
use std::fmt::Display;
use std::io::{self, Cursor, Read, Write};
use std::usize;

// Internal
use app::parser::Parser;
use app::usage;
use app::{App, AppSettings};
use args::{AnyArg, ArgSettings, DispOrder};
use errors::{Error, Result as ClapResult};
use fmt::{Colorizer, ColorizerOption, Format};
use map::VecMap;
use INTERNAL_ERROR_MSG;
use std::{
borrow::Cow,
cmp,
collections::BTreeMap,
fmt::Display,
io::{self, Cursor, Read, Write},
usize,
};

// Third Party
#[cfg(feature = "wrap_help")]
use term_size;
#[cfg(feature = "wrap_help")]
use textwrap;
use unicode_width::UnicodeWidthStr;

// Internal
use crate::{
app::{parser::Parser, usage, App, AppSettings},
args::{AnyArg, ArgSettings, DispOrder},
errors::{Error, Result as ClapResult},
fmt::{Colorizer, ColorizerOption, Format},
map::VecMap,
INTERNAL_ERROR_MSG,
};

#[cfg(not(feature = "wrap_help"))]
mod term_size {
pub fn dimensions() -> Option<(usize, usize)> {
Expand All @@ -33,7 +36,7 @@ fn str_width(s: &str) -> usize {
UnicodeWidthStr::width(s)
}

const TAB: &'static str = " ";
const TAB: &str = " ";

// These are just convenient traits to make the code easier to read.
trait ArgWithDisplay<'b, 'c>: AnyArg<'b, 'c> + Display {}
Expand Down Expand Up @@ -96,7 +99,7 @@ pub struct Help<'a> {
// Public Functions
impl<'a> Help<'a> {
/// Create a new `Help` instance.
#[cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))]
#[cfg_attr(feature = "cargo-clippy", allow(clippy::too_many_arguments))]
pub fn new(
w: &'a mut Write,
next_line_help: bool,
Expand All @@ -110,8 +113,8 @@ impl<'a> Help<'a> {
debugln!("Help::new;");
Help {
writer: w,
next_line_help: next_line_help,
hide_pv: hide_pv,
next_line_help,
hide_pv,
term_w: match term_w {
Some(width) => {
if width == 0 {
Expand All @@ -128,11 +131,11 @@ impl<'a> Help<'a> {
},
),
},
color: color,
cizer: cizer,
color,
cizer,
longest: 0,
force_next_line: false,
use_long: use_long,
use_long,
}
}

Expand Down Expand Up @@ -492,7 +495,7 @@ impl<'a> Help<'a> {
write!(self.writer, "{}", part)?;
}
for part in help.lines().skip(1) {
write!(self.writer, "\n")?;
writeln!(self.writer)?;
if nlh || self.force_next_line {
write!(self.writer, "{}{}{}", TAB, TAB, TAB)?;
} else if arg.has_switch() {
Expand All @@ -503,7 +506,7 @@ impl<'a> Help<'a> {
write!(self.writer, "{}", part)?;
}
if !help.contains('\n') && (nlh || self.force_next_line) {
write!(self.writer, "\n")?;
writeln!(self.writer)?;
}
Ok(())
}
Expand Down Expand Up @@ -590,8 +593,6 @@ fn should_show_arg(use_long: bool, arg: &ArgWithOrder) -> bool {
impl<'a> Help<'a> {
/// Writes help for all arguments (options, flags, args, subcommands)
/// including titles of a Parser Object to the wrapped stream.
#[cfg_attr(feature = "lints", allow(useless_let_if_seq))]
#[cfg_attr(feature = "cargo-clippy", allow(useless_let_if_seq))]
pub fn write_all_args(&mut self, parser: &Parser) -> ClapResult<()> {
debugln!("Help::write_all_args;");
let flags = parser.has_flags();
Expand Down
39 changes: 22 additions & 17 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@ mod usage;
mod validator;

// Std
use std::env;
use std::ffi::{OsStr, OsString};
use std::fmt;
use std::io::{self, BufRead, BufWriter, Write};
use std::path::Path;
use std::process;
use std::rc::Rc;
use std::result::Result as StdResult;
use std::{
env,
ffi::{OsStr, OsString},
fmt,
io::{self, BufRead, BufWriter, Write},
path::Path,
process,
rc::Rc,
};

// Third Party
#[cfg(feature = "yaml")]
use yaml_rust::Yaml;

// Internal
pub use self::settings::AppSettings;
use app::help::Help;
use app::parser::Parser;
use args::{AnyArg, Arg, ArgGroup, ArgMatcher, ArgMatches, ArgSettings};
use completions::Shell;
use errors::Result as ClapResult;
use map::{self, VecMap};
use crate::errors::Result as ClapResult;
use crate::{
app::{help::Help, parser::Parser},
args::{AnyArg, Arg, ArgGroup, ArgMatcher, ArgMatches, ArgSettings},
completions::Shell,
map::{self, VecMap},
};
pub use settings::AppSettings;

/// Used to create a representation of a command line program and all possible command line
/// arguments. Application settings are set using the "builder pattern" with the
Expand Down Expand Up @@ -90,7 +93,7 @@ impl<'a, 'b> App<'a, 'b> {

/// Get the name of the binary
pub fn get_bin_name(&self) -> Option<&str> {
self.p.meta.bin_name.as_ref().map(|s| s.as_str())
self.p.meta.bin_name.as_deref()
}

/// Creates a new instance of an application requiring a name, but uses the [`crate_authors!`]
Expand Down Expand Up @@ -1640,7 +1643,7 @@ impl<'a, 'b> App<'a, 'b> {
return Err(e);
}

let global_arg_vec: Vec<&str> = (&self).p.global_args.iter().map(|ga| ga.b.name).collect();
let global_arg_vec: Vec<&str> = self.p.global_args.iter().map(|ga| ga.b.name).collect();
matcher.propagate_globals(&global_arg_vec);

Ok(matcher.into())
Expand All @@ -1650,7 +1653,7 @@ impl<'a, 'b> App<'a, 'b> {
#[cfg(feature = "yaml")]
impl<'a> From<&'a Yaml> for App<'a, 'a> {
fn from(mut yaml: &'a Yaml) -> Self {
use args::SubCommand;
use crate::args::SubCommand;
// We WANT this to panic on error...so expect() is good.
let mut is_sc = None;
let mut a = if let Some(name) = yaml["name"].as_str() {
Expand Down Expand Up @@ -1841,9 +1844,11 @@ impl<'n, 'e> AnyArg<'n, 'e> for App<'n, 'e> {
fn possible_vals(&self) -> Option<&[&'e str]> {
None
}
#[cfg_attr(feature = "cargo-clippy", allow(clippy::type_complexity))]
fn validator(&self) -> Option<&Rc<Fn(String) -> StdResult<(), String>>> {
None
}
#[cfg_attr(feature = "cargo-clippy", allow(clippy::type_complexity))]
fn validator_os(&self) -> Option<&Rc<Fn(&OsStr) -> StdResult<(), OsString>>> {
None
}
Expand Down
Loading

0 comments on commit 2bb9180

Please sign in to comment.