From e5e10c681a98249fd433825dfdcbd69b014f00a1 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Mon, 31 May 2021 19:43:15 -0700 Subject: [PATCH 1/5] add init flags add init flags update init command in guide --- guide/src/cli/init.md | 16 ++++++++++++++++ src/cmd/init.rs | 32 ++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/guide/src/cli/init.md b/guide/src/cli/init.md index a0a407c5..4ac42fcc 100644 --- a/guide/src/cli/init.md +++ b/guide/src/cli/init.md @@ -52,3 +52,19 @@ directory called `theme` in your source directory so that you can modify it. The theme is selectively overwritten, this means that if you don't want to overwrite a specific file, just delete it and the default file will be used. + +#### --title + +Specify a title for the book. If not supplied, an interactive prompt will ask for +a title. + +```bash +mdbook init --title="my amazing book" +``` + +#### --gitignore + +Create a `.gitignore` file configured to ignore the `book` directory created when [building] a book. +If not supplied, an interactive prompt will ask whether it should be created. + +[building]: build.md diff --git a/src/cmd/init.rs b/src/cmd/init.rs index cccc1671..b7f9f4d6 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,5 +1,5 @@ use crate::get_book_dir; -use clap::{App, ArgMatches, SubCommand}; +use clap::{App, Arg, ArgMatches, SubCommand}; use mdbook::config; use mdbook::errors::Result; use mdbook::MDBook; @@ -18,6 +18,20 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { ) .arg_from_usage("--theme 'Copies the default theme into your source folder'") .arg_from_usage("--force 'Skips confirmation prompts'") + .arg( + Arg::with_name("title") + .short("t") + .long("title") + .takes_value(true) + .help("Sets the book title") + .required(false), + ) + .arg( + Arg::with_name("gitignore") + .short("g") + .long("gitignore") + .help("Creates a .gitignore"), + ) } // Init command implementation @@ -25,7 +39,6 @@ pub fn execute(args: &ArgMatches) -> Result<()> { let book_dir = get_book_dir(args); let mut builder = MDBook::init(&book_dir); let mut config = config::Config::default(); - // If flag `--theme` is present, copy theme to src if args.is_present("theme") { let theme_dir = book_dir.join("theme"); @@ -45,13 +58,20 @@ pub fn execute(args: &ArgMatches) -> Result<()> { } } - println!("\nDo you want a .gitignore to be created? (y/n)"); - - if confirm() { + if args.is_present("gitignore") { builder.create_gitignore(true); + } else { + println!("\nDo you want a .gitignore to be created? (y/n)"); + if confirm() { + builder.create_gitignore(true); + } } - config.book.title = request_book_title(); + config.book.title = if args.is_present("title") { + args.value_of("title").map(String::from) + } else { + request_book_title() + }; if let Some(author) = get_author_name() { debug!("Obtained user name from gitconfig: {:?}", author); From 86d390032be3afc8ea5306962fb51ea723890cf3 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Sun, 4 Jul 2021 08:15:51 -0700 Subject: [PATCH 2/5] drop short flags --- src/cmd/init.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/cmd/init.rs b/src/cmd/init.rs index b7f9f4d6..68a1145b 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -20,7 +20,6 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { .arg_from_usage("--force 'Skips confirmation prompts'") .arg( Arg::with_name("title") - .short("t") .long("title") .takes_value(true) .help("Sets the book title") @@ -28,7 +27,6 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { ) .arg( Arg::with_name("gitignore") - .short("g") .long("gitignore") .help("Creates a .gitignore"), ) From 8ec0bf6e30545defd9e81f1775740b03df2da544 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Sun, 4 Jul 2021 11:57:46 -0700 Subject: [PATCH 3/5] ignore now takes a value --- src/cmd/init.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 68a1145b..010e4003 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -26,9 +26,12 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { .required(false), ) .arg( - Arg::with_name("gitignore") - .long("gitignore") - .help("Creates a .gitignore"), + Arg::with_name("ignore") + .long("ignore") + .takes_value(true) + .possible_values(&["none", "git"]) + .help("Creates a VCS ignore file (i.e. .gitignore)") + .required(false), ) } @@ -56,8 +59,11 @@ pub fn execute(args: &ArgMatches) -> Result<()> { } } - if args.is_present("gitignore") { - builder.create_gitignore(true); + if let Some(ignore) = args.value_of("ignore") { + match ignore { + "git" => builder.create_gitignore(true), + _ => builder.create_gitignore(false) + }; } else { println!("\nDo you want a .gitignore to be created? (y/n)"); if confirm() { From 8024b08f93bb3bcb4e94f6ced0d335169c604016 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Sun, 4 Jul 2021 12:00:13 -0700 Subject: [PATCH 4/5] format --- src/cmd/init.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 010e4003..ed0aa17d 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -62,7 +62,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> { if let Some(ignore) = args.value_of("ignore") { match ignore { "git" => builder.create_gitignore(true), - _ => builder.create_gitignore(false) + _ => builder.create_gitignore(false), }; } else { println!("\nDo you want a .gitignore to be created? (y/n)"); From c3a1e41ed72b469d7694d3df2e84e13238f9c2e1 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 6 Jul 2021 09:22:05 -0700 Subject: [PATCH 5/5] Update CLI docs for flag name change. --- guide/src/cli/init.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guide/src/cli/init.md b/guide/src/cli/init.md index 4ac42fcc..99c0be09 100644 --- a/guide/src/cli/init.md +++ b/guide/src/cli/init.md @@ -62,7 +62,7 @@ a title. mdbook init --title="my amazing book" ``` -#### --gitignore +#### --ignore Create a `.gitignore` file configured to ignore the `book` directory created when [building] a book. If not supplied, an interactive prompt will ask whether it should be created.