Skip to content

Commit

Permalink
docs: Move everything to docs.rs
Browse files Browse the repository at this point in the history
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 clap-rs#3189
  • Loading branch information
epage committed Jul 19, 2022
1 parent a8a7a61 commit d7a0c9e
Show file tree
Hide file tree
Showing 95 changed files with 2,293 additions and 2,125 deletions.
143 changes: 3 additions & 140 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!-- omit in TOC -->
# clap

> **Command Line Argument Parser for Rust**
Expand All @@ -13,149 +12,13 @@

Dual-licensed under [Apache 2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT).

1. [About](#about)
2. Tutorial: [Builder API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_builder/README.md), [Derive API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_derive/README.md)
3. [Examples](https://github.com/clap-rs/clap/blob/v3.2.12/examples/README.md)
4. [API Reference](https://docs.rs/clap)
- [Derive Reference](https://github.com/clap-rs/clap/blob/v3.2.12/examples/derive_ref/README.md)
- [Feature Flags](#feature-flags)
5. [CHANGELOG](https://github.com/clap-rs/clap/blob/v3.2.12/CHANGELOG.md)
6. [FAQ](https://github.com/clap-rs/clap/blob/v3.2.12/docs/FAQ.md)
7. [Questions & Discussions](https://github.com/clap-rs/clap/discussions)
8. [Contributing](https://github.com/clap-rs/clap/blob/v3.2.12/CONTRIBUTING.md)
8. [Sponsors](#sponsors)

## About

Create your command-line parser, with all of the bells and whistles, declaratively or procedurally.

### Example

Add `clap` as a dependency with the `derive` feature enabled:
```console
cargo add clap -F derive
```

This allows using the
[Derive API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_derive/README.md)
which provides access to the [Builder API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_builder/README.md) as attributes on a `struct`:

<!-- Copied from examples/demo.{rs,md} -->
```rust,no_run
use clap::Parser;
/// Simple program to greet a person
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// Name of the person to greet
#[clap(short, long, value_parser)]
name: String,
/// Number of times to greet
#[clap(short, long, value_parser, default_value_t = 1)]
count: u8,
}
fn main() {
let args = Args::parse();
for _ in 0..args.count {
println!("Hello {}!", args.name)
}
}
```

```bash
$ demo --help
clap [..]
Simple program to greet a person

USAGE:
demo[EXE] [OPTIONS] --name <NAME>

OPTIONS:
-c, --count <COUNT> Number of times to greet [default: 1]
-h, --help Print help information
-n, --name <NAME> Name of the person to greet
-V, --version Print version information
```
*(version number and `.exe` extension on windows replaced by placeholders)*

### Aspirations

- Out of the box, users get a polished CLI experience
- Including common argument behavior, help generation, suggested fixes for users, colored output, [shell completions](https://github.com/clap-rs/clap/tree/master/clap_complete), etc
- Flexible enough to port your existing CLI interface
- However, we won't necessarily streamline support for each use case
- Reasonable parse performance
- Resilient maintainership, including
- Willing to break compatibility rather than batching up breaking changes in large releases
- Leverage feature flags to keep to one active branch
- Being under [WG-CLI](https://github.com/rust-cli/team/) to increase the bus factor
- We follow semver and will wait about 6-9 months between major breaking changes
- We will support the last two minor Rust releases (MSRV, currently 1.56.1)

While these aspirations can be at odds with fast build times and low binary
size, we will still strive to keep these reasonable for the flexibility you
get. Check out the
[argparse-benchmarks](https://github.com/rust-cli/argparse-benchmarks-rs) for
CLI parsers optimized for other use cases.

### Selecting an API

Why use the declarative [Derive API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_derive/README.md):
- Easier to read, write, and modify
- Easier to keep the argument declaration and reading of argument in sync
- Easier to reuse, e.g. [clap-verbosity-flag](https://crates.io/crates/clap-verbosity-flag)

Why use the procedural [Builder API](https://github.com/clap-rs/clap/blob/v3.2.12/examples/tutorial_builder/README.md):
- Faster compile times if you aren't already using other procedural macros
- More flexible, e.g. you can look up how many times an argument showed up,
what its values were, and what were the indexes of those values. The Derive
API can only report presence, number of occurrences, or values but no indices
or combinations of data.

### Related Projects

- [wild](https://crates.io/crates/wild) for supporting wildcards (`*`) on Windows like you do Linux
- [argfile](https://crates.io/crates/argfile) for loading additional arguments from a file (aka response files)
- [shadow-rs](https://crates.io/crates/shadow-rs) for generating `Command::long_version`
- [clap_lex](https://crates.io/crates/clap_lex) for a lighter-weight, battle-tested CLI parser
- [clap_mangen](https://crates.io/crates/clap_mangen) for generating man page source (roff)
- [clap_complete](https://crates.io/crates/clap_complete) for shell completion support
- [clap-verbosity-flag](https://crates.io/crates/clap-verbosity-flag)
- [clap-cargo](https://crates.io/crates/clap-cargo)
- [concolor-clap](https://crates.io/crates/concolor-clap)
- [Command-line Apps for Rust](https://rust-cli.github.io/book/index.html) book
- [`trycmd`](https://crates.io/crates/trycmd): Snapshot testing
- Or for more control, [`assert_cmd`](https://crates.io/crates/assert_cmd) and [`assert_fs`](https://crates.io/crates/assert_fs)

## Feature Flags

### Default Features

* **std**: _Not Currently Used._ Placeholder for supporting `no_std` environments in a backwards compatible manner.
* **color**: Turns on colored error messages.
* **suggestions**: Turns on the `Did you mean '--myoption'?` feature for when users make typos.

#### Optional features

* **deprecated**: Guided experience to prepare for next breaking release (at different stages of development, this may become default)
* **derive**: Enables the custom derive (i.e. `#[derive(Parser)]`). Without this you must use one of the other methods of creating a `clap` CLI listed above.
* **cargo**: Turns on macros that read values from `CARGO_*` environment variables.
* **env**: Turns on the usage of environment variables during parsing.
* **regex**: Enables regex validators.
* **unicode**: Turns on support for unicode characters (including emoji) in arguments and help messages.
* **wrap_help**: Turns on the help text wrapping feature, based on the terminal size.

#### Experimental features

**Warning:** These may contain breaking changes between minor releases.

* **unstable-replace**: Enable [`Command::replace`](https://github.com/clap-rs/clap/issues/2836)
* **unstable-grouped**: Enable [`ArgMatches::grouped_values_of`](https://github.com/clap-rs/clap/issues/2924)
* **unstable-v4**: Preview features which will be stable on the v4.0 release
For more details, see:
- [docs.rs](https://docs.rs/clap/latest/clap/)
- [examples](examples/)

## Sponsors

Expand Down
76 changes: 0 additions & 76 deletions docs/FAQ.md

This file was deleted.

40 changes: 0 additions & 40 deletions examples/README.md

This file was deleted.

2 changes: 0 additions & 2 deletions examples/cargo-example-derive.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
*Jump to [source](cargo-example-derive.rs)*

For more on creating a custom subcommand, see [the cargo
book](https://doc.rust-lang.org/cargo/reference/external-tools.html#custom-subcommands).
The crate [`clap-cargo`](https://github.com/crate-ci/clap-cargo) can help in
Expand Down
4 changes: 1 addition & 3 deletions examples/cargo-example-derive.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Note: this requires the `derive` feature

use clap::Parser;

#[derive(Parser)]
#[derive(Parser)] // requires `derive` feature
#[clap(name = "cargo")]
#[clap(bin_name = "cargo")]
enum Cargo {
Expand Down
2 changes: 0 additions & 2 deletions examples/cargo-example.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
*Jump to [source](cargo-example.rs)*

For more on creating a custom subcommand, see [the cargo
book](https://doc.rust-lang.org/cargo/reference/external-tools.html#custom-subcommands).
The crate [`clap-cargo`](https://github.com/crate-ci/clap-cargo) can help in
Expand Down
2 changes: 0 additions & 2 deletions examples/cargo-example.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Note: this requires the `cargo` feature

fn main() {
let cmd = clap::Command::new("cargo")
.bin_name("cargo")
Expand Down
9 changes: 4 additions & 5 deletions examples/demo.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
*Jump to [source](demo.rs)*

**This requires enabling the `derive` feature flag.**

Used to validate README.md's content
```console
$ demo --help
clap [..]
Expand All @@ -17,4 +12,8 @@ OPTIONS:
-n, --name <NAME> Name of the person to greet
-V, --version Print version information

$ demo --name Me
Hello Me!

```
*(version number and `.exe` extension on windows replaced by placeholders)*
2 changes: 0 additions & 2 deletions examples/demo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Note: this requires the `derive` feature

use clap::Parser;

/// Simple program to greet a person
Expand Down
Loading

0 comments on commit d7a0c9e

Please sign in to comment.