Skip to content

Commit

Permalink
refactor: Update app variables to cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 14, 2022
1 parent 9a7e6a5 commit e8010e7
Show file tree
Hide file tree
Showing 99 changed files with 1,472 additions and 1,472 deletions.
44 changes: 22 additions & 22 deletions benches/04_new_help.rs
Expand Up @@ -3,9 +3,9 @@ use clap::{arg, Arg};
use criterion::{criterion_group, criterion_main, Criterion};
use std::io::Cursor;

fn build_help(app: &mut Command) -> String {
fn build_help(cmd: &mut Command) -> String {
let mut buf = Cursor::new(Vec::with_capacity(50));
app.write_help(&mut buf).unwrap();
cmd.write_help(&mut buf).unwrap();
let content = buf.into_inner();
String::from_utf8(content).unwrap()
}
Expand Down Expand Up @@ -157,53 +157,53 @@ fn app_example10<'c>() -> Command<'c> {
}

pub fn example1(c: &mut Criterion) {
let mut app = app_example1();
c.bench_function("example1", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_example1();
c.bench_function("example1", |b| b.iter(|| build_help(&mut cmd)));
}

pub fn example2(c: &mut Criterion) {
let mut app = app_example2();
c.bench_function("example2", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_example2();
c.bench_function("example2", |b| b.iter(|| build_help(&mut cmd)));
}

pub fn example3(c: &mut Criterion) {
let mut app = app_example3();
c.bench_function("example3", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_example3();
c.bench_function("example3", |b| b.iter(|| build_help(&mut cmd)));
}

pub fn example4(c: &mut Criterion) {
let mut app = app_example4();
c.bench_function("example4", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_example4();
c.bench_function("example4", |b| b.iter(|| build_help(&mut cmd)));
}

pub fn example5(c: &mut Criterion) {
let mut app = app_example5();
c.bench_function("example5", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_example5();
c.bench_function("example5", |b| b.iter(|| build_help(&mut cmd)));
}

pub fn example6(c: &mut Criterion) {
let mut app = app_example6();
c.bench_function("example6", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_example6();
c.bench_function("example6", |b| b.iter(|| build_help(&mut cmd)));
}

pub fn example7(c: &mut Criterion) {
let mut app = app_example7();
c.bench_function("example7", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_example7();
c.bench_function("example7", |b| b.iter(|| build_help(&mut cmd)));
}

pub fn example8(c: &mut Criterion) {
let mut app = app_example8();
c.bench_function("example8", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_example8();
c.bench_function("example8", |b| b.iter(|| build_help(&mut cmd)));
}

pub fn example10(c: &mut Criterion) {
let mut app = app_example10();
c.bench_function("example10", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_example10();
c.bench_function("example10", |b| b.iter(|| build_help(&mut cmd)));
}

pub fn example4_template(c: &mut Criterion) {
let mut app = app_example4().help_template("{bin} {version}\n{author}\n{about}\n\nUSAGE:\n {usage}\n\nOPTIONS:\n{options}\n\nARGS:\n{args}\n");
c.bench_function("example4_template", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_example4().help_template("{bin} {version}\n{author}\n{about}\n\nUSAGE:\n {usage}\n\nOPTIONS:\n{options}\n\nARGS:\n{args}\n");
c.bench_function("example4_template", |b| b.iter(|| build_help(&mut cmd)));
}

criterion_group!(
Expand Down
20 changes: 10 additions & 10 deletions benches/05_ripgrep.rs
Expand Up @@ -19,13 +19,13 @@ pub fn build_rg_with_long_help(c: &mut Criterion) {
}

pub fn write_rg_short_help(c: &mut Criterion) {
let mut app = app_short();
c.bench_function("write_rg_short_help", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_short();
c.bench_function("write_rg_short_help", |b| b.iter(|| build_help(&mut cmd)));
}

pub fn write_rg_long_help(c: &mut Criterion) {
let mut app = app_long();
c.bench_function("write_rg_long_help", |b| b.iter(|| build_help(&mut app)));
let mut cmd = app_long();
c.bench_function("write_rg_long_help", |b| b.iter(|| build_help(&mut cmd)));
}

pub fn parse_rg(c: &mut Criterion) {
Expand Down Expand Up @@ -271,30 +271,30 @@ OPTIONS:

/// Build a clap application with short help strings.
fn app_short() -> Command<'static> {
app(false, |k| USAGES[k].short)
cmd(false, |k| USAGES[k].short)
}

/// Build a clap application with long help strings.
fn app_long() -> Command<'static> {
app(true, |k| USAGES[k].long)
cmd(true, |k| USAGES[k].long)
}

/// Build the help text of an application.
fn build_help(app: &mut Command) -> String {
fn build_help(cmd: &mut Command) -> String {
let mut buf = Cursor::new(Vec::with_capacity(50));
app.write_help(&mut buf).unwrap();
cmd.write_help(&mut buf).unwrap();
let content = buf.into_inner();
String::from_utf8(content).unwrap()
}

/// Build a clap application parameterized by usage strings.
///
/// The function given should take a clap argument name and return a help
/// string. `app` will panic if a usage string is not defined.
/// string. `cmd` will panic if a usage string is not defined.
///
/// This is an intentionally stand-alone module so that it can be used easily
/// in a `build.rs` script to build shell completion files.
fn app<F>(_next_line_help: bool, doc: F) -> Command<'static>
fn cmd<F>(_next_line_help: bool, doc: F) -> Command<'static>
where
F: Fn(&'static str) -> &'static str,
{
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/examples/bash_completion.rs
Expand Up @@ -3,9 +3,9 @@ use clap_complete::{generate, shells::Bash};
use std::io;

fn main() {
let mut app = Command::new("myapp")
let mut cmd = Command::new("myapp")
.subcommand(Command::new("test").subcommand(Command::new("config")))
.subcommand(Command::new("hello"));

generate(Bash, &mut app, "myapp", &mut io::stdout());
generate(Bash, &mut cmd, "myapp", &mut io::stdout());
}
8 changes: 4 additions & 4 deletions clap_complete/examples/value_hints.rs
Expand Up @@ -92,16 +92,16 @@ fn build_cli() -> Command<'static> {
)
}

fn print_completions<G: Generator>(gen: G, app: &mut Command) {
generate(gen, app, app.get_name().to_string(), &mut io::stdout());
fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

fn main() {
let matches = build_cli().get_matches();

if let Ok(generator) = matches.value_of_t::<Shell>("generator") {
let mut app = build_cli();
let mut cmd = build_cli();
eprintln!("Generating completion file for {}...", generator);
print_completions(generator, &mut app);
print_completions(generator, &mut cmd);
}
}
8 changes: 4 additions & 4 deletions clap_complete/examples/value_hints_derive.rs
Expand Up @@ -57,17 +57,17 @@ struct Opt {
email: Option<String>,
}

fn print_completions<G: Generator>(gen: G, app: &mut Command) {
generate(gen, app, app.get_name().to_string(), &mut io::stdout());
fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

fn main() {
let opt = Opt::parse();

if let Some(generator) = opt.generator {
let mut app = Opt::into_app();
let mut cmd = Opt::into_app();
eprintln!("Generating completion file for {:?}...", generator);
print_completions(generator, &mut app);
print_completions(generator, &mut cmd);
} else {
println!("{:#?}", opt);
}
Expand Down
32 changes: 16 additions & 16 deletions clap_complete/src/generator/mod.rs
Expand Up @@ -28,7 +28,7 @@ pub trait Generator {
/// pub struct Fish;
///
/// impl Generator for Fish {
/// # fn generate(&self, app: &Command, buf: &mut dyn Write) {}
/// # fn generate(&self, cmd: &Command, buf: &mut dyn Write) {}
/// fn file_name(&self, name: &str) -> String {
/// format!("{}.fish", name)
/// }
Expand All @@ -55,15 +55,15 @@ pub trait Generator {
/// pub struct ClapDebug;
///
/// impl Generator for ClapDebug {
/// fn generate(&self, app: &Command, buf: &mut dyn Write) {
/// write!(buf, "{}", app).unwrap();
/// fn generate(&self, cmd: &Command, buf: &mut dyn Write) {
/// write!(buf, "{}", cmd).unwrap();
/// }
/// # fn file_name(&self, name: &str) -> String {
/// # name.into()
/// # }
/// }
/// ```
fn generate(&self, app: &Command, buf: &mut dyn Write);
fn generate(&self, cmd: &Command, buf: &mut dyn Write);
}

/// Generate a completions file for a specified shell at compile-time.
Expand Down Expand Up @@ -144,10 +144,10 @@ pub trait Generator {
/// Some(outdir) => outdir,
/// };
///
/// let mut app = build_cli();
/// let mut cmd = build_cli();
/// let path = generate_to(
/// Bash,
/// &mut app, // We need to specify what generator to use
/// &mut cmd, // We need to specify what generator to use
/// "myapp", // We need to specify the bin name manually
/// outdir, // We need to specify where to write to
/// )?;
Expand All @@ -166,7 +166,7 @@ pub trait Generator {
/// to see the name of the files generated.
pub fn generate_to<G, S, T>(
gen: G,
app: &mut clap::Command,
cmd: &mut clap::Command,
bin_name: S,
out_dir: T,
) -> Result<PathBuf, Error>
Expand All @@ -175,15 +175,15 @@ where
S: Into<String>,
T: Into<OsString>,
{
app.set_bin_name(bin_name);
cmd.set_bin_name(bin_name);

let out_dir = PathBuf::from(out_dir.into());
let file_name = gen.file_name(app.get_bin_name().unwrap());
let file_name = gen.file_name(cmd.get_bin_name().unwrap());

let path = out_dir.join(file_name);
let mut file = File::create(&path)?;

_generate::<G, S>(gen, app, &mut file);
_generate::<G, S>(gen, cmd, &mut file);
Ok(path)
}

Expand Down Expand Up @@ -222,21 +222,21 @@ where
/// ```shell
/// $ myapp generate-bash-completions > /usr/share/bash-completion/completions/myapp.bash
/// ```
pub fn generate<G, S>(gen: G, app: &mut clap::Command, bin_name: S, buf: &mut dyn Write)
pub fn generate<G, S>(gen: G, cmd: &mut clap::Command, bin_name: S, buf: &mut dyn Write)
where
G: Generator,
S: Into<String>,
{
app.set_bin_name(bin_name);
_generate::<G, S>(gen, app, buf)
cmd.set_bin_name(bin_name);
_generate::<G, S>(gen, cmd, buf)
}

fn _generate<G, S>(gen: G, app: &mut clap::Command, buf: &mut dyn Write)
fn _generate<G, S>(gen: G, cmd: &mut clap::Command, buf: &mut dyn Write)
where
G: Generator,
S: Into<String>,
{
app._build_all();
cmd._build_all();

gen.generate(app, buf)
gen.generate(cmd, buf)
}

0 comments on commit e8010e7

Please sign in to comment.