Skip to content

Commit 18f9f78

Browse files
committed
add init flags
1 parent 22ea5fe commit 18f9f78

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/cmd/init.rs

Lines changed: 26 additions & 6 deletions
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)