Expand description

Provides derive(Kurisu) for the Kurisu crate.

The Kurisu derive macro is not meant to do any validation nor display usage for the command line string values. It will only construct kurisu::Arg structs according to a given Rust Struct, and put them within a function get_info_instance wrapped by a once_cell static instance of a kurisu::Info struct.

Example:

use kurisu::*;

#[derive(Debug, Kurisu)]
struct Yargs {
    knots: usize,
}

fn main() {
    let env_vars: Vec<String> = std::env::args().skip(1).collect();

    // The Derive Macro add `from_args` function to the user's struct,
    // `from_args` will normalize the command line values into kurisu::Info.env_args
    let args = Yargs::from_args(&env_vars);
    
    let knots;
    // This scope is so the mutex guard gets released after
    // we retrieve the information we need.
    {
        // The Derive Macro also add `get_info_instance` function to the user's struct.
        // The Kurisu:Info struct holds all the parsed information the Derive Macro did.
        let info = Yargs::get_info_instance(&env_vars).lock().unwrap();
        knots = info.args.iter().find(|a| a.name == "knots").unwrap().clone();
    }
    
    // If we assume the current example was called as follow: `mycli --knots 8`
    // We would find that information within the Kurisu::Info struct
    assert_eq!(info.env_args, vec!["--knots=8".to_string()]);
    assert_eq!(knots.value, vec!["8".to_string()]);
    assert_eq!(args.knots, 8);
}

Rust types with specific meaning

TypeDescription
boolDefines a Flag
u8Defines a Flag with occurrence tracking as its value
StringDefines value type of an Option or Argument
PathBufDefines value type of an Option or Argument
usizeDefines value type of an Option or Argument
isizeDefines value type of an Option or Argument
f64Defines value type of an Option or Argument
VecDefines a repetitive Option or Argument

Override default behaviors on main Struct

The struct that drives these behaviors is handled by kurisu::Info, most of the annotation are associated to one of its field.

Field nameDefaultAnnotationDescription
name“Unknown”name = “mycli”Change name on usage screen
version0version = “0.1.0”Change version on usage screen
descNonedesc = “some short text”Change description on usage screen
docNone///Change DISCUSSION text on usage screen
allow_noargsfalseallow_noargsDoes not display usage screen if no command line values
  cargoWill try to fetch name, version & desc from the Cargo.toml, the specific field annotation take precedence on the cargo annotation
  nosortAvoid sorting alphabetically arguments, flags & options on usage screen
  auto_shortsEnables the auto generation of short flags / options according to their field’s name first letter, by default no short flags / options are generated unless specified on the struct’s field annotation

Example:

    #[derive(Debug, Kurisu)]
    // Only picks the version from Cargo.toml since name & desc are already present
    #[kurisu(name = "yargs", cargo, desc = "some desc here", auto_shorts)]
    /// This text is for the DISCUSSION
    /// section in the use screen
    ///
    /// It can be a very large text
    struct Yargs {}

Override default behaviors on main Struct’s fields

The struct that drives these behaviors is handled by kurisu::Arg, most of the annotation are associated to one of its field.

Field nameDefaultAnnotationDescription
vnamefield’s namevname = “myname”Change the name of the in the usage screen
positionNonepos OR pos = “1”Defines the position of an argument, pos without a value defines an infinite positional argument
docNone///Defines the description of arg on the usage screen
shortNoneshort OR short = “b”Change the letter used by short flag / option, otherwise it takes the first letter of the struct’s field name
longfield’s namenolong OR long = “myname”Change or remove the long flag / option
envfield’s nameenv = “MYSQL_HOST”By default the field’s name is used to look for environment variable match as last resort, this is to change which environment variable is being matched
env_prefixNoneenv_prefix = “MYSQL_”Will take the field’s name and prefix it and look for a match environment variable
required_ifNonerequired_if = “fieldname”Will make this option required if the other flag/option is present
default“”default = “42”If not present in the command line values, struct field will be assigned this default value instead of default type value
exitNoneexit = “my_exit_func”Local function that triggers std::process::exit() after being executed. A bit like the usage display, it will stop execution at from_args
  parse_with = “my_func”Local function that allows custom parsing for this argument/flag/option

Example:

use std::path::PathBuf;

#[derive(Debug, Kurisu)]
struct Yargs {
    #[kurisu(pos = 1)]
    file: PathBuf,
    #[kurisu(pos)] // Infinite positional argument
    others: String,
    #[kurisu(short, nolong, env_prefix = "MYSQL_")]
    host: String,
    #[kurisu(long = "very-long-flag")]
    more: bool,
    /// description for `crashed` on usage screen
    crashed: bool,
}

Derive Macros