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

the --version flag concatenates subcommands #1382

Open
Geal opened this issue Nov 15, 2018 · 5 comments
Open

the --version flag concatenates subcommands #1382

Geal opened this issue Nov 15, 2018 · 5 comments
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-bug Category: Updating dependencies S-blocked Status: Blocked on something else such as an RFC or other implementation work.

Comments

@Geal
Copy link

Geal commented Nov 15, 2018

Maintainer's notes


Rust Version

rustc 1.31.0-nightly (4bd4e4130 2018-10-25)

Affected Version of clap

2.32.0

Bug or Feature Request Summary

Passing the --version flag after one or more subcommands should not append the subcommand names to the process name

Expected Behavior Summary

$ ./target/debug/clap-test --version
claptest 1.0
$ ./target/debug/clap-test test --version
claptest 1.0
$ ./target/debug/clap-test test hello --version
claptest 1.0

Actual Behavior Summary

$ ./target/debug/clap-test --version
claptest 1.0
$ ./target/debug/clap-test test --version
clap-test-test 
$ ./target/debug/clap-test test hello --version
clap-test-test-hello

Sample Code or Link to Sample Code

extern crate clap;
use clap::{Arg, App, SubCommand};

fn main() {
	let matches = App::new("claptest")
		.version("1.0")
		.arg(Arg::with_name("config")
				 .short("c")
				 .long("config")
				 .value_name("FILE")
				 .help("Sets a custom config file")
				 .takes_value(true))
		.subcommand(SubCommand::with_name("test")
								.help("print debug information verbosely")
								.subcommand(SubCommand::with_name("hello")
														.help("hello")))
		.get_matches();
}

Debug output

Compile clap with cargo features "debug" such as:

[dependencies]
clap = { version = "2", features = ["debug"] }

Output for: ./target/debug/clap-test test --version:

Debug Output

DEBUG:clap:Parser::add_subcommand: term_w=None, name=hello
DEBUG:clap:Parser::add_subcommand: term_w=None, name=test
DEBUG:clap:Parser::propagate_settings: self=claptest, g_settings=AppFlags(
(empty)
)
DEBUG:clap:Parser::propagate_settings: sc=test, settings=AppFlags(
NEEDS_LONG_HELP | NEEDS_LONG_VERSION | NEEDS_SC_HELP | UTF8_NONE | COLOR_AUTO
), g_settings=AppFlags(
(empty)
)
DEBUG:clap:Parser::propagate_settings: self=test, g_settings=AppFlags(
(empty)
)
DEBUG:clap:Parser::propagate_settings: sc=hello, settings=AppFlags(
NEEDS_LONG_HELP | NEEDS_LONG_VERSION | NEEDS_SC_HELP | UTF8_NONE | COLOR_AUTO
), g_settings=AppFlags(
(empty)
)
DEBUG:clap:Parser::propagate_settings: self=hello, g_settings=AppFlags(
(empty)
)
DEBUG:clap:Parser::get_matches_with;
DEBUG:clap:Parser::create_help_and_version;
DEBUG:clap:Parser::create_help_and_version: Building --help
DEBUG:clap:Parser::create_help_and_version: Building --version
DEBUG:clap:Parser::create_help_and_version: Building help
DEBUG:clap:Parser::get_matches_with: Begin parsing '"test"' ([116, 101, 115, 116])
DEBUG:clap:Parser::is_new_arg:"test":NotFound
DEBUG:clap:Parser::is_new_arg: arg_allows_tac=false
DEBUG:clap:Parser::is_new_arg: probably value
DEBUG:clap:Parser::is_new_arg: starts_new_arg=false
DEBUG:clap:Parser::possible_subcommand: arg="test"
DEBUG:clap:Parser::get_matches_with: possible_sc=true, sc=Some("test")
DEBUG:clap:Parser::parse_subcommand;
DEBUG:clap:usage::get_required_usage_from: reqs=[], extra=None
DEBUG:clap:usage::get_required_usage_from: after init desc_reqs=[]
DEBUG:clap:usage::get_required_usage_from: no more children
DEBUG:clap:usage::get_required_usage_from: final desc_reqs=[]
DEBUG:clap:usage::get_required_usage_from: args_in_groups=[]
DEBUG:clap:Parser::parse_subcommand: About to parse sc=test
DEBUG:clap:Parser::parse_subcommand: sc settings=AppFlags(
NEEDS_LONG_HELP | NEEDS_LONG_VERSION | NEEDS_SC_HELP | UTF8_NONE | COLOR_AUTO
)
DEBUG:clap:Parser::get_matches_with;
DEBUG:clap:Parser::create_help_and_version;
DEBUG:clap:Parser::create_help_and_version: Building --help
DEBUG:clap:Parser::create_help_and_version: Building --version
DEBUG:clap:Parser::create_help_and_version: Building help
DEBUG:clap:Parser::get_matches_with: Begin parsing '"--version"' ([45, 45, 118, 101, 114, 115, 105, 111, 110])
DEBUG:clap:Parser::is_new_arg:"--version":NotFound
DEBUG:clap:Parser::is_new_arg: arg_allows_tac=false
DEBUG:clap:Parser::is_new_arg: -- found
DEBUG:clap:Parser::is_new_arg: starts_new_arg=true
DEBUG:clap:Parser::possible_subcommand: arg="--version"
DEBUG:clap:Parser::get_matches_with: possible_sc=false, sc=None
DEBUG:clap:ArgMatcher::process_arg_overrides:None;
DEBUG:clap:Parser::parse_long_arg;
DEBUG:clap:Parser::parse_long_arg: Does it contain '='...No
DEBUG:clap:Parser::parse_long_arg: Found valid flag '--version'
DEBUG:clap:Parser::check_for_help_and_version_str;
DEBUG:clap:Parser::check_for_help_and_version_str: Checking if --version is help or version...Version
DEBUG:clap:Parser::_version:
clap-test-test

