Skip to main content

Crate clap_jobs_arg

Crate clap_jobs_arg 

Source
Expand description

§JobsArguments

Give your clap parsers a --jobs (-j) argument.

It can be given alone (only -j on the command line), to use the number of available cpus (as determined by num_cpus), or a number from 1..=N (where N is the number of available cpus).

It also accepts the keywords serial (1), single (1), some (25%), half (50%), most (75%), full, all, or auto (100%).

If -j is not present on the command line, it will default to the number of available cpus.

Usage example (derived Parser):

use clap::Parser;
use clap_jobs_arg::JobsArguments;

#[derive(Parser, Debug)]
#[command()]
struct Args {
    #[command(flatten)]
    jobs: JobsArguments,
}

fn main() {
    let args = Args::parse_from(["cli-name", "-j", "6"]);
    let jobs = args.jobs;
    assert_eq!(jobs, 6);
    println!("User wants {jobs} parallel processes.");

    // notice: deref to usize, or .as_usize()
    do_in_parallel(*jobs, || ());
    do_in_parallel(jobs.as_usize(), || ());
}

fn do_in_parallel(n_procs: usize, f: impl Fn() -> ()) {
    // ...
}

Structs§

JobsArguments
The struct to apply to the parser, see the crate level documentation.