Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

YAML not parsed when name, version, author, about not specified in it #102

Open
epage opened this issue Dec 6, 2021 · 4 comments
Open

Comments

@epage
Copy link
Owner

epage commented Dec 6, 2021

Issue by Hellseher
Thursday Sep 06, 2018 at 23:45 GMT
Originally opened as clap-rs/clap#1338


Rust Version

rustc 1.28.0 (9634041f0 2018-07-30)

Affected Version of clap

[dependencies.clap]
version = "2.31.2"
features = [ "yaml"]

Bug or Feature Request Summary

Expected Behavior Summary

By using method and macros build arguments from yml file where that filed not specified.

    .about(crate_description!())
    .name(crate_name!())
    .author(crate_authors!())
    .version(crate_version!())

Actual Behavior Summary

Arguments list is ignored completely

Steps to Reproduce the issue

Sample Code or Link to Sample Code

cli.yml

version:
author:
about:
args:
  - harware:
      short: H
      long: hardware
      help: Show system hardware specification
  - config:
      short: c
      long: config
      value_name: FILE
      help: Sets a custom config file
      takes_value: true
  - INPUT:
      help: Sets the input file to use
      required: true
      index: 1
  - verbose:
      short: v
      multiple: true
      help: Sets the level of verbosity

main.rs

extern crate clap;

use clap::App;

fn main() {
    let cli_yaml = load_yaml!("cli.yml");
    let matches = App::from_yaml(cli_yaml)
        .about(crate_description!())
        .name(crate_name!())
        .author(crate_authors!())
        .version(crate_version!())
        .get_matches();

    let config = matches.value_of("config").unwrap_or("default.conf");
    println!("Value for config: {}", config);
}
// End of main.rs

Debug output

Debug Output

DEBUG:clap:Parser::propagate_settings: self=stem, 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::get_matches_with: Begin parsing '"--help"' ([45, 45, 104, 101, 108, 112])
DEBUG:clap:Parser::is_new_arg:"--help":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="--help"
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 '--help'
DEBUG:clap:Parser::check_for_help_and_version_str;
DEBUG:clap:Parser::check_for_help_and_version_str: Checking if --help is help or version...Help
DEBUG:clap:Parser::_help: use_long=true
DEBUG:clap:Help::write_parser_help;
DEBUG:clap:Help::write_parser_help;
DEBUG:clap:Parser::color;
DEBUG:clap:Parser::color: Color setting...Auto
DEBUG:clap:is_a_tty: stderr=false
DEBUG:clap:Help::new;
DEBUG:clap:Help::write_help;
DEBUG:clap:Help::write_default_help;
DEBUG:clap:Help::write_bin_name;
DEBUG:clap:Help::write_version;
DEBUG:clap:Help::write_default_help: writing about
DEBUG:clap:usage::create_usage_no_title;
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:usage::needs_flags_tag;
DEBUG:clap:usage::needs_flags_tag:iter: f=hclap_help;
DEBUG:clap:usage::needs_flags_tag:iter: f=vclap_version;
DEBUG:clap:usage::needs_flags_tag: [FLAGS] not required
DEBUG:clap:usage::create_help_usage: usage=stem
DEBUG:clap:Help::write_all_args;
DEBUG:clap:Help::write_args;
DEBUG:clap:Help::write_args: Current Longest...2
DEBUG:clap:Help::write_args: New Longest...6
DEBUG:clap:Help::write_args: Current Longest...6
DEBUG:clap:Help::write_args: New Longest...9
DEBUG:clap:Help::write_arg;
DEBUG:clap:Help::short;
DEBUG:clap:Help::long;
DEBUG:clap:Help::val: arg=--help
DEBUG:clap:Help::spec_vals: a=--help
DEBUG:clap:Help::val: Has switch...Yes
DEBUG:clap:Help::val: force_next_line...false
DEBUG:clap:Help::val: nlh...false
DEBUG:clap:Help::val: taken...21
DEBUG:clap:Help::val: help_width > (width - taken)...23 > (120 - 21)
DEBUG:clap:Help::val: longest...9
DEBUG:clap:Help::val: next_line...No
DEBUG:clap:write_spaces!: num=7
DEBUG:clap:Help::help;
DEBUG:clap:Help::help: Next Line...false
DEBUG:clap:Help::help: Too long...No
DEBUG:clap:Help::write_arg;
DEBUG:clap:Help::short;
DEBUG:clap:Help::long;
DEBUG:clap:Help::val: arg=--version
DEBUG:clap:Help::spec_vals: a=--version
DEBUG:clap:Help::val: Has switch...Yes
DEBUG:clap:Help::val: force_next_line...false
DEBUG:clap:Help::val: nlh...false
DEBUG:clap:Help::val: taken...21
DEBUG:clap:Help::val: help_width > (width - taken)...26 > (120 - 21)
DEBUG:clap:Help::val: longest...9
DEBUG:clap:Help::val: next_line...No
DEBUG:clap:write_spaces!: num=4
DEBUG:clap:Help::help;
DEBUG:clap:Help::help: Next Line...false
DEBUG:clap:Help::help: Too long...No
stem 0.1.0
Sharlatan 
Collecting system level information such as hardware and OS stats.

USAGE:
stem

FLAGS:
-h, --help Prints help information
-V, --version Prints version information

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by evanjs
Friday Mar 22, 2019 at 16:24 GMT


I was able to work around this by specifying only the name in yml and the remaining properties via the crate! macros.
Not sure what other combinations work, but I feel like what is happening should at least be more clear. Between this and the indentation thing I've spent quite some time confused as the app compiles fine but the yml might completely be ignored.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by CreepySkeleton
Sunday Feb 02, 2020 at 05:42 GMT


I would say this is how it's supposed to work.

  • No field - no version/auther/about/etc at all
  • version: crate_version!() - derive it from Cargo.toml
  • The name field is mandatory, you can't omit it. Full stop. If you want it to be derived - use name: crate_name!()

But I agree this should be documented better.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by alexschrod
Friday Apr 17, 2020 at 18:30 GMT


I'm in a somewhat similar situation, didn't feel different enough to create a whole new issue. I do this in my code:

let matches = App::from_yaml(yaml)
    .name(Game::GAME_ID)
    .version(Game::VERSION)
    .author(Game::AUTHOR)
    .about(Game::DESCRIPTION)
    .get_matches();

Since I set all those there, I figured I could leave them out of the YAML file. But no. name is mandatory in the YAML, even if you provide it some other way. So for now, my YAML file looks like this:

name: required_for_some_reason_even_though_it_is_defined_in_code
args:
  - ...

😜

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by CreepySkeleton
Saturday Apr 18, 2020 at 02:11 GMT


@alexschrod Well, this is inevitable. clap::App needs a name because it's a central concept - there's no application without a name. So, in order to being able to create an App, the YAML loader needs a name. There's also no way for it to know that you are going to override it in your code one line below.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant