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

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

Closed
Hellseher opened this issue Sep 6, 2018 · 5 comments
Closed
Labels
A-docs Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue.
Milestone

Comments

@Hellseher
Copy link

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

@Hellseher Hellseher changed the title YAML not parsed when when name, version, author, about not specified in it YAML not parsed when name, version, author, about not specified in it Sep 7, 2018
@evanjs
Copy link

evanjs commented Mar 22, 2019

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.

@CreepySkeleton
Copy link
Contributor

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.

@CreepySkeleton CreepySkeleton added A-docs Area: documentation, including docs.rs, readme, examples, etc... C: yaml parser E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue. C-enhancement Category: Raise on the bar on expectations labels Feb 2, 2020
@CreepySkeleton CreepySkeleton added this to the 3.1 milestone Feb 2, 2020
@ilyvion
Copy link

ilyvion commented Apr 17, 2020

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:
  - ...

😜

@CreepySkeleton
Copy link
Contributor

@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.

@epage
Copy link
Member

epage commented Dec 8, 2021

We have decided to deprecate the YAML API. For more details, see #3087. If there is interest in a YAML API, it will likely be broken out into a clap_yaml and developed independent from the core of clap.

@epage epage closed this as completed Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue.
Projects
None yet
Development

No branches or pull requests

6 participants