Boolean arguments in clap

Could somebody please give me short answer:
should I handle data type from CLI input like -i false or I forgot to derive something here:

use clap::Parser;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Argument {
    #[arg(short, long, default_value_t = true)]
    pub index: bool,

No argument, you just do -i and it will be set false...

1 Like

thank you for tip, but in my case empty -i set the value as true in debug
when I set annotation to #[arg(short, long, default_value_t = false)] then it works, but I want have it enabled by default (true when no arguments)

You need to change the default action for bool. The default is to SetTrue. You want to set the default value to true and set the action to SetFalse.

struct Foo {
    #[arg(short, long, action = clap::ArgAction::SetFalse, default_value_t = true)]
    index: bool,
}
1 Like

Really, thank you much!

But now I think that should rename this option as -i maybe really mean -i=true haha