Skip to content

Commit

Permalink
Merge pull request #97 from epage/rename
Browse files Browse the repository at this point in the history
Prepare for rename
  • Loading branch information
epage committed Dec 6, 2021
2 parents 44c34f0 + b2836c0 commit 6b3ba22
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 261 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,13 @@ On top of the clap 2 changes
- `IgnoreCase` is now unicode aware (requires `unicode` feature flag)
- Always respect `ColorChoice::Never`, even if that means we skip colors in some cases
- `ArgMatches` panics on unknown arguments
- Gracefully handle empty `authors` field in `Cargo.toml` with `app_from_crate`

**From structopt 0.3.25**

- Support `SubcommandsNegateReqs` by allowing required `Option<_>`s ([clap-rs/clap#2255](https://github.com/clap-rs/clap/issues/2255))
- Infer `AllowInvalidUtf8` based on parser ([clap-rs/clap#751](https://github.com/clap-rs/clap/issues/2255))
- Gracefully handle empty `authors` field in `Cargo.toml`

On top of the clap 2 changes

Expand Down
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ members = [
name = "clap"
version = "3.0.0-beta.5"
description = "A simple to use, efficient, and full-featured Command Line Argument Parser"
authors = [
"Kevin K. <[email protected]>",
"Clap Maintainers"
]
repository = "https://github.com/clap-rs/clap"
documentation = "https://docs.rs/clap/"
homepage = "https://clap.rs/"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn main() {
$ demo --help
clap [..]

Kevin K. <[email protected]>, Clap Maintainers


A simple to use, efficient, and full-featured Command Line Argument Parser

Expand Down
7 changes: 0 additions & 7 deletions clap_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
name = "clap_derive"
version = "3.0.0-beta.5"
edition = "2018"
authors = [
"Guillaume Pinot <[email protected]>",
"Clap Maintainers"
]
include = [
"src/**/*",
"Cargo.toml",
Expand Down Expand Up @@ -38,9 +34,6 @@ proc-macro2 = "1.0.28"
heck = "0.3.0"
proc-macro-error = "1"

[dev-dependencies]
clap = { path = "../", default-features = false, features = ["std", "derive", "env"] }

[features]
default = []
debug = []
Expand Down
143 changes: 1 addition & 142 deletions clap_derive/README.md
Original file line number Diff line number Diff line change
@@ -1,147 +1,6 @@
# clap_derive

Parse command line argument by defining a struct. It combines [structopt](https://github.com/TeXitoi/structopt) and [clap](https://crates.io/crates/clap) into a single experience. This crate is used by clap, and not meant to be used directly by
consumers.

## Documentation

Find it on [Docs.rs](https://docs.rs/clap_derive). You can also check the [examples](https://github.com/clap-rs/clap/tree/master/clap_derive/examples) and the [changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md).

## Example

Add `clap` to your dependencies of your `Cargo.toml`:

```toml
[dependencies]
clap = "3"
```

And then, in your rust file:
```rust,no_run
use std::path::PathBuf;
use clap::{Parser, ValueHint};
/// A basic example
#[derive(Parser, Debug)]
#[clap(name = "basic")]
struct Opt {
// A flag, true if used in the command line. Note doc comment will
// be used for the help message of the flag. The name of the
// argument will be, by default, based on the name of the field.
/// Activate debug mode
#[clap(short, long)]
debug: bool,
// The number of occurrences of the `v/verbose` flag
/// Verbose mode (-v, -vv, -vvv, etc.)
#[clap(short, long, parse(from_occurrences))]
verbose: u8,
/// Set speed
#[clap(short, long, default_value = "42")]
speed: f64,
/// Output file
#[clap(short, long, parse(from_os_str), value_hint = ValueHint::FilePath)]
output: PathBuf,
// the long option will be translated by default to kebab case,
// i.e. `--nb-cars`.
/// Number of cars
#[clap(short = 'c', long)]
nb_cars: Option<i32>,
/// admin_level to consider
#[clap(short, long)]
level: Vec<String>,
/// Files to process
#[clap(name = "FILE", parse(from_os_str), value_hint = ValueHint::AnyPath)]
files: Vec<PathBuf>,
}
fn main() {
let opt = Opt::parse();
println!("{:#?}", opt);
}
```

Using this example:
```bash
$ ./basic
error: The following required arguments were not provided:
--output <output>

USAGE:
basic --output <output> --speed <speed>

For more information try --help
$ ./basic --help
basic 0.3.0
Guillaume Pinot <[email protected]>, others
A basic example

USAGE:
basic [OPTIONS] --output <output> [--] [file]...

ARGS:
<FILE>... Files to process

OPTIONS:
-c, --nb-cars <nb-cars> Number of cars
-d, --debug Activate debug mode
-h, --help Print help information
-l, --level <level>... admin_level to consider
-o, --output <output> Output file
-s, --speed <speed> Set speed [default: 42]
-V, --version Print version information
-v, --verbose Verbose mode (-v, -vv, -vvv, etc.)

ARGS:
<file>... Files to process
$ ./basic -o foo.txt
Opt {
debug: false,
verbose: 0,
speed: 42.0,
output: "foo.txt",
nb_cars: None,
level: [],
files: [],
}
$ ./basic -o foo.txt -dvvvs 1337 -l alice -l bob --nb-cars 4 bar.txt baz.txt
Opt {
debug: true,
verbose: 3,
speed: 1337.0,
output: "foo.txt",
nb_cars: Some(
4,
),
level: [
"alice",
"bob",
],
files: [
"bar.txt",
"baz.txt",
],
}
```

## clap_derive rustc version policy

- Minimum rustc version modification must be specified in the [changelog](https://github.com/clap-rs/clap_derive/blob/master/CHANGELOG.md) and in the [travis configuration](https://github.com/clap-rs/clap_derive/blob/master/.travis.yaml).
- Contributors can increment minimum rustc version without any justification if the new version is required by the latest version of one of clap_derive's dependencies (`cargo update` will not fail on clap_derive).
- Contributors can increment minimum rustc version if the library user experience is improved.

## Why

I've (@TeXitoi) used [docopt](https://crates.io/crates/docopt) for a long time (pre rust 1.0). I really like the fact that you have a structure with the parsed argument: no need to convert `String` to `f64`, no useless `unwrap`. But on the other hand, I don't like to write by hand the usage string. That's like going back to the golden age of WYSIWYG editors. Field naming is also a bit artificial.

Today, the new standard to read command line arguments in Rust is [clap](https://crates.io/crates/clap). This library is so feature full! But I think there is one downside: even if you can validate argument and expressing that an argument is required, you still need to transform something looking like a hashmap of string vectors to something useful for your application.

Now, there is stable custom derive. Thus I can add to clap the automatic conversion that I miss. Here is the result.
Macro implementation for clap's derives.

## License

Expand Down
23 changes: 15 additions & 8 deletions clap_derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,19 @@ impl Attrs {
}

About(ident, about) => {
let method = Method::from_lit_or_env(ident, about, "CARGO_PKG_DESCRIPTION");
self.methods.push(method);
if let Some(method) =
Method::from_lit_or_env(ident, about, "CARGO_PKG_DESCRIPTION")
{
self.methods.push(method);
}
}

Author(ident, author) => {
self.author = Some(Method::from_lit_or_env(ident, author, "CARGO_PKG_AUTHORS"));
self.author = Method::from_lit_or_env(ident, author, "CARGO_PKG_AUTHORS");
}

Version(ident, version) => {
self.version =
Some(Method::from_lit_or_env(ident, version, "CARGO_PKG_VERSION"));
self.version = Method::from_lit_or_env(ident, version, "CARGO_PKG_VERSION");
}

NameLitStr(name, lit) => {
Expand Down Expand Up @@ -675,12 +677,17 @@ impl Method {
Method { name, args }
}

fn from_lit_or_env(ident: Ident, lit: Option<LitStr>, env_var: &str) -> Self {
fn from_lit_or_env(ident: Ident, lit: Option<LitStr>, env_var: &str) -> Option<Self> {
let mut lit = match lit {
Some(lit) => lit,

None => match env::var(env_var) {
Ok(val) => LitStr::new(&val, ident.span()),
Ok(val) => {
if val.is_empty() {
return None;
}
LitStr::new(&val, ident.span())
}
Err(_) => {
abort!(ident,
"cannot derive `{}` from Cargo.toml", ident;
Expand All @@ -696,7 +703,7 @@ impl Method {
lit = LitStr::new(&edited, lit.span());
}

Method::new(ident, quote!(#lit))
Some(Method::new(ident, quote!(#lit)))
}
}

Expand Down
2 changes: 0 additions & 2 deletions clap_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
// MIT/Apache 2.0 license.

#![doc(html_logo_url = "https://clap.rs/images/media/clap.png")]
#![doc(html_root_url = "https://docs.rs/clap_derive/3.0.0-beta.5")]
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]

Expand Down
4 changes: 0 additions & 4 deletions clap_generate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
name = "clap_generate"
version = "3.0.0-beta.5"
edition = "2018"
authors = [
"Kevin K. <[email protected]>",
"Clap Maintainers"
]
include = [
"src/**/*",
"Cargo.toml",
Expand Down
4 changes: 1 addition & 3 deletions clap_generate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
// See the [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT) files in this repository
// for more information.

#![doc(html_logo_url = "https://clap.rs/images/media/clap.png")]
#![doc(html_root_url = "https://docs.rs/clap_generate/3.0.0-beta.5")]
#![doc = include_str!("../README.md")]
#![deny(missing_docs, trivial_casts, unused_allocation, trivial_numeric_casts)]
#![warn(missing_docs, trivial_casts, unused_allocation, trivial_numeric_casts)]
#![forbid(unsafe_code)]
#![allow(clippy::needless_doctest_main)]

Expand Down
3 changes: 0 additions & 3 deletions clap_generate_fig/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
name = "clap_generate_fig"
version = "3.0.0-beta.5"
edition = "2018"
authors = [
"Clap Maintainers"
]
include = [
"src/**/*",
"Cargo.toml",
Expand Down
4 changes: 1 addition & 3 deletions clap_generate_fig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

//! Generates [Fig](https://github.com/withfig/autocomplete) completions for [`clap`](https://github.com/clap-rs/clap) based CLIs

#![doc(html_logo_url = "https://clap.rs/images/media/clap.png")]
#![doc(html_root_url = "https://docs.rs/clap_generate_fig/3.0.0-beta.5")]
#![deny(missing_docs, trivial_casts, unused_allocation, trivial_numeric_casts)]
#![warn(missing_docs, trivial_casts, unused_allocation, trivial_numeric_casts)]
#![forbid(unsafe_code)]
#![allow(clippy::needless_doctest_main)]

Expand Down
2 changes: 0 additions & 2 deletions examples/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ Used to validate README.md's content
$ demo --help
clap [..]

Kevin K. <[email protected]>, Clap Maintainers

A simple to use, efficient, and full-featured Command Line Argument Parser

USAGE:
Expand Down
2 changes: 0 additions & 2 deletions examples/escaped_positional.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Let's see what this looks like in the help:
$ escaped_positional --help
clap [..]

Kevin K. <[email protected]>, Clap Maintainers

A simple to use, efficient, and full-featured Command Line Argument Parser

USAGE:
Expand Down
2 changes: 0 additions & 2 deletions examples/escaped_positional_derive.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Let's see what this looks like in the help:
$ escaped_positional_derive --help
clap [..]

Kevin K. <[email protected]>, Clap Maintainers

A simple to use, efficient, and full-featured Command Line Argument Parser

USAGE:
Expand Down
Loading

0 comments on commit 6b3ba22

Please sign in to comment.