rustc 1.28.0 (9634041f0 2018-07-30)
[dependencies.clap]
version = "2.31.2"
features = [ "yaml"]
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!())
Arguments list is ignored completely
__cli.yml__
```name:
version:
author:
about:
args:
__main.rs__
```#[macro_use]
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: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
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.
I would say this is how it's supposed to work.
version: crate_version!() - derive it from Cargo.tomlname 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.
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:
- ...
馃槣
@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.
Most helpful comment
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.