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

Use serde to serialize-deserialize configs, including yaml #131

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

Use serde to serialize-deserialize configs, including yaml #131

epage opened this issue Dec 6, 2021 · 3 comments

Comments

@epage
Copy link
Owner

epage commented Dec 6, 2021

Issue by CreepySkeleton
Friday Jan 10, 2020 at 15:02 GMT
Originally opened as clap-rs/clap#1630


Summary

Implement serde::Serialize/Deserialize for clap::App so it could be deserialized not only from a YAML file, but from any other format.

Motivation

Currently, yaml supports deserializing only from YAML via App::from_yaml. This restricts users to only the one blessed format, while the layout of clap::App is pretty simple on it's own and can be expressed in many config formats, such as JSON, TOML, .plist to name a few.

Supporting only the chosen one format is kind of inane nowadays, when we have serde, which handles the differences between formats in a generic way.

Not to mention a number of "YAML is too WTF-full format for config files" considerations. Some users (myself included) would happily move to TOML if it was supported out-of-box.

User experience

As for users of clap who only interested in YAML, nothing would change. From "user experience" side, there's no actual difference between

let yml = load_yaml!("file.yml");
let app = App::from_yaml(&yml);

and

let config = std::fs::File::open("file.yml").expect("No file.yaml!");
let app: App = serde_yaml::from_reader(config).unwrap();

But users interested on other formats, like TOML, JSON, you name it, could really make use of this feature.

Implementation

I gave a quick look-over to App and Arg layout and I'm fairly certain that Serialize and Deserialize can be simply derived.

Related issues

#918: Serializing to / deserializing from JSON. @kbknapp agreed this feature would be pretty useful.

#853: use serde_yaml since it provides much more useful error messages.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by jonericcook
Saturday Jan 18, 2020 at 06:43 GMT


Would this feature allow for the following behavior?

./examplecli helpdump --json helpdump.json

and/or

./examplecli helpdump --stdout

The helpdump.json and stdout would contain all the information about how to use examplecli, essentially the --help for each command and subcommand with all the descriptions and arg type input.

The reason I ask because I have a handful of rust binaries that use clap and i would like to be able to call, for example, the helpdump command to get a json object that tells me how to interact with the binary so I can programmatically use the binary from another language.

I guess in a nut shell i'm asking for a rust program that uses clap to be able to output a .json file and/or a json string to stdout that describes how to use it so that it (the rust program) can be used by another program just by ingesting the .json/json string.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by pickfire
Saturday Apr 11, 2020 at 15:46 GMT


Maybe we could also merge configuration with the commands, with the configuration in file overwritten by the configuration specified by command arguments?

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by throwaway1037
Tuesday Apr 20, 2021 at 17:18 GMT


I think RON should be supported.

Also, I think DSL support should only cost compile time and be baked into the binary, with no cost at runtime since it's essentially a declarative specification of the command-line arguments for the program. The alternative is reading the file at runtime, and then having to distribute it with the program, which is slower and uses more storage space. I don't know which is the current behaviour.

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