Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(parser): Reduce lookups for conflicts #4572

Merged
merged 3 commits into from Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion clap_mangen/src/lib.rs
Expand Up @@ -231,7 +231,7 @@ impl Man {
}

fn _render_version_section(&self, roff: &mut Roff) {
let version = roman(&render::version(&self.cmd));
let version = roman(render::version(&self.cmd));
roff.control("SH", ["VERSION"]);
roff.text([version]);
}
Expand Down
24 changes: 12 additions & 12 deletions clap_mangen/src/render.rs
Expand Up @@ -13,7 +13,7 @@ pub(crate) fn about(roff: &mut Roff, cmd: &clap::Command) {
Some(about) => format!("{} - {}", cmd.get_name(), about),
None => cmd.get_name().to_string(),
};
roff.text([roman(&s)]);
roff.text([roman(s)]);
}

pub(crate) fn description(roff: &mut Roff, cmd: &clap::Command) {
Expand All @@ -36,19 +36,19 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
match (opt.get_short(), opt.get_long()) {
(Some(short), Some(long)) => {
line.push(roman(lhs));
line.push(bold(&format!("-{}", short)));
line.push(bold(format!("-{}", short)));
line.push(roman("|"));
line.push(bold(&format!("--{}", long)));
line.push(bold(format!("--{}", long)));
line.push(roman(rhs));
}
(Some(short), None) => {
line.push(roman(lhs));
line.push(bold(&format!("-{} ", short)));
line.push(bold(format!("-{} ", short)));
line.push(roman(rhs));
}
(None, Some(long)) => {
line.push(roman(lhs));
line.push(bold(&format!("--{}", long)));
line.push(bold(format!("--{}", long)));
line.push(roman(rhs));
}
(None, None) => continue,
Expand Down Expand Up @@ -101,12 +101,12 @@ pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {

if let Some(value) = &opt.get_value_names() {
header.push(roman("="));
header.push(italic(&value.join(" ")));
header.push(italic(value.join(" ")));
}

if let Some(defs) = option_default_values(opt) {
header.push(roman(" "));
header.push(roman(&defs));
header.push(roman(defs));
}

let mut body = vec![];
Expand Down Expand Up @@ -168,13 +168,13 @@ pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
header.push(roman(rhs));

if let Some(defs) = option_default_values(pos) {
header.push(roman(&format!(" {}", defs)));
header.push(roman(format!(" {}", defs)));
}

let mut body = vec![];
let mut arg_help_written = false;
if let Some(help) = option_help(pos) {
body.push(roman(&help.to_string()));
body.push(roman(help.to_string()));
arg_help_written = true;
}

Expand Down Expand Up @@ -229,7 +229,7 @@ pub(crate) fn subcommands(roff: &mut Roff, cmd: &clap::Command, section: &str) {
sub.get_name(),
section
);
roff.text([roman(&name)]);
roff.text([roman(name)]);

if let Some(about) = sub.get_about().or_else(|| sub.get_long_about()) {
for line in about.to_string().lines() {
Expand Down Expand Up @@ -273,11 +273,11 @@ fn markers(required: bool) -> (&'static str, &'static str) {
}

fn short_option(opt: char) -> Inline {
bold(&format!("-{}", opt))
bold(format!("-{}", opt))
}

fn long_option(opt: &str) -> Inline {
bold(&format!("--{}", opt))
bold(format!("--{}", opt))
}

fn option_help(opt: &clap::Arg) -> Option<&clap::builder::StyledStr> {
Expand Down
2 changes: 1 addition & 1 deletion examples/repl.rs
Expand Up @@ -29,7 +29,7 @@ fn main() -> Result<(), String> {
fn respond(line: &str) -> Result<bool, String> {
let args = shlex::split(line).ok_or("error: Invalid quoting")?;
let matches = cli()
.try_get_matches_from(&args)
.try_get_matches_from(args)
.map_err(|e| e.to_string())?;
match matches.subcommand() {
Some(("ping", _matches)) => {
Expand Down
4 changes: 2 additions & 2 deletions src/builder/command.rs
Expand Up @@ -487,7 +487,7 @@ impl Command {
/// [`Command::try_get_matches_from_mut`]: Command::try_get_matches_from_mut()
#[inline]
pub fn get_matches(self) -> ArgMatches {
self.get_matches_from(&mut env::args_os())
self.get_matches_from(env::args_os())
}

/// Parse [`env::args_os`], exiting on failure.
Expand Down Expand Up @@ -545,7 +545,7 @@ impl Command {
#[inline]
pub fn try_get_matches(self) -> ClapResult<ArgMatches> {
// Start the parsing
self.try_get_matches_from(&mut env::args_os())
self.try_get_matches_from(env::args_os())
}

/// Parse the specified arguments, exiting on failure.
Expand Down
4 changes: 4 additions & 0 deletions src/parser/arg_matcher.rs
Expand Up @@ -113,6 +113,10 @@ impl ArgMatcher {
self.matches.args.keys()
}

pub(crate) fn args(&self) -> crate::util::flat_map::Iter<'_, Id, MatchedArg> {
self.matches.args.iter()
}

pub(crate) fn entry(&mut self, arg: Id) -> crate::util::Entry<Id, MatchedArg> {
self.matches.args.entry(arg)
}
Expand Down