2025-07-21 10:14:25 -07:00
|
|
|
//! The mdbook CLI.
|
|
|
|
|
|
2025-07-25 09:02:55 -07:00
|
|
|
#![allow(unreachable_pub, reason = "not needed in a bin crate")]
|
|
|
|
|
|
2021-01-04 23:51:02 +02:00
|
|
|
use anyhow::anyhow;
|
2022-07-04 23:16:31 +08:00
|
|
|
use clap::{Arg, ArgMatches, Command};
|
2022-01-18 15:56:45 -06:00
|
|
|
use clap_complete::Shell;
|
2025-07-21 12:20:21 -07:00
|
|
|
use mdbook_core::utils;
|
2015-07-06 21:12:24 +02:00
|
|
|
use std::env;
|
2017-01-01 09:42:47 -08:00
|
|
|
use std::ffi::OsStr;
|
2022-07-04 23:16:31 +08:00
|
|
|
use std::path::PathBuf;
|
2025-09-12 06:13:45 -07:00
|
|
|
use tracing::{error, info};
|
2015-07-06 21:12:24 +02:00
|
|
|
|
2018-07-24 16:34:49 -05:00
|
|
|
mod cmd;
|
2015-11-09 14:31:00 +01:00
|
|
|
|
2025-09-12 06:16:58 -07:00
|
|
|
const VERSION: &str = concat!("v", clap::crate_version!());
|
2015-07-06 21:12:24 +02:00
|
|
|
|
|
|
|
|
fn main() {
|
2017-06-28 23:37:03 +02:00
|
|
|
init_logger();
|
2016-08-14 14:55:10 +01:00
|
|
|
|
2022-07-04 23:16:31 +08:00
|
|
|
let command = create_clap_command();
|
2021-01-04 23:51:02 +02:00
|
|
|
|
2022-07-04 23:16:31 +08:00
|
|
|
// Check which subcommand the user ran...
|
|
|
|
|
let res = match command.get_matches().subcommand() {
|
2022-01-18 15:56:45 -06:00
|
|
|
Some(("init", sub_matches)) => cmd::init::execute(sub_matches),
|
|
|
|
|
Some(("build", sub_matches)) => cmd::build::execute(sub_matches),
|
|
|
|
|
Some(("clean", sub_matches)) => cmd::clean::execute(sub_matches),
|
2021-01-04 23:51:02 +02:00
|
|
|
#[cfg(feature = "watch")]
|
2022-01-18 15:56:45 -06:00
|
|
|
Some(("watch", sub_matches)) => cmd::watch::execute(sub_matches),
|
2021-01-04 23:51:02 +02:00
|
|
|
#[cfg(feature = "serve")]
|
2022-01-18 15:56:45 -06:00
|
|
|
Some(("serve", sub_matches)) => cmd::serve::execute(sub_matches),
|
|
|
|
|
Some(("test", sub_matches)) => cmd::test::execute(sub_matches),
|
|
|
|
|
Some(("completions", sub_matches)) => (|| {
|
2022-07-04 23:16:31 +08:00
|
|
|
let shell = sub_matches
|
|
|
|
|
.get_one::<Shell>("shell")
|
|
|
|
|
.ok_or_else(|| anyhow!("Shell name missing."))?;
|
2021-01-04 23:51:02 +02:00
|
|
|
|
2022-07-04 23:16:31 +08:00
|
|
|
let mut complete_app = create_clap_command();
|
2022-01-18 15:56:45 -06:00
|
|
|
clap_complete::generate(
|
2022-07-04 23:16:31 +08:00
|
|
|
*shell,
|
2022-01-18 15:56:45 -06:00
|
|
|
&mut complete_app,
|
|
|
|
|
"mdbook",
|
|
|
|
|
&mut std::io::stdout().lock(),
|
|
|
|
|
);
|
2021-01-04 23:51:02 +02:00
|
|
|
Ok(())
|
|
|
|
|
})(),
|
2022-01-18 15:56:45 -06:00
|
|
|
_ => unreachable!(),
|
2021-01-04 23:51:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
|
utils::log_backtrace(&e);
|
|
|
|
|
|
|
|
|
|
std::process::exit(101);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a list of valid arguments and sub-commands
|
2022-07-04 23:16:31 +08:00
|
|
|
fn create_clap_command() -> Command {
|
2025-09-12 06:16:58 -07:00
|
|
|
let app = Command::new(clap::crate_name!())
|
|
|
|
|
.about(clap::crate_description!())
|
2018-08-02 15:48:22 -05:00
|
|
|
.author("Mathieu David <mathieudavid@mathieudavid.org>")
|
|
|
|
|
.version(VERSION)
|
2022-07-04 23:16:31 +08:00
|
|
|
.propagate_version(true)
|
|
|
|
|
.arg_required_else_help(true)
|
2018-08-02 15:48:22 -05:00
|
|
|
.after_help(
|
|
|
|
|
"For more information about a specific command, try `mdbook <command> --help`\n\
|
2019-10-29 08:04:16 -05:00
|
|
|
The source code for mdBook is available at: https://github.com/rust-lang/mdBook",
|
2018-08-02 15:48:22 -05:00
|
|
|
)
|
|
|
|
|
.subcommand(cmd::init::make_subcommand())
|
|
|
|
|
.subcommand(cmd::build::make_subcommand())
|
|
|
|
|
.subcommand(cmd::test::make_subcommand())
|
2021-01-04 23:51:02 +02:00
|
|
|
.subcommand(cmd::clean::make_subcommand())
|
|
|
|
|
.subcommand(
|
2022-07-04 23:16:31 +08:00
|
|
|
Command::new("completions")
|
2021-01-04 23:51:02 +02:00
|
|
|
.about("Generate shell completions for your shell to stdout")
|
|
|
|
|
.arg(
|
2022-01-18 16:17:36 -06:00
|
|
|
Arg::new("shell")
|
2022-07-04 23:16:31 +08:00
|
|
|
.value_parser(clap::value_parser!(Shell))
|
2021-01-04 23:51:02 +02:00
|
|
|
.help("the shell to generate completions for")
|
|
|
|
|
.value_name("SHELL")
|
|
|
|
|
.required(true),
|
|
|
|
|
),
|
|
|
|
|
);
|
2017-06-26 00:31:42 +02:00
|
|
|
|
|
|
|
|
#[cfg(feature = "watch")]
|
2018-07-24 16:34:49 -05:00
|
|
|
let app = app.subcommand(cmd::watch::make_subcommand());
|
2017-06-26 00:31:42 +02:00
|
|
|
#[cfg(feature = "serve")]
|
2018-07-24 16:34:49 -05:00
|
|
|
let app = app.subcommand(cmd::serve::make_subcommand());
|
2015-08-01 00:59:05 -04:00
|
|
|
|
2021-01-04 23:51:02 +02:00
|
|
|
app
|
2015-07-06 21:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-28 23:37:03 +02:00
|
|
|
fn init_logger() {
|
2025-09-12 06:13:45 -07:00
|
|
|
let filter = tracing_subscriber::EnvFilter::builder()
|
|
|
|
|
.with_env_var("MDBOOK_LOG")
|
|
|
|
|
.with_default_directive(tracing_subscriber::filter::LevelFilter::INFO.into())
|
|
|
|
|
.from_env_lossy();
|
|
|
|
|
let log_env = std::env::var("MDBOOK_LOG");
|
|
|
|
|
// Silence some particularly noisy dependencies unless the user
|
|
|
|
|
// specifically asks for them.
|
|
|
|
|
let silence_unless_specified = |filter: tracing_subscriber::EnvFilter, target| {
|
|
|
|
|
if !log_env.as_ref().map_or(false, |s| {
|
|
|
|
|
s.split(',').any(|directive| directive.starts_with(target))
|
|
|
|
|
}) {
|
|
|
|
|
filter.add_directive(format!("{target}=warn").parse().unwrap())
|
|
|
|
|
} else {
|
|
|
|
|
filter
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let filter = silence_unless_specified(filter, "handlebars");
|
|
|
|
|
let filter = silence_unless_specified(filter, "html5ever");
|
|
|
|
|
|
|
|
|
|
// Don't show the target by default, since it generally isn't useful
|
|
|
|
|
// unless you are overriding the level.
|
|
|
|
|
let with_target = log_env.is_ok();
|
|
|
|
|
|
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
|
.without_time()
|
|
|
|
|
.with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr()))
|
|
|
|
|
.with_writer(std::io::stderr)
|
|
|
|
|
.with_env_filter(filter)
|
|
|
|
|
.with_target(with_target)
|
|
|
|
|
.init();
|
2017-06-28 23:37:03 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-01 00:59:05 -04:00
|
|
|
fn get_book_dir(args: &ArgMatches) -> PathBuf {
|
2022-07-04 23:16:31 +08:00
|
|
|
if let Some(p) = args.get_one::<PathBuf>("dir") {
|
2015-08-01 00:59:05 -04:00
|
|
|
// Check if path is relative from current dir, or absolute...
|
|
|
|
|
if p.is_relative() {
|
2022-07-04 23:16:31 +08:00
|
|
|
env::current_dir().unwrap().join(p)
|
2015-08-01 00:59:05 -04:00
|
|
|
} else {
|
|
|
|
|
p.to_path_buf()
|
|
|
|
|
}
|
2015-07-18 00:04:20 +02:00
|
|
|
} else {
|
2018-01-07 22:10:48 +08:00
|
|
|
env::current_dir().expect("Unable to determine the current directory")
|
2015-07-06 21:12:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-02 04:46:05 +02:00
|
|
|
|
2017-01-01 09:42:47 -08:00
|
|
|
fn open<P: AsRef<OsStr>>(path: P) {
|
2021-09-06 15:52:43 -04:00
|
|
|
info!("Opening web browser");
|
2021-09-26 19:55:41 +02:00
|
|
|
if let Err(e) = opener::open(path) {
|
2018-01-07 22:10:48 +08:00
|
|
|
error!("Error opening web browser: {}", e);
|
2017-01-01 09:42:47 -08:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-18 15:56:45 -06:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn verify_app() {
|
2022-07-04 23:16:31 +08:00
|
|
|
create_clap_command().debug_assert();
|
2022-01-18 15:56:45 -06:00
|
|
|
}
|