Skip to content

Commit 56652e8

Browse files
authored
Merge pull request #1559 from joshrotenberg/title_ignore_flags_init
Add --title option and --gitignore flag to mdbook init
2 parents 3976c9d + c3a1e41 commit 56652e8

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

guide/src/cli/init.md

+16
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,19 @@ directory called `theme` in your source directory so that you can modify it.
5252

5353
The theme is selectively overwritten, this means that if you don't want to
5454
overwrite a specific file, just delete it and the default file will be used.
55+
56+
#### --title
57+
58+
Specify a title for the book. If not supplied, an interactive prompt will ask for
59+
a title.
60+
61+
```bash
62+
mdbook init --title="my amazing book"
63+
```
64+
65+
#### --ignore
66+
67+
Create a `.gitignore` file configured to ignore the `book` directory created when [building] a book.
68+
If not supplied, an interactive prompt will ask whether it should be created.
69+
70+
[building]: build.md

src/cmd/init.rs

+31-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::get_book_dir;
2-
use clap::{App, ArgMatches, SubCommand};
2+
use clap::{App, Arg, ArgMatches, SubCommand};
33
use mdbook::config;
44
use mdbook::errors::Result;
55
use mdbook::MDBook;
@@ -18,14 +18,28 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
1818
)
1919
.arg_from_usage("--theme 'Copies the default theme into your source folder'")
2020
.arg_from_usage("--force 'Skips confirmation prompts'")
21+
.arg(
22+
Arg::with_name("title")
23+
.long("title")
24+
.takes_value(true)
25+
.help("Sets the book title")
26+
.required(false),
27+
)
28+
.arg(
29+
Arg::with_name("ignore")
30+
.long("ignore")
31+
.takes_value(true)
32+
.possible_values(&["none", "git"])
33+
.help("Creates a VCS ignore file (i.e. .gitignore)")
34+
.required(false),
35+
)
2136
}
2237

2338
// Init command implementation
2439
pub fn execute(args: &ArgMatches) -> Result<()> {
2540
let book_dir = get_book_dir(args);
2641
let mut builder = MDBook::init(&book_dir);
2742
let mut config = config::Config::default();
28-
2943
// If flag `--theme` is present, copy theme to src
3044
if args.is_present("theme") {
3145
let theme_dir = book_dir.join("theme");
@@ -45,13 +59,23 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
4559
}
4660
}
4761

48-
println!("\nDo you want a .gitignore to be created? (y/n)");
49-
50-
if confirm() {
51-
builder.create_gitignore(true);
62+
if let Some(ignore) = args.value_of("ignore") {
63+
match ignore {
64+
"git" => builder.create_gitignore(true),
65+
_ => builder.create_gitignore(false),
66+
};
67+
} else {
68+
println!("\nDo you want a .gitignore to be created? (y/n)");
69+
if confirm() {
70+
builder.create_gitignore(true);
71+
}
5272
}
5373

54-
config.book.title = request_book_title();
74+
config.book.title = if args.is_present("title") {
75+
args.value_of("title").map(String::from)
76+
} else {
77+
request_book_title()
78+
};
5579

5680
if let Some(author) = get_author_name() {
5781
debug!("Obtained user name from gitconfig: {:?}", author);

0 commit comments

Comments
 (0)