clap/examples/typed-derive.md

132 lines
3.8 KiB
Markdown
Raw Normal View History

docs: Move everything to docs.rs A couple of things happened when preparing to release 3.0 - We needed derive documentation - I had liked how serde handled theres - I had bad experiences finding things in structopt's documentation - The examples were broken and we needed tests - The examples seemed to follow a pattern of having tutorial content and cookbook content - We had been getting bug reports from people looking at master and thinking they were looking at what is currently released - We had gotten feedback to keep down the number of places that documentation was located From this, we went with a mix of docs.rs and github - We kept the number of content locations at 2 rather than 3 by not having an external site like serde - We rewrote the examples into explicit tutorials and cookbooks to align with the 4 styles of documentation - We could test our examples by running `console` code blocks with trycmd - Documentation was versioned and the README pointed to the last release This had downsides - The tutorials didn't have the code inlined - Users still had a hard time finding and navigating between the different forms of documentation - In practice, we were less likely to cross-link between the different types of documentation Moving to docs.rs would offer a lot of benefits, even if it is only designed for Rust-reference documentation and isn't good for Rust derive reference documentation, tutorials, cookbooks, etc. The big problem was keeping the examples tested to keep maintenance costs down. Maybe its just me but its easy to overlook - You can pull documentation from a file using `#[doc = "path"]` - Repeated doc attributes get concatenated rather than first or last writer winning Remember these when specifically thinking about Rust documentation made me realize that we could get everything into docs.rs. When doing this - Tutorial code got brought in as was one of the aims - We needed to split the lib documentation and the README to have all of the linking work. This allowed us to specialize them according to their rule (user vs contributor) - We needed to avoid users getting caught up in making a decision between Derive and Builder APIs so we put the focus on the derive API with links to the FAQ to help users decide when to use one or the other. - Improved cross-referencing between different parts of the documentation - Limited inline comments were added to example code - Introductory example code intentionally does not have teaching comments in it as its meant to give a flavor or sense of things and not meant to teach on its own. This is a first attempt. There will be a lot of room for further improvement. Current know downsides: - Content source is more split up for the tutorials This hopefully addresses #3189
2022-07-19 18:29:31 +00:00
**This requires enabling the [`derive` feature flag][crate::_features].**
2022-03-14 14:43:17 +00:00
Help:
```console
$ typed-derive --help
Usage: typed-derive[EXE] [OPTIONS]
Options:
-O <OPTIMIZATION> Implicitly using `std::str::FromStr`
-I <DIR> Allow invalid UTF-8 paths
--bind <BIND> Handle IP addresses
--sleep <SLEEP> Allow human-readable durations
-D <DEFINES> Hand-written parser for tuples
--port <PORT> Support for discrete numbers [default: 22] [possible values: 22, 80]
2023-07-21 15:54:08 +00:00
--log-level <LOG_LEVEL> Support enums from a foreign crate that don't implement `ValueEnum` [default: info] [possible values: trace, debug, info, warn, error]
-h, --help Print help
2022-03-14 14:43:17 +00:00
```
Optimization-level (number)
```console
$ typed-derive -O 1
Args { optimization: Some(1), include: None, bind: None, sleep: None, defines: [], port: 22, log_level: Info }
2022-03-14 14:43:17 +00:00
$ typed-derive -O plaid
? failed
error: invalid value 'plaid' for '-O <OPTIMIZATION>': invalid digit found in string
2022-03-14 14:43:17 +00:00
For more information, try '--help'.
2022-03-14 14:43:17 +00:00
```
2022-03-14 14:45:43 +00:00
Include (path)
```console
$ typed-derive -I../hello
Args { optimization: None, include: Some("../hello"), bind: None, sleep: None, defines: [], port: 22, log_level: Info }
2022-03-14 14:53:31 +00:00
```
IP Address
```console
$ typed-derive --bind 192.0.0.1
Args { optimization: None, include: None, bind: Some(192.0.0.1), sleep: None, defines: [], port: 22, log_level: Info }
2022-03-14 14:53:31 +00:00
$ typed-derive --bind localhost
? failed
error: invalid value 'localhost' for '--bind <BIND>': invalid IP address syntax
2022-03-14 14:53:31 +00:00
For more information, try '--help'.
2022-03-14 14:49:46 +00:00
```
Time
```console
$ typed-derive --sleep 10s
Args { optimization: None, include: None, bind: None, sleep: Some(Duration(10s)), defines: [], port: 22, log_level: Info }
2022-03-14 14:49:46 +00:00
$ typed-derive --sleep forever
? failed
error: invalid value 'forever' for '--sleep <SLEEP>': expected number at 0
2022-03-14 14:49:46 +00:00
For more information, try '--help'.
2022-03-14 14:45:43 +00:00
```
2022-03-14 14:43:17 +00:00
Defines (key-value pairs)
```console
$ typed-derive -D Foo=10 -D Alice=30
Args { optimization: None, include: None, bind: None, sleep: None, defines: [("Foo", 10), ("Alice", 30)], port: 22, log_level: Info }
$ typed-derive -D Foo
? failed
error: invalid value 'Foo' for '-D <DEFINES>': invalid KEY=value: no `=` found in `Foo`
For more information, try '--help'.
$ typed-derive -D Foo=Bar
? failed
error: invalid value 'Foo=Bar' for '-D <DEFINES>': invalid digit found in string
For more information, try '--help'.
```
Discrete numbers
```console
$ typed-derive --port 22
Args { optimization: None, include: None, bind: None, sleep: None, defines: [], port: 22, log_level: Info }
$ typed-derive --port 80
Args { optimization: None, include: None, bind: None, sleep: None, defines: [], port: 80, log_level: Info }
$ typed-derive --port
? failed
error: a value is required for '--port <PORT>' but none was supplied
[possible values: 22, 80]
For more information, try '--help'.
$ typed-derive --port 3000
? failed
error: invalid value '3000' for '--port <PORT>'
[possible values: 22, 80]
For more information, try '--help'.
```
Enums from crates that can't implement `ValueEnum`
```console
$ typed-derive --log-level debug
Args { optimization: None, include: None, bind: None, sleep: None, defines: [], port: 22, log_level: Debug }
$ typed-derive --log-level error
Args { optimization: None, include: None, bind: None, sleep: None, defines: [], port: 22, log_level: Error }
$ typed-derive --log-level
? failed
error: a value is required for '--log-level <LOG_LEVEL>' but none was supplied
2023-07-21 15:54:08 +00:00
[possible values: trace, debug, info, warn, error]
For more information, try '--help'.
$ typed-derive --log-level critical
? failed
error: invalid value 'critical' for '--log-level <LOG_LEVEL>'
2023-07-21 15:54:08 +00:00
[possible values: trace, debug, info, warn, error]
For more information, try '--help'.
```