Skip to content

Commit e5e10c6

Browse files
committed
add init flags
add init flags update init command in guide
1 parent 22ea5fe commit e5e10c6

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
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+
#### --gitignore
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

+26-6
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,27 @@ 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+
.short("t")
24+
.long("title")
25+
.takes_value(true)
26+
.help("Sets the book title")
27+
.required(false),
28+
)
29+
.arg(
30+
Arg::with_name("gitignore")
31+
.short("g")
32+
.long("gitignore")
33+
.help("Creates a .gitignore"),
34+
)
2135
}
2236

2337
// Init command implementation
2438
pub fn execute(args: &ArgMatches) -> Result<()> {
2539
let book_dir = get_book_dir(args);
2640
let mut builder = MDBook::init(&book_dir);
2741
let mut config = config::Config::default();
28-
2942
// If flag `--theme` is present, copy theme to src
3043
if args.is_present("theme") {
3144
let theme_dir = book_dir.join("theme");
@@ -45,13 +58,20 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
4558
}
4659
}
4760

48-
println!("\nDo you want a .gitignore to be created? (y/n)");
49-
50-
if confirm() {
61+
if args.is_present("gitignore") {
5162
builder.create_gitignore(true);
63+
} else {
64+
println!("\nDo you want a .gitignore to be created? (y/n)");
65+
if confirm() {
66+
builder.create_gitignore(true);
67+
}
5268
}
5369

54-
config.book.title = request_book_title();
70+
config.book.title = if args.is_present("title") {
71+
args.value_of("title").map(String::from)
72+
} else {
73+
request_book_title()
74+
};
5575

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

0 commit comments

Comments
 (0)