Skip to content

Commit 8830771

Browse files
CreepySkeletonTeXitoi
authored andcommitted
Update documentation and changelog (#236)
1 parent 6bd1a69 commit 8830771

File tree

3 files changed

+309
-81
lines changed

3 files changed

+309
-81
lines changed

CHANGELOG.md

Lines changed: 143 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,159 @@
22

33
## Breaking changes
44

5-
* Bump minimum rust version to 1.34 by [@TeXitoi](https://github.com/TeXitoi)
5+
### Bump minimum rustc version to 1.34 by [@TeXitoi](https://github.com/TeXitoi)
6+
Now `rustc` 1.34 is the minimum compiler version supported by `structopt`,
7+
it likely won't work with older compilers.
68

7-
* Support optional vectors of arguments for distinguishing between `-o 1 2`, `-o` and no option provided at all
8-
by [@sphynx](https://github.com/sphynx)
9-
([#180](https://github.com/TeXitoi/structopt/issues/188)). This is a breaking change
10-
as `Option<Vec<T>>` is handled differently. If you need to have a `Option<Vec<T>>`
11-
handled the old was, you can `type Something = Vec<T>;` and then use `Option<Something>`
12-
as your structopt field.
9+
### Remove "nightly" feature
10+
Once upon a time this feature had been used to enable some of improvements
11+
in `proc-macro2` crate that were available only on nightly. Nowadays this feature doesn't
12+
mean anything so it's now removed.
1313

14-
* Change default case from 'Verbatim' into 'Kebab' by [@0ndorio](https://github.com/0ndorio)
15-
([#202](https://github.com/TeXitoi/structopt/issues/202)). This is a breaking change.
16-
If you rely on the old behavior you need to add `#[structopt(rename_all = "verbatim")]`
17-
as an attribute to each data structure deriving the `StructOpt` trait.
14+
### Support optional vectors of arguments for distinguishing between `-o 1 2`, `-o` and no option provided at all by [@sphynx](https://github.com/sphynx) ([#180](https://github.com/TeXitoi/structopt/issues/188)).
1815

19-
* Change `version`, `author` and `about` attributes behavior. `version/author/about = ""` is no longer a
20-
valid syntax. `author` and `about` are no longer derived from `Cargo.toml` by default, i.e when no
21-
attributes mentioned. `version` is still to be derived from `Cargo.toml` by default.
22-
Introduced explicit `author` and `about` attributes (with no arguments, i.e `#[structopt(author)]`)
23-
for explicit requests to deduce author/about fields from `Cargo.toml`.
24-
`version/author/about = "version/author/about"` is still valid syntax.
25-
Introduced `no_version` attribute (no args) which prevents version auto deducing by default.
26-
Proposed by [@TeXitoi](https://github.com/TeXitoi) [(#217)](https://github.com/TeXitoi/structopt/issues/217), implemented by [@CreepySkeleton](https://github.com/CreepySkeleton) [(#229)](https://github.com/TeXitoi/structopt/pull/229).
16+
```rust
17+
#[derive(StructOpt)]
18+
struct Opt {
19+
#[structopt(long)]
20+
fruit: Option<Vec<String>>,
21+
}
22+
23+
fn main() {
24+
assert_eq!(Opt::from_args(&["test"]), None);
25+
assert_eq!(Opt::from_args(&["test", "--fruit"]), Some(vec![]));
26+
assert_eq!(Opt::from_args(&["test", "--fruit=apple orange"]), Some(vec!["apple", "orange"]));
27+
}
28+
```
29+
30+
If you need to fall back to the old behavior you can use a type alias:
31+
```rust
32+
type Something = Vec<String>;
33+
34+
#[derive(StructOpt)]
35+
struct Opt {
36+
#[structopt(long)]
37+
fruit: Option<Vec<String>>,
38+
}
39+
```
40+
41+
### Change default case from 'Verbatim' into 'Kebab' by [@0ndorio](https://github.com/0ndorio) ([#202](https://github.com/TeXitoi/structopt/issues/202)).
42+
`structopt` 0.3 uses field renaming to deduce a name for long options and subcommands.
43+
44+
```rust
45+
#[derive(StructOpt)]
46+
struct Opt {
47+
#[structopt(long)]
48+
http_addr: String, // will be renamed to `--http-addr`
49+
50+
#[structopt(subcommand)]
51+
addr_type: AddrType // this adds `addr-type` subcommand
52+
}
53+
```
54+
55+
`structopt` 0.2 used to leave things "as is", not renaming anything. If you want to keep old
56+
behavior add `#[structopt(rename_all = "verbatim")]` on top of a `struct`/`enum`.
57+
58+
### Change `version`, `author` and `about` attributes behavior.
59+
Proposed by [@TeXitoi](https://github.com/TeXitoi) [(#217)](https://github.com/TeXitoi/structopt/issues/217), implemented by [@CreepySkeleton](https://github.com/CreepySkeleton) [(#229)](https://github.com/TeXitoi/structopt/pull/229).
60+
61+
`structopt` have been deducing `version`, `author`, and `about` properties from `Cargo.toml`
62+
for a long time (more accurately, from `CARGO_PKG_...` environment variables).
63+
But many users found this behavior somewhat confusing, and a hack was added to cancel out
64+
this behavior: `#[structopt(author = "")]`.
65+
66+
In `structopt` 0.3 this has changed.
67+
* `author` and `about` are no longer deduced by default. You should use `#[structopt(author, about)]`
68+
to explicitly request `structopt` to deduce them.
69+
* Contrary, `version` **is still deduced by default**. You can use `#[structopt(no_version)]` to
70+
cancel it out.
71+
* `#[structopt(author = "", about = "", version = "")]` is no longer a valid syntax
72+
and will trigger an error.
73+
* `#[structopt(version = "version", author = "author", about = "about")]` syntax
74+
stays unaffected by this changes.
75+
76+
### Raw attributes are removed ([#198](https://github.com/TeXitoi/structopt/pull/198)) by [@sphynx](https://github.com/sphynx)
77+
In `structopt` 0.2 you were able to use any method from `clap::App` and `clap::Arg` via
78+
raw attribute: `#[structopt(raw(method_name = "arg"))]`. This syntax was kind of awkward.
79+
80+
```rust
81+
#[derive(StructOpt, Debug)]
82+
#[structopt(raw(
83+
global_settings = "&[AppSettings::ColoredHelp, AppSettings::VersionlessSubcommands]"
84+
))]
85+
struct Opt {
86+
#[structopt(short = "l", long = "level", raw(aliases = r#"&["set-level", "lvl"]"#))]
87+
level: Vec<String>,
88+
}
89+
```
90+
91+
Raw attributes were removed in 0.3. Now you can use any method from `App` and `Arg` *directly*:
92+
```rust
93+
#[derive(StructOpt)]
94+
#[structopt(global_settings(&[AppSettings::ColoredHelp, AppSettings::VersionlessSubcommands]))]
95+
struct Opt {
96+
#[structopt(short = "l", long = "level", aliases(&["set-level", "lvl"]))]
97+
level: Vec<String>,
98+
}
99+
```
100+
101+
## Improvements
102+
103+
### Support skipping struct fields
104+
Proposed by [@Morganamilo](https://github.com/Morganamilo) in ([#174](https://github.com/TeXitoi/structopt/issues/174))
105+
implemented by [@sphynx](https://github.com/sphynx) in ([#213](https://github.com/TeXitoi/structopt/issues/213)).
106+
107+
Sometimes you want to include some fields in your `StructOpt` `struct` that are not options
108+
and `clap` should know nothing about them. In `structopt` 0.3 it's possible via the
109+
`#[structopt(skip)]` attribute. The field in question will be assigned with `Default::default()`
110+
value.
27111

28-
## improvements
112+
```rust
113+
#[derive(StructOpt)]
114+
struct Opt {
115+
#[structopt(short, long)]
116+
speed: f32,
117+
118+
car: String,
119+
120+
// this field should not generate any arguments
121+
#[structopt(skip)]
122+
meta: Vec<u64>
123+
}
124+
```
29125

30-
* Support skipping struct fields by [@sphynx](https://github.com/sphynx)
31-
([#174](https://github.com/TeXitoi/structopt/issues/174))
126+
### Add optional feature to support `paw` by [@gameldar](https://github.com/gameldar) ([#187](https://github.com/TeXitoi/structopt/issues/187))
32127

33-
* Add optional feature to support `paw` by [@gameldar](https://github.com/gameldar)
34-
([#187](https://github.com/TeXitoi/structopt/issues/187))
128+
### Significantly improve error reporting by [@CreepySkeleton](https://github.com/CreepySkeleton) ([#225](https://github.com/TeXitoi/structopt/pull/225/))
129+
Now (almost) every error message points to the location it originates from:
35130

36-
* Significantly improve error reporting by [@CreepySkeleton](https://github.com/CreepySkeleton)
37-
([#225](https://github.com/TeXitoi/structopt/pull/225/))
131+
```text
132+
error: default_value is meaningless for bool
133+
--> $DIR/bool_default_value.rs:14:24
134+
|
135+
14 | #[structopt(short, default_value = true)]
136+
| ^^^^^^^^^^^^^
137+
```
38138

39139
# v0.2.16 (2019-05-29)
40140

41-
* Support optional options with optional argument, allowing `cmd [--opt[=value]]`
42-
by [@sphynx](https://github.com/sphynx)
43-
([#188](https://github.com/TeXitoi/structopt/issues/188))
141+
### Support optional options with optional argument, allowing `cmd [--opt[=value]]` by [@sphynx](https://github.com/sphynx) ([#188](https://github.com/TeXitoi/structopt/issues/188))
142+
Sometimes you want to represent an optional option that optionally takes an argument,
143+
i.e `[--opt[=value]]`. This is represented by `Option<Option<T>>`
144+
145+
```rust
146+
#[derive(StructOpt)]
147+
struct Opt {
148+
#[structopt(long)]
149+
fruit: Option<Option<String>>,
150+
}
151+
152+
fn main() {
153+
assert_eq!(Opt::from_args(&["test"]), None);
154+
assert_eq!(Opt::from_args(&["test", "--fruit"]), Some(None));
155+
assert_eq!(Opt::from_args(&["test", "--fruit=apple"]), Some("apple"));
156+
}
157+
```
44158

45159
# v0.2.15 (2019-03-08)
46160

examples/skip.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use structopt::StructOpt;
2+
3+
#[derive(StructOpt, Debug, PartialEq)]
4+
#[structopt(name = "a")]
5+
pub struct Opt {
6+
#[structopt(long, short)]
7+
number: u32,
8+
#[structopt(skip)]
9+
k: Kind,
10+
#[structopt(skip)]
11+
v: Vec<u32>,
12+
}
13+
14+
#[derive(Debug, PartialEq)]
15+
#[allow(unused)]
16+
enum Kind {
17+
A,
18+
B,
19+
}
20+
21+
impl Default for Kind {
22+
fn default() -> Self {
23+
return Kind::B;
24+
}
25+
}
26+
27+
fn main() {
28+
assert_eq!(
29+
Opt::from_iter(&["test", "-n", "10"]),
30+
Opt {
31+
number: 10,
32+
k: Kind::B,
33+
v: vec![],
34+
}
35+
);
36+
}

0 commit comments

Comments
 (0)