Skip to content

giellatekno/clap_jobs_arg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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() -> ()) {
    // ...
}

About

Give your clap parser a --jobs/-j argument

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages