Rename and move the clap sub-command generation functions
This commit is contained in:
parent
7799ce285e
commit
7f51039f9a
|
@ -3,8 +3,19 @@ use mdbook::MDBook;
|
||||||
use mdbook::errors::Result;
|
use mdbook::errors::Result;
|
||||||
use {get_book_dir, open};
|
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
|
// Build command implementation
|
||||||
pub fn build(args: &ArgMatches) -> Result<()> {
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
||||||
let book_dir = get_book_dir(args);
|
let book_dir = get_book_dir(args);
|
||||||
let book = MDBook::new(&book_dir).read_config()?;
|
let book = MDBook::new(&book_dir).read_config()?;
|
||||||
|
|
||||||
|
@ -31,13 +42,3 @@ pub fn build(args: &ArgMatches) -> Result<()> {
|
||||||
|
|
||||||
Ok(())
|
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)'")
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,8 +5,18 @@ use mdbook::MDBook;
|
||||||
use mdbook::errors::Result;
|
use mdbook::errors::Result;
|
||||||
use get_book_dir;
|
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
|
// Init command implementation
|
||||||
pub fn init(args: &ArgMatches) -> Result<()> {
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
||||||
|
|
||||||
let book_dir = get_book_dir(args);
|
let book_dir = get_book_dir(args);
|
||||||
let mut book = MDBook::new(&book_dir);
|
let mut book = MDBook::new(&book_dir);
|
||||||
|
@ -67,12 +77,3 @@ fn confirm() -> bool {
|
||||||
_ => false,
|
_ => 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'")
|
|
||||||
}
|
|
||||||
|
|
|
@ -43,13 +43,13 @@ fn main() {
|
||||||
|
|
||||||
// Check which subcomamnd the user ran...
|
// Check which subcomamnd the user ran...
|
||||||
let res = match app.get_matches().subcommand() {
|
let res = match app.get_matches().subcommand() {
|
||||||
("init", Some(sub_matches)) => init::init(sub_matches),
|
("init", Some(sub_matches)) => init::execute(sub_matches),
|
||||||
("build", Some(sub_matches)) => build::build(sub_matches),
|
("build", Some(sub_matches)) => build::execute(sub_matches),
|
||||||
#[cfg(feature = "watch")]
|
#[cfg(feature = "watch")]
|
||||||
("watch", Some(sub_matches)) => watch::watch(sub_matches),
|
("watch", Some(sub_matches)) => watch::execute(sub_matches),
|
||||||
#[cfg(feature = "serve")]
|
#[cfg(feature = "serve")]
|
||||||
("serve", Some(sub_matches)) => serve::serve(sub_matches),
|
("serve", Some(sub_matches)) => serve::execute(sub_matches),
|
||||||
("test", Some(sub_matches)) => test::test(sub_matches),
|
("test", Some(sub_matches)) => test::execute(sub_matches),
|
||||||
(_, _) => unreachable!(),
|
(_, _) => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -14,18 +14,22 @@ use watch;
|
||||||
|
|
||||||
struct ErrorRecover;
|
struct ErrorRecover;
|
||||||
|
|
||||||
impl AfterMiddleware for ErrorRecover {
|
// Create clap subcommand arguments
|
||||||
fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
|
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
||||||
match err.response.status {
|
SubCommand::with_name("serve")
|
||||||
// each error will result in 404 response
|
.about("Serve the book at http://localhost:3000. Rebuild and reload on change.")
|
||||||
Some(_) => Ok(err.response.set(status::NotFound)),
|
.arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'")
|
||||||
_ => Err(err),
|
.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
|
// Watch command implementation
|
||||||
pub fn serve(args: &ArgMatches) -> Result<()> {
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
||||||
const RELOAD_COMMAND: &'static str = "reload";
|
const RELOAD_COMMAND: &'static str = "reload";
|
||||||
|
|
||||||
let book_dir = get_book_dir(args);
|
let book_dir = get_book_dir(args);
|
||||||
|
@ -106,15 +110,12 @@ pub fn serve(args: &ArgMatches) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
impl AfterMiddleware for ErrorRecover {
|
||||||
SubCommand::with_name("serve")
|
fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
|
||||||
.about("Serve the book at http://localhost:3000. Rebuild and reload on change.")
|
match err.response.status {
|
||||||
.arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'")
|
// each error will result in 404 response
|
||||||
.arg_from_usage("-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book when omitted)'")
|
Some(_) => Ok(err.response.set(status::NotFound)),
|
||||||
.arg_from_usage("--curly-quotes 'Convert straight quotes to curly quotes, except for those that occur in code blocks and code spans'")
|
_ => Err(err),
|
||||||
.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'")
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,13 @@ use mdbook::MDBook;
|
||||||
use mdbook::errors::Result;
|
use mdbook::errors::Result;
|
||||||
use get_book_dir;
|
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
|
// test command implementation
|
||||||
pub fn test(args: &ArgMatches) -> Result<()> {
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
||||||
let book_dir = get_book_dir(args);
|
let book_dir = get_book_dir(args);
|
||||||
let mut book = MDBook::new(&book_dir).read_config()?;
|
let mut book = MDBook::new(&book_dir).read_config()?;
|
||||||
|
|
||||||
|
@ -12,7 +17,3 @@ pub fn test(args: &ArgMatches) -> Result<()> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
|
||||||
SubCommand::with_name("test").about("Test that code samples compile")
|
|
||||||
}
|
|
||||||
|
|
|
@ -11,8 +11,18 @@ use mdbook::MDBook;
|
||||||
use mdbook::errors::Result;
|
use mdbook::errors::Result;
|
||||||
use {get_book_dir, open};
|
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
|
// Watch command implementation
|
||||||
pub fn watch(args: &ArgMatches) -> Result<()> {
|
pub fn execute(args: &ArgMatches) -> Result<()> {
|
||||||
let book_dir = get_book_dir(args);
|
let book_dir = get_book_dir(args);
|
||||||
let book = MDBook::new(&book_dir).read_config()?;
|
let book = MDBook::new(&book_dir).read_config()?;
|
||||||
|
|
||||||
|
@ -109,12 +119,3 @@ pub fn trigger_on_change<F>(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)'")
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue