Skip to content

Commit 7c37dd5

Browse files
authored
Merge pull request #1731 from epage/clap3
Port mdBook to clap3
2 parents 1f04a62 + 857ca19 commit 7c37dd5

File tree

12 files changed

+176
-127
lines changed

12 files changed

+176
-127
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ jobs:
3131
rust: stable
3232
- build: msrv
3333
os: ubuntu-latest
34-
rust: 1.46.0
34+
# sync MSRV with docs: guide/src/guide/installation.md
35+
rust: 1.54.0
3536
steps:
3637
- uses: actions/checkout@master
3738
- name: Install Rust

Cargo.lock

Lines changed: 29 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ description = "Creates a book from markdown files"
1818
[dependencies]
1919
anyhow = "1.0.28"
2020
chrono = "0.4"
21-
clap = "2.24"
21+
clap = { version = "3.0", features = ["cargo"] }
22+
clap_complete = "3.0"
2223
env_logger = "0.7.1"
2324
handlebars = "4.0"
2425
lazy_static = "1.0"

examples/nop-preprocessor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use crate::nop_lib::Nop;
2-
use clap::{App, Arg, ArgMatches, SubCommand};
2+
use clap::{App, Arg, ArgMatches};
33
use mdbook::book::Book;
44
use mdbook::errors::Error;
55
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext};
66
use semver::{Version, VersionReq};
77
use std::io;
88
use std::process;
99

10-
pub fn make_app() -> App<'static, 'static> {
10+
pub fn make_app() -> App<'static> {
1111
App::new("nop-preprocessor")
1212
.about("A mdbook preprocessor which does precisely nothing")
1313
.subcommand(
14-
SubCommand::with_name("supports")
15-
.arg(Arg::with_name("renderer").required(true))
14+
App::new("supports")
15+
.arg(Arg::new("renderer").required(true))
1616
.about("Check whether a renderer is supported by this preprocessor"),
1717
)
1818
}

guide/src/guide/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ To make it easier to run, put the path to the binary into your `PATH`.
2020

2121
To build the `mdbook` executable from source, you will first need to install Rust and Cargo.
2222
Follow the instructions on the [Rust installation page].
23-
mdBook currently requires at least Rust version 1.46.
23+
mdBook currently requires at least Rust version 1.54.
2424

2525
Once you have installed Rust, the following command can be used to build and install mdBook:
2626

src/cmd/build.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
use crate::{get_book_dir, open};
2-
use clap::{App, ArgMatches, SubCommand};
2+
use clap::{arg, App, Arg, ArgMatches};
33
use mdbook::errors::Result;
44
use mdbook::MDBook;
55

66
// Create clap subcommand arguments
7-
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
8-
SubCommand::with_name("build")
7+
pub fn make_subcommand<'help>() -> App<'help> {
8+
App::new("build")
99
.about("Builds a book from its markdown files")
10-
.arg_from_usage(
11-
"-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\
12-
Relative paths are interpreted relative to the book's root directory.{n}\
13-
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.'",
10+
.arg(
11+
Arg::new("dest-dir")
12+
.short('d')
13+
.long("dest-dir")
14+
.value_name("dest-dir")
15+
.help(
16+
"Output directory for the book{n}\
17+
Relative paths are interpreted relative to the book's root directory.{n}\
18+
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.",
19+
),
1420
)
15-
.arg_from_usage(
16-
"[dir] 'Root directory for the book{n}\
17-
(Defaults to the Current Directory when omitted)'",
18-
)
19-
.arg_from_usage("-o, --open 'Opens the compiled book in a web browser'")
21+
.arg(arg!([dir]
22+
"Root directory for the book{n}\
23+
(Defaults to the Current Directory when omitted)"
24+
))
25+
.arg(arg!(-o --open "Opens the compiled book in a web browser"))
2026
}
2127

2228
// Build command implementation

src/cmd/clean.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
use crate::get_book_dir;
22
use anyhow::Context;
3-
use clap::{App, ArgMatches, SubCommand};
3+
use clap::{arg, App, Arg, ArgMatches};
44
use mdbook::MDBook;
55
use std::fs;
66

77
// Create clap subcommand arguments
8-
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
9-
SubCommand::with_name("clean")
8+
pub fn make_subcommand<'help>() -> App<'help> {
9+
App::new("clean")
1010
.about("Deletes a built book")
11-
.arg_from_usage(
12-
"-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\
13-
Relative paths are interpreted relative to the book's root directory.{n}\
14-
Running this command deletes this directory.{n}\
15-
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.'",
16-
)
17-
.arg_from_usage(
18-
"[dir] 'Root directory for the book{n}\
19-
(Defaults to the Current Directory when omitted)'",
11+
.arg(
12+
Arg::new("dest-dir")
13+
.short('d')
14+
.long("dest-dir")
15+
.value_name("dest-dir")
16+
.help(
17+
"Output directory for the book{n}\
18+
Relative paths are interpreted relative to the book's root directory.{n}\
19+
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.",
20+
),
2021
)
22+
.arg(arg!([dir]
23+
"Root directory for the book{n}\
24+
(Defaults to the Current Directory when omitted)"
25+
))
2126
}
2227

2328
// Clean command implementation

src/cmd/init.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::get_book_dir;
2-
use clap::{App, Arg, ArgMatches, SubCommand};
2+
use clap::{arg, App, Arg, ArgMatches};
33
use mdbook::config;
44
use mdbook::errors::Result;
55
use mdbook::MDBook;
@@ -8,25 +8,25 @@ use std::io::Write;
88
use std::process::Command;
99

1010
// Create clap subcommand arguments
11-
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
12-
SubCommand::with_name("init")
11+
pub fn make_subcommand<'help>() -> App<'help> {
12+
App::new("init")
1313
.about("Creates the boilerplate structure and files for a new book")
1414
// the {n} denotes a newline which will properly aligned in all help messages
15-
.arg_from_usage(
16-
"[dir] 'Directory to create the book in{n}\
17-
(Defaults to the Current Directory when omitted)'",
18-
)
19-
.arg_from_usage("--theme 'Copies the default theme into your source folder'")
20-
.arg_from_usage("--force 'Skips confirmation prompts'")
15+
.arg(arg!([dir]
16+
"Directory to create the book in{n}\
17+
(Defaults to the Current Directory when omitted)"
18+
))
19+
.arg(arg!(--theme "Copies the default theme into your source folder"))
20+
.arg(arg!(--force "Skips confirmation prompts"))
2121
.arg(
22-
Arg::with_name("title")
22+
Arg::new("title")
2323
.long("title")
2424
.takes_value(true)
2525
.help("Sets the book title")
2626
.required(false),
2727
)
2828
.arg(
29-
Arg::with_name("ignore")
29+
Arg::new("ignore")
3030
.long("ignore")
3131
.takes_value(true)
3232
.possible_values(&["none", "git"])

src/cmd/serve.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(feature = "watch")]
22
use super::watch;
33
use crate::{get_book_dir, open};
4-
use clap::{App, Arg, ArgMatches, SubCommand};
4+
use clap::{arg, App, Arg, ArgMatches};
55
use futures_util::sink::SinkExt;
66
use futures_util::StreamExt;
77
use mdbook::errors::*;
@@ -18,37 +18,43 @@ use warp::Filter;
1818
const LIVE_RELOAD_ENDPOINT: &str = "__livereload";
1919

2020
// Create clap subcommand arguments
21-
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
22-
SubCommand::with_name("serve")
21+
pub fn make_subcommand<'help>() -> App<'help> {
22+
App::new("serve")
2323
.about("Serves a book at http://localhost:3000, and rebuilds it on changes")
24-
.arg_from_usage(
25-
"-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\
26-
Relative paths are interpreted relative to the book's root directory.{n}\
27-
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.'",
28-
)
29-
.arg_from_usage(
30-
"[dir] 'Root directory for the book{n}\
31-
(Defaults to the Current Directory when omitted)'",
24+
.arg(
25+
Arg::new("dest-dir")
26+
.short('d')
27+
.long("dest-dir")
28+
.value_name("dest-dir")
29+
.help(
30+
"Output directory for the book{n}\
31+
Relative paths are interpreted relative to the book's root directory.{n}\
32+
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.",
33+
),
3234
)
35+
.arg(arg!([dir]
36+
"Root directory for the book{n}\
37+
(Defaults to the Current Directory when omitted)"
38+
))
3339
.arg(
34-
Arg::with_name("hostname")
35-
.short("n")
40+
Arg::new("hostname")
41+
.short('n')
3642
.long("hostname")
3743
.takes_value(true)
3844
.default_value("localhost")
39-
.empty_values(false)
45+
.forbid_empty_values(true)
4046
.help("Hostname to listen on for HTTP connections"),
4147
)
4248
.arg(
43-
Arg::with_name("port")
44-
.short("p")
49+
Arg::new("port")
50+
.short('p')
4551
.long("port")
4652
.takes_value(true)
4753
.default_value("3000")
48-
.empty_values(false)
54+
.forbid_empty_values(true)
4955
.help("Port to use for HTTP connections"),
5056
)
51-
.arg_from_usage("-o, --open 'Opens the book server in a web browser'")
57+
.arg(arg!(-o --open "Opens the compiled book in a web browser"))
5258
}
5359

5460
// Serve command implementation

0 commit comments

Comments
 (0)