Debian Codegraph @

src/cmd/command_prelude.rs

raw
1 //! Helpers for building the command-line arguments for commands.
2
3 pub use clap::{Arg, ArgMatches, Command, arg};
4 use mdbook_driver::MDBook;
5 use std::path::PathBuf;
6
7 pub trait CommandExt: Sized {
8 fn _arg(self, arg: Arg) -> Self;
9
10 fn arg_dest_dir(self) -> Self {
11 self._arg(
12 Arg::new("dest-dir")
13 .short('d')
14 .long("dest-dir")
15 .value_name("dest-dir")
16 .value_parser(clap::value_parser!(PathBuf))
17 .help(
18 "Output directory for the book\n\
19 Relative paths are interpreted relative to the current directory.\n\
20 If omitted, mdBook uses build.build-dir from book.toml \
21 or defaults to `./book`.",
22 ),
23 )
24 }
25
26 fn arg_root_dir(self) -> Self {
27 self._arg(
28 Arg::new("dir")
29 .help(
30 "Root directory for the book\n\
31 (Defaults to the current directory when omitted)",
32 )
33 .value_parser(clap::value_parser!(PathBuf)),
34 )
35 }
36
37 fn arg_open(self) -> Self {
38 self._arg(arg!(-o --open "Opens the compiled book in a web browser"))
39 }
40
41 #[cfg(any(feature = "watch", feature = "serve"))]
42 fn arg_watcher(self) -> Self {
43 #[cfg(feature = "watch")]
44 return self._arg(
45 Arg::new("watcher")
46 .long("watcher")
47 .value_parser(["poll", "native"])
48 .default_value("poll")
49 .help("The filesystem watching technique"),
50 );
51 #[cfg(not(feature = "watch"))]
52 return self;
53 }
54 }
55
56 impl CommandExt for Command {
57 fn _arg(self, arg: Arg) -> Self {
58 self.arg(arg)
59 }
60 }
61
62 pub fn set_dest_dir(args: &ArgMatches, book: &mut MDBook) {
63 if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
64 let build_dir = std::env::current_dir()
65 .expect("current dir should be valid")
66 .join(dest_dir);
67 book.config.build.build_dir = build_dir;
68 }
69 }