[][src]Trait ctflag::Flags

pub trait Flags: Sized {
    fn from_args<T>(args: T) -> Result<(Self, Vec<String>)>
    where
        T: IntoIterator<Item = String>
;
fn description() -> String; }

Provides a command-line argument parsing implementation when derived for a named-struct.

#[derive(Flags)]
struct MyFlags {
    enable_floopy: bool,
    // ...
}

Each field in the struct must implement the ctflag::FromArgs trait.

Default values

A flag's default value can be specified using the #[flag(default = ...)] attribute. This attribute only allows the default value to be a type literal. For more control over the default value, using an Option type is preferred. If this attribute is not set, the type must implement the Default trait.

Implementing this trait by hand is not recommended and would defeat the purpose of this crate.

Required methods

fn from_args<T>(args: T) -> Result<(Self, Vec<String>)> where
    T: IntoIterator<Item = String>, 

Consumes the command-line arguments and returns a tuple containing the value of this type, and a list of command-line arguments that were not consumed. These arguments typically include the first command-line argument (usually the program name) and any non-flag arguments (no - prefix).

Example

#[derive(Flags)]
struct MyFlags {
    enable_floopy: bool,
    // ...
}

let (flags, args) = MyFlags::from_args(std::env::args())?;
if flags.enable_floopy {
    // ...
}

fn description() -> String

Returns a String that describes the flags defined in the struct implementing this trait.

For a struct like:

#[derive(Flags)]
struct MyFlags {
    #[flag(desc = "The floopy floops the whoop")]
    enable_floopy: bool,

    #[flag(short = 'o', desc = "Output file", placeholder = "PATH")]
    output: Option<String>,

    #[flag(
        desc = "How many slomps to include",
        placeholder = "INTEGER",
        default = 34
    )]
    slomps: i64,
}

The returned String looks something like:

OPTIONS:
  --enable_floopy        The floopy floops the whoop
  -o, --output [PATH]    Output file
  --slomps INTEGER       How many slomps to include (defaults to 34)
Loading content...

Implementors

Loading content...