Skip to content

A proc-macro wrapper around Clap to extend its functionality and change some defaults.

Notifications You must be signed in to change notification settings

wfraser/clap_wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is a helper for Clap that extends it and changes some defaults.

What this changes

Extensions:

  • Ability to add some specified prefix to the long flag for all options in a struct.
    • The prefix is specified on the struct itself, which is useful for a set of options used by a library. All programs that incorporate this struct then get the same flags with the same names (good for consistency), and they won't collide with any flags the program defines.
    • It also means the options all get grouped together automatically in the program's --help text.

Changed Defaults:

  • Boolean fields
    • Can take a value or not. Specifying --flag=true or --flag true or just --flag are all the same, and specifying --flag=false is the same as not specifying it at all.
    • The "value name" used in help text defaults to the string "BOOL".
  • The first line of doc comments on a struct is used as a help text heading and groups all the options together.

How to use:

Instead of

use clap::Parser;

#[derive(Parser)]
#[command(next_help_heading = "My Options")]
struct MyOptions {
    #[arg(long = "my.foo", value_name = "BOOL", num_args(0..=1), action=clap::ArgAction::Set, default_missing_value = "true")]
    foo: bool,
    
    #[arg(long = "my.bar")]
    bar: Option<String>,
}

write:

use clap::Parser;
use clap_wrapper::clap_wrapper;

/// My Options
#[clap_wrapper(prefix = "my")]
#[derive(Parser)]
struct MyOptions {
    #[arg(long)]
    foo: bool,
    
    #[arg(long)]
    bar: Option<String>,
}

These are equivalent, but one is much easier to read and write :)

Help text for this group of options will be like:

My Options:
      --my.foo [<BOOL>]  [default: false] [possible values: true, false]
      --my.bar <BAR>

This group of options would then be used within a program's main options struct like:

#[derive(Parser)]
struct Args {
    #[arg]
    whatever: String,
    // and other program options...
    
    // include all the options from MyOptions:
    #[command(flatten)]
    my: MyOptions,
}

About

A proc-macro wrapper around Clap to extend its functionality and change some defaults.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages