Clap: Panicking default_value due to converting YAML Real to String

Created on 1 Sep 2016  路  2Comments  路  Source: clap-rs/clap

Given the minimal example below, I get the following error message:

thread 'main' panicked at 'failed to convert YAML Real("0.1") value to a string'

Any idea what's going on here?

main.rs:

#[macro_use] extern crate clap;

use clap::App;

fn main() {
    let yml = load_yaml!("cli.yml");
    let matches = App::from_yaml(yml)
        .get_matches();
}

cli.yml:

name: Clap Yaml Scrap

args:
    - test:
        takes_value: true
        default_value: 0.1

Cargo.toml:

[package]
name = "clap-yaml-scrap"
version = "0.1.0"
authors = ["SuperFluffy"]

[dependencies]

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

EDIT: Alright, I figure the issue comes from this macro. It's calling as_str() on the Yaml::Real, which is not defined. as_str() is only defined for Yaml::String, as you can see here.

Yaml::Real only has as_f64() defined, which however we cannot a priori invoke as we don't know whether or not default_value is a f64 or something else entirely.

I guess the only way to work around this is by demanding that default_value be set to a string in the Yaml, i.e. default_value: "0.1".

EDIT: Of course, this applies to Yaml::Integer as well. Basically, default_value _has_ to take a string.

RFC / question

Most helpful comment

Maybe this should be made explicit in one of the docs? At first, I was expecting the 0.1 to be simply parsed as a string just like all the other values in the Yaml.

Since I suspect there is no way of making yaml-rust treat all values as strings (due to the spec), maybe this should be made explicit in the docs?

All 2 comments

This is partly due to that clap deals in strings, i.e. the default value will be the string "0.1" not the integer 0.1. To fix this, use default_value: "0.1" and then in the user code convert that string to an integer (via parse::<f32>() or whichever is appropriate.

I agree it's confusing, and less than optimal, but logically all command line arguments are strings, and it's the consumer code that does the conversions.

There may be a better way to represent this in the docs/training. Of course I'd love to have clap automatically parse out to the correct number type, but at this time that's not quite doable on a large scale and thus gets left to consumer code.

Maybe this should be made explicit in one of the docs? At first, I was expecting the 0.1 to be simply parsed as a string just like all the other values in the Yaml.

Since I suspect there is no way of making yaml-rust treat all values as strings (due to the spec), maybe this should be made explicit in the docs?

Was this page helpful?
0 / 5 - 0 ratings