Repository: rust-lang/mdBook (language: rust)

Issue:
mdbook init force flag documentation is wrong
The documentation for the force flag for the `init` subcommand states "Skips confirmation prompts", but it actually only affects whether a prompt is given if the `--theme` flag is present and the theme directory exists. It has no effect on the prompts for creating the `.gitignore` or getting the book name (which, ok, isn't technically a "confirmation" prompt). 

The easiest fix is to just update the documentation. Another fix might be to make the `.gitignore` prompt also respect the flag so that the "confirmation" part is technically right. It might also be helpful to add a flag for creating the `.gitignore` and the book title too to round things out and make book initialization fully prompt free if someone desires.

Something like this. If `--force` is specified, in addition to the current behavior, init the book with the defaults for `--with-gitignore` and `--with-title`.

```bash
mdbook-init v0.4.8
Creates the boilerplate structure and files for a new book

USAGE:
    mdbook init [FLAGS] [dir]

FLAGS:
        --force             Skips confirmation prompts
    -h, --help              Prints help information
        --theme             Copies the default theme into your source folder
    -V, --version           Prints version information
        --with-gitignore    Creates a .gitignore file [default: true]
        --with-title        Set the book title [default: "my book"]

ARGS:
    <dir>    Directory to create the book in
             (Defaults to the Current Directory when omitted)
```

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

<!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":"seanpoulter"}$$TRIAGEBOT_ASSIGN_DATA_END -->

<!-- TRIAGEBOT_ASSIGN_END -->
<!-- TRIAGEBOT_END -->


Discussion / hints:
Hm, looks like this was changed as part of #491, but there doesn't seem to be an explanation why.  I would say the original intent is that it avoids all prompts.  I agree that `--force` should probably not do any prompts.


Thoughts on adding the extra flags as well to make it possible to supply an answer for book name and .gitignore creation as part of the fix here? Or do you prefer to just make force do what it says and use defaults for those (and maybe call them out in the documentation string)?
Preferably have separate PRs.

As for the flags, how about `--title=...` and `--ignore=git` or `--ignore=none`?

> As for the flags, how about `--title=...` and `--ignore=git` or `--ignore=none`?

I went with `--gitignore` as a boolean. If we wanted to support other `.ignore` file types, though, `--ignore=<type>` is probably better ... but the only other one I know is `.hgignore`.

The next step would be to set defaults and check `--force` first for both args:

* if `--force`, set default
* if `is_present`, set from command line value
* otherwise prompt

What's the status on this?
I just found this issue. Would you welcome a PR? What default would you want for the title, `Example book` like the docs?
@rustbot claim
I'm not certain about the defaults, but I'll suggest:

* Assume `--ignore=none`
* Set `BookConfig.title` to None (no title)

Thanks @ehuss. I've got a PR #2057 up.

Fix patch:
diff --git a/guide/src/cli/init.md b/guide/src/cli/init.md
index 99c0be09a6..5d0f903287 100644
--- a/guide/src/cli/init.md
+++ b/guide/src/cli/init.md
@@ -68,3 +68,7 @@ Create a `.gitignore` file configured to ignore the `book` directory created whe
 If not supplied, an interactive prompt will ask whether it should be created.
 
 [building]: build.md
+
+#### --force
+
+Skip the prompts to create a `.gitignore` and for the title for the book.
diff --git a/src/cmd/init.rs b/src/cmd/init.rs
index d8ce93d162..3a60d97586 100644
--- a/src/cmd/init.rs
+++ b/src/cmd/init.rs
@@ -56,7 +56,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
             "git" => builder.create_gitignore(true),
             _ => builder.create_gitignore(false),
         };
-    } else {
+    } else if !args.get_flag("force") {
         println!("\nDo you want a .gitignore to be created? (y/n)");
         if confirm() {
             builder.create_gitignore(true);
@@ -65,6 +65,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
 
     config.book.title = if args.contains_id("title") {
         args.get_one::<String>("title").map(String::from)
+    } else if args.get_flag("force") {
+        None
     } else {
         request_book_title()
     };


Test patch:
diff --git a/tests/cli/init.rs b/tests/cli/init.rs
new file mode 100644
index 0000000000..6bd1227437
--- /dev/null
+++ b/tests/cli/init.rs
@@ -0,0 +1,24 @@
+use crate::cli::cmd::mdbook_cmd;
+use crate::dummy_book::DummyBook;
+
+use mdbook::config::Config;
+
+/// Run `mdbook init` with `--force` to skip the confirmation prompts
+#[test]
+fn base_mdbook_init_can_skip_confirmation_prompts() {
+    let temp = DummyBook::new().build().unwrap();
+
+    // doesn't exist before
+    assert!(!temp.path().join("book").exists());
+
+    let mut cmd = mdbook_cmd();
+    cmd.args(["init", "--force"]).current_dir(temp.path());
+    cmd.assert()
+        .success()
+        .stdout(predicates::str::contains("\nAll done, no errors...\n"));
+
+    let config = Config::from_disk(temp.path().join("book.toml")).unwrap();
+    assert_eq!(config.book.title, None);
+
+    assert!(!temp.path().join(".gitignore").exists());
+}
diff --git a/tests/cli/mod.rs b/tests/cli/mod.rs
index 989f443f02..152b4ee91b 100644
--- a/tests/cli/mod.rs
+++ b/tests/cli/mod.rs
@@ -1,3 +1,4 @@
 mod build;
 mod cmd;
+mod init;
 mod test;