I have two features I'd like to propose:
The first is the ability to specify features when adding dependencies, with a -F or --feature flag that can be specified multiple times:
$ cargo add serde_json --feature preserve_order
The second is that when adding a dependency, the console output should list the available features. I recently implemented my own serialization for some types, because I didn't realize that serde support was an available feature. Something like the following would be nice:
$ cargo add nalgebra
Adding nalgebra v0.13.1 to dependencies
Optional features:
- arbitrary
- serde-serialize
- abomonation-serialize
- debug
Thanks for the suggestion!
I guess listing features was implemented some time ago, but we've removed the cargo list subcommand in #130.
It seems like a useful addition to me, let's see what @killercup and @bjgill think.
What is not clear is whether we should list aliased features as well, e.g. in semver's Cargo.toml serde feature is not listed directly, so in your example the output would be something like (num-complex/serde is not listed):
$ cargo add nalgebra --features "abomonation-serialize"
Adding nalgebra v0.13.1 to dependencies
Features:
+ abomonation
+ abomonation-serialize
- arbitrary
- debug
- serde
- serde-derive
- serde-serialize
Also we should follow cargo in specifying multiple features.
Good point about the syntax for specifying multiple features.
Sometimes I see optional dependencies that probably wouldn't work right if you enabled them individually. For example, serde and serde-derive might be grouped together under a single feature like in that example.
My inclination is to list both the features and the optional dependencies themselves, but with some indentation to indicate how they're grouped together. (I'd make an example if I wasn't on my phone)
I'd be happy to implement this. I looked at the code, and it seems fairly straightforward.
Any update on this?
How about:
cargo feature <crate> +<feature1> -<feature2> (adding feature1 to crate, and removing feature2).
So it'd be:
cargo feature serde_json +preserve_order
I guess, removing features will rarely be done (would be acceptable to have to use an editor for that), so maybe the following syntax would be better:
cargo fadd <crate> <feature1> <feature2> ..
So:
cargo fadd serde_json preserve_order
The syntax shouldn't be too verbose (not requiring writing --feature).
Without considering default-features, this syntax is consistent with the features.*[*] format of Cargo.toml and should be backward-compatible:
# this appends `serde = {version = "x.y.z", features = ["derive"]}`,
# or edits the existing entry with the feature added
cargo add serde/derive
# if existing entry is serde = {version = "x.y.z", features = ["derive"]}`,
# this will only remove the feature without removing serde
# if the existing entry does not have the derive feature, we abort with eror
cargo rm serde/derive
# this adds any missing features among serde/derive and serde/rc without errors
# (but might warn if exists)
cargo add serde/derive serde/rc
As for default-features, I think it makes sense for anyone who wants to have more complex argument lists to be in separate command.
Most helpful comment
Any update on this?
How about:
cargo feature <crate> +<feature1> -<feature2>(adding feature1 to crate, and removing feature2).So it'd be:
cargo feature serde_json +preserve_orderI guess, removing features will rarely be done (would be acceptable to have to use an editor for that), so maybe the following syntax would be better:
cargo fadd <crate> <feature1> <feature2> ..So:
cargo fadd serde_json preserve_orderThe syntax shouldn't be too verbose (not requiring writing
--feature).