this does not look like a critical bug, but it still feels weird :)

@EverlastingBugstopper
Copy link

+1 on this. Thought we had a bug in our implementation but it seems to be the way clap handles versions for subcommands. It can be overridden with a .version on each subcommand, but my feeling is that it should default to the parent's version. cloudflare/wrangler-legacy#791

@epage
Copy link
Member

epage commented Dec 8, 2021

This seems like a slight variant on #1431. We should probably look at these together.

@epage epage added the C-bug Category: Updating dependencies label Dec 9, 2021
@epage
Copy link
Member

epage commented Dec 9, 2021

One other aspect of this is that we can have per-subcommand versions. In that case, we probably would want to list what the version is relevant for.

@epage epage added the S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing label Dec 9, 2021
@epage
Copy link
Member

epage commented Feb 3, 2022

Updated the code for clap3

use clap::{App, AppSettings, Arg};

fn main() {
    let matches = App::new("claptest")
        .setting(AppSettings::PropagateVersion)
        .version("1.0")
        .arg(
            Arg::new("config")
                .short('c')
                .long("config")
                .value_name("FILE")
                .help("Sets a custom config file")
                .takes_value(true),
        )
        .subcommand(
            App::new("test")
                .about("print debug information verbosely")
                .subcommand(App::new("hello").about("hello")),
        )
        .get_matches();
}

PropagateVersion ensures you get

$ ./target/debug/clap-test --version
claptest 1.0
$ ./target/debug/clap-test test --version
claptest-test 1.0
$ ./target/debug/clap-test test hello --version
claptest-test-test 1.0

instead of

$ ./target/debug/clap-test --version
claptest 1.0
$ ./target/debug/clap-test test --version
clap-test-test 
$ ./target/debug/clap-test test hello --version
clap-test-test-hello

@epage
Copy link
Member

epage commented Feb 3, 2022

I feel like what we show could be derived from the work in #1334

@epage epage added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing labels Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-bug Category: Updating dependencies S-blocked Status: Blocked on something else such as an RFC or other implementation work.
Projects
None yet
Development

No branches or pull requests

5 participants