Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making custom help optional with default_value_t panicks #4326

Closed
2 tasks done
erazor-de opened this issue Oct 1, 2022 · 1 comment · Fixed by #4330
Closed
2 tasks done

making custom help optional with default_value_t panicks #4326

erazor-de opened this issue Oct 1, 2022 · 1 comment · Fixed by #4330
Labels
C-bug Category: bug

Comments

@erazor-de
Copy link

Please complete the following tasks

Rust Version

rustc 1.64.0 (a55dd71d5 2022-09-19)

Clap Version

4.0.7

Minimal reproducible code

[package]
name = "claptest"
version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4", features = ["derive", "debug"] }
use clap::{ArgAction, Parser};

#[derive(Parser)]
#[command(disable_help_flag = true)]
struct Args {
    #[arg(long, action = ArgAction::Help, default_value_t = false)]
    help: bool,
}

fn main() {
    let _args = Args::parse();
}

Steps to reproduce the bug with the above code

cargo run

Actual Behaviour

thread 'main' panicked at 'assertion failed: (left == right)
left: ["false"],
right: []', /home/stefan/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.0.7/src/parser/parser.rs:1277:17

Expected Behaviour

No panick, no output.

Additional Context

I try to get rid of the short help '-h' by removing default behaviour and creating own help argument with only long version using default functionality.
With using only #[arg(long, action = ArgAction::Help)] help is required.
Making the argument optional with default_value_t = false leads to panick.
Using help: Option<bool>, works and can be used as workaround.

Debug Output

[ clap::builder::command] Command::_do_parse
[ clap::builder::command] Command::_build: name="claptest"
[ clap::builder::command] Command::_propagate:claptest
[ clap::builder::command] Command::_check_help_and_version:claptest expand_help_tree=false
[ clap::builder::command] Command::long_help_exists
[ clap::builder::command] Command::_propagate_global_args:claptest
[clap::builder::debug_asserts] Command::_debug_asserts
[clap::builder::debug_asserts] Arg::_debug_asserts:help
[clap::builder::debug_asserts] Command::_verify_positionals
[ clap::parser::parser] Parser::get_matches_with
[ clap::parser::parser] Parser::add_defaults
[ clap::parser::parser] Parser::add_defaults:iter:help:
[ clap::parser::parser] Parser::add_default_value: doesn't have conditional defaults
[ clap::parser::parser] Parser::add_default_value:iter:help: has default vals
[ clap::parser::parser] Parser::add_default_value:iter:help: wasn't used
[ clap::parser::parser] Parser::react action=Help, identifier=None, source=DefaultValue
thread 'main' panicked at 'assertion failed: (left == right)
left: ["false"],
right: []', /home/stefan/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.0.7/src/parser/parser.rs:1277:17
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

@erazor-de erazor-de added the C-bug Category: bug label Oct 1, 2022
epage added a commit to epage/clap that referenced this issue Oct 1, 2022
These didn't make sense for the builder but are helpful for the derive.
The assert was assuming people wouldn't do this and to catch internal
problems.

Fixes clap-rs#4326
@epage
Copy link
Member

epage commented Oct 1, 2022

The assert is removed in 4.0.8. I've also considered adding support for () as a type to say "don't bother reading a value". If you are interested in that for this case, feel free to create an issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants