diff --git a/src/bin/mdbook.rs b/src/bin/mdbook.rs index 44c16b21..5df36c8e 100644 --- a/src/bin/mdbook.rs +++ b/src/bin/mdbook.rs @@ -26,11 +26,11 @@ fn main() { env_logger::init().unwrap(); // Create a list of valid arguments and sub-commands - let matches = App::new(NAME) + let app = App::new(NAME) .about("Create a book in form of a static website from markdown files") .author("Mathieu David ") // Get the version from our Cargo.toml using clap's crate_version!() macro - .version(&*format!("v{}", crate_version!())) + .version(concat!("v",crate_version!())) .setting(AppSettings::SubcommandRequired) .after_help("For more information about a specific command, try `mdbook --help`\nSource code for mdbook available at: https://github.com/azerupi/mdBook") .subcommand(SubCommand::with_name("init") @@ -46,28 +46,16 @@ fn main() { .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)'")) - .subcommand(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)'")) - .subcommand(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'")) .subcommand(SubCommand::with_name("test") - .about("Test that code samples compile")) - .get_matches(); + .about("Test that code samples compile")); + + #[cfg(feature = "watch")] + let app = app.subcommand(watch::make_subcommand()); + #[cfg(feature = "serve")] + let app = app.subcommand(serve::make_subcommand()); // Check which subcomamnd the user ran... - let res = match matches.subcommand() { + let res = match app.get_matches().subcommand() { ("init", Some(sub_matches)) => init(sub_matches), ("build", Some(sub_matches)) => build(sub_matches), #[cfg(feature = "watch")] diff --git a/src/bin/serve.rs b/src/bin/serve.rs index 2cd08ab3..3c99c940 100644 --- a/src/bin/serve.rs +++ b/src/bin/serve.rs @@ -6,7 +6,7 @@ use std; use std::path::Path; use std::error::Error; use self::iron::{Iron, AfterMiddleware, IronResult, IronError, Request, Response, status, Set, Chain}; -use clap::ArgMatches; +use clap::{ArgMatches, SubCommand, App}; use mdbook::MDBook; use {get_book_dir, open}; @@ -107,3 +107,16 @@ pub fn serve(args: &ArgMatches) -> Result<(), Box> { 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'") +} diff --git a/src/bin/watch.rs b/src/bin/watch.rs index 79f9439c..b7aaf584 100644 --- a/src/bin/watch.rs +++ b/src/bin/watch.rs @@ -8,7 +8,7 @@ use std::error::Error; use self::notify::Watcher; use std::time::Duration; use std::sync::mpsc::channel; -use clap::ArgMatches; +use clap::{ArgMatches, SubCommand, App}; use mdbook::MDBook; use {get_book_dir, open}; @@ -111,3 +111,12 @@ 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)'") +}