1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
16 use std::io::{self, Write};
17 use std::path::{Path, PathBuf};
19 use clap::{App, ArgMatches, SubCommand, AppSettings};
22 use mdbook::errors::Result;
25 let d_message = "-d, --dest-dir=[dest-dir]
26 'The output directory for your book{n}(Defaults to ./book when omitted)'";
27 let dir_message = "[dir]
28 'A directory for your book{n}(Defaults to Current Directory when omitted)'";
30 let matches = App::new("rustbook")
31 .about("Build a book with mdBook")
32 .author("Steve Klabnik <steve@steveklabnik.com>")
33 .version(&*format!("v{}", crate_version!()))
34 .setting(AppSettings::SubcommandRequired)
35 .subcommand(SubCommand::with_name("build")
36 .about("Build the book from the markdown files")
37 .arg_from_usage(d_message)
38 .arg_from_usage(dir_message))
41 // Check which subcomamnd the user ran...
42 let res = match matches.subcommand() {
43 ("build", Some(sub_matches)) => build(sub_matches),
44 (_, _) => unreachable!(),
48 writeln!(&mut io::stderr(), "An error occured:\n{}", e).ok();
49 ::std::process::exit(101);
52 // Build command implementation
53 pub fn build(args: &ArgMatches) -> Result<()> {
54 let book_dir = get_book_dir(args);
55 let book = MDBook::new(&book_dir).read_config()?;
57 let mut book = match args.value_of("dest-dir") {
58 Some(dest_dir) => book.with_destination(dest_dir),
67 fn get_book_dir(args: &ArgMatches) -> PathBuf {
68 if let Some(dir) = args.value_of("dir") {
69 // Check if path is relative from current dir, or absolute...
70 let p = Path::new(dir);
72 env::current_dir().unwrap().join(dir)
77 env::current_dir().unwrap()