From 7f51039f9a76b848dc50fa928455879758ba5520 Mon Sep 17 00:00:00 2001 From: Michal Budzynski Date: Tue, 27 Jun 2017 07:59:50 +0200 Subject: [PATCH] Rename and move the clap sub-command generation functions --- src/bin/build.rs | 23 ++++++++++++----------- src/bin/init.rs | 21 +++++++++++---------- src/bin/mdbook.rs | 10 +++++----- src/bin/serve.rs | 41 +++++++++++++++++++++-------------------- src/bin/test.rs | 11 ++++++----- src/bin/watch.rs | 21 +++++++++++---------- 6 files changed, 66 insertions(+), 61 deletions(-) diff --git a/src/bin/build.rs b/src/bin/build.rs index 1ee390a7..72251f8c 100644 --- a/src/bin/build.rs +++ b/src/bin/build.rs @@ -3,8 +3,19 @@ use mdbook::MDBook; use mdbook::errors::Result; use {get_book_dir, open}; +// Create clap subcommand arguments +pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("build") + .about("Build the book from the markdown files") + .arg_from_usage("-o, --open 'Open the compiled book in a web browser'") + .arg_from_usage("-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book when omitted)'") + .arg_from_usage("--no-create 'Will not create non-existent files linked from SUMMARY.md'") + .arg_from_usage("--curly-quotes 'Convert straight quotes to curly quotes, except for those that occur in code blocks and code spans'") + .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'") +} + // Build command implementation -pub fn build(args: &ArgMatches) -> Result<()> { +pub fn execute(args: &ArgMatches) -> Result<()> { let book_dir = get_book_dir(args); let book = MDBook::new(&book_dir).read_config()?; @@ -31,13 +42,3 @@ pub fn build(args: &ArgMatches) -> Result<()> { Ok(()) } - -pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { - SubCommand::with_name("build") - .about("Build the book from the markdown files") - .arg_from_usage("-o, --open 'Open the compiled book in a web browser'") - .arg_from_usage("-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book when omitted)'") - .arg_from_usage("--no-create 'Will not create non-existent files linked from SUMMARY.md'") - .arg_from_usage("--curly-quotes 'Convert straight quotes to curly quotes, except for those that occur in code blocks and code spans'") - .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'") -} diff --git a/src/bin/init.rs b/src/bin/init.rs index 5a445b9f..91224d0e 100644 --- a/src/bin/init.rs +++ b/src/bin/init.rs @@ -5,8 +5,18 @@ use mdbook::MDBook; use mdbook::errors::Result; use get_book_dir; +// Create clap subcommand arguments +pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("init") + .about("Create boilerplate structure and files in the directory") + // the {n} denotes a newline which will properly aligned in all help messages + .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'") + .arg_from_usage("--theme 'Copies the default theme into your source folder'") + .arg_from_usage("--force 'skip confirmation prompts'") +} + // Init command implementation -pub fn init(args: &ArgMatches) -> Result<()> { +pub fn execute(args: &ArgMatches) -> Result<()> { let book_dir = get_book_dir(args); let mut book = MDBook::new(&book_dir); @@ -67,12 +77,3 @@ fn confirm() -> bool { _ => false, } } - -pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { - SubCommand::with_name("init") - .about("Create boilerplate structure and files in the directory") - // the {n} denotes a newline which will properly aligned in all help messages - .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'") - .arg_from_usage("--theme 'Copies the default theme into your source folder'") - .arg_from_usage("--force 'skip confirmation prompts'") -} diff --git a/src/bin/mdbook.rs b/src/bin/mdbook.rs index 12ce8e6f..43fa0212 100644 --- a/src/bin/mdbook.rs +++ b/src/bin/mdbook.rs @@ -43,13 +43,13 @@ fn main() { // Check which subcomamnd the user ran... let res = match app.get_matches().subcommand() { - ("init", Some(sub_matches)) => init::init(sub_matches), - ("build", Some(sub_matches)) => build::build(sub_matches), + ("init", Some(sub_matches)) => init::execute(sub_matches), + ("build", Some(sub_matches)) => build::execute(sub_matches), #[cfg(feature = "watch")] - ("watch", Some(sub_matches)) => watch::watch(sub_matches), + ("watch", Some(sub_matches)) => watch::execute(sub_matches), #[cfg(feature = "serve")] - ("serve", Some(sub_matches)) => serve::serve(sub_matches), - ("test", Some(sub_matches)) => test::test(sub_matches), + ("serve", Some(sub_matches)) => serve::execute(sub_matches), + ("test", Some(sub_matches)) => test::execute(sub_matches), (_, _) => unreachable!(), }; diff --git a/src/bin/serve.rs b/src/bin/serve.rs index cc4eb8f1..63ffa860 100644 --- a/src/bin/serve.rs +++ b/src/bin/serve.rs @@ -14,18 +14,22 @@ use watch; struct ErrorRecover; -impl AfterMiddleware for ErrorRecover { - fn catch(&self, _: &mut Request, err: IronError) -> IronResult { - match err.response.status { - // each error will result in 404 response - Some(_) => Ok(err.response.set(status::NotFound)), - _ => Err(err), - } - } +// Create clap subcommand arguments +pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("serve") + .about("Serve the book at http://localhost:3000. Rebuild and reload on change.") + .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'") + .arg_from_usage("-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book when omitted)'") + .arg_from_usage("--curly-quotes 'Convert straight quotes to curly quotes, except for those that occur in code blocks and code spans'") + .arg_from_usage("-p, --port=[port] 'Use another port{n}(Defaults to 3000)'") + .arg_from_usage("-w, --websocket-port=[ws-port] 'Use another port for the websocket connection (livereload){n}(Defaults to 3001)'") + .arg_from_usage("-i, --interface=[interface] 'Interface to listen on{n}(Defaults to localhost)'") + .arg_from_usage("-a, --address=[address] 'Address that the browser can reach the websocket server from{n}(Defaults to the interface address)'") + .arg_from_usage("-o, --open 'Open the book server in a web browser'") } // Watch command implementation -pub fn serve(args: &ArgMatches) -> Result<()> { +pub fn execute(args: &ArgMatches) -> Result<()> { const RELOAD_COMMAND: &'static str = "reload"; let book_dir = get_book_dir(args); @@ -106,15 +110,12 @@ pub fn serve(args: &ArgMatches) -> Result<()> { Ok(()) } -pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { - SubCommand::with_name("serve") - .about("Serve the book at http://localhost:3000. Rebuild and reload on change.") - .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'") - .arg_from_usage("-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book when omitted)'") - .arg_from_usage("--curly-quotes 'Convert straight quotes to curly quotes, except for those that occur in code blocks and code spans'") - .arg_from_usage("-p, --port=[port] 'Use another port{n}(Defaults to 3000)'") - .arg_from_usage("-w, --websocket-port=[ws-port] 'Use another port for the websocket connection (livereload){n}(Defaults to 3001)'") - .arg_from_usage("-i, --interface=[interface] 'Interface to listen on{n}(Defaults to localhost)'") - .arg_from_usage("-a, --address=[address] 'Address that the browser can reach the websocket server from{n}(Defaults to the interface address)'") - .arg_from_usage("-o, --open 'Open the book server in a web browser'") +impl AfterMiddleware for ErrorRecover { + fn catch(&self, _: &mut Request, err: IronError) -> IronResult { + match err.response.status { + // each error will result in 404 response + Some(_) => Ok(err.response.set(status::NotFound)), + _ => Err(err), + } + } } diff --git a/src/bin/test.rs b/src/bin/test.rs index 77648bac..e2ce5471 100644 --- a/src/bin/test.rs +++ b/src/bin/test.rs @@ -3,8 +3,13 @@ use mdbook::MDBook; use mdbook::errors::Result; use get_book_dir; +// Create clap subcommand arguments +pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("test").about("Test that code samples compile") +} + // test command implementation -pub fn test(args: &ArgMatches) -> Result<()> { +pub fn execute(args: &ArgMatches) -> Result<()> { let book_dir = get_book_dir(args); let mut book = MDBook::new(&book_dir).read_config()?; @@ -12,7 +17,3 @@ pub fn test(args: &ArgMatches) -> Result<()> { Ok(()) } - -pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { - SubCommand::with_name("test").about("Test that code samples compile") -} diff --git a/src/bin/watch.rs b/src/bin/watch.rs index 3a965d1d..8b520194 100644 --- a/src/bin/watch.rs +++ b/src/bin/watch.rs @@ -11,8 +11,18 @@ use mdbook::MDBook; use mdbook::errors::Result; use {get_book_dir, open}; +// Create clap subcommand arguments +pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("watch") + .about("Watch the files for changes") + .arg_from_usage("-o, --open 'Open the compiled book in a web browser'") + .arg_from_usage("-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book when omitted)'") + .arg_from_usage("--curly-quotes 'Convert straight quotes to curly quotes, except for those that occur in code blocks and code spans'") + .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'") +} + // Watch command implementation -pub fn watch(args: &ArgMatches) -> Result<()> { +pub fn execute(args: &ArgMatches) -> Result<()> { let book_dir = get_book_dir(args); let book = MDBook::new(&book_dir).read_config()?; @@ -109,12 +119,3 @@ pub fn trigger_on_change(book: &mut MDBook, closure: F) -> () } } } - -pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { - SubCommand::with_name("watch") - .about("Watch the files for changes") - .arg_from_usage("-o, --open 'Open the compiled book in a web browser'") - .arg_from_usage("-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book when omitted)'") - .arg_from_usage("--curly-quotes 'Convert straight quotes to curly quotes, except for those that occur in code blocks and code spans'") - .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'") -}