1
Fork 0

Bump to clap 3

This commit is contained in:
Michael Howell 2022-06-17 14:53:03 -07:00
parent 73443a0590
commit b8844f2811
3 changed files with 22 additions and 21 deletions

View file

@ -3385,7 +3385,7 @@ dependencies = [
name = "rustbook" name = "rustbook"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap 2.34.0", "clap 3.1.1",
"env_logger 0.7.1", "env_logger 0.7.1",
"mdbook", "mdbook",
] ]

View file

@ -5,7 +5,7 @@ license = "MIT OR Apache-2.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
clap = "2.25.0" clap = "3.1.1"
env_logger = "0.7.1" env_logger = "0.7.1"
[dependencies.mdbook] [dependencies.mdbook]

View file

@ -3,54 +3,55 @@ use clap::crate_version;
use std::env; use std::env;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use clap::{App, AppSettings, ArgMatches, SubCommand}; use clap::{arg, ArgMatches, Command};
use mdbook::errors::Result as Result3; use mdbook::errors::Result as Result3;
use mdbook::MDBook; use mdbook::MDBook;
fn main() { fn main() {
let crate_version = format!("v{}", crate_version!());
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init(); env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
let d_message = "-d, --dest-dir=[dest-dir] let d_arg = arg!(-d --"dest-dir" <DEST_DIR>
'The output directory for your book{n}(Defaults to ./book when omitted)'"; "The output directory for your book{n}(Defaults to ./book when omitted)");
let dir_message = "[dir] let dir_arg = arg!([dir]
'A directory for your book{n}(Defaults to Current Directory when omitted)'"; "A directory for your book{n}(Defaults to Current Directory when omitted)");
let matches = App::new("rustbook") let matches = Command::new("rustbook")
.about("Build a book with mdBook") .about("Build a book with mdBook")
.author("Steve Klabnik <steve@steveklabnik.com>") .author("Steve Klabnik <steve@steveklabnik.com>")
.version(&*format!("v{}", crate_version!())) .version(&*crate_version)
.setting(AppSettings::SubcommandRequired) .subcommand_required(true)
.subcommand( .subcommand(
SubCommand::with_name("build") Command::new("build")
.about("Build the book from the markdown files") .about("Build the book from the markdown files")
.arg_from_usage(d_message) .arg(d_arg)
.arg_from_usage(dir_message), .arg(&dir_arg),
) )
.subcommand( .subcommand(
SubCommand::with_name("test") Command::new("test")
.about("Tests that a book's Rust code samples compile") .about("Tests that a book's Rust code samples compile")
.arg_from_usage(dir_message), .arg(dir_arg),
) )
.get_matches(); .get_matches();
// Check which subcomamnd the user ran... // Check which subcomamnd the user ran...
match matches.subcommand() { match matches.subcommand() {
("build", Some(sub_matches)) => { Some(("build", sub_matches)) => {
if let Err(e) = build(sub_matches) { if let Err(e) = build(sub_matches) {
handle_error(e); handle_error(e);
} }
} }
("test", Some(sub_matches)) => { Some(("test", sub_matches)) => {
if let Err(e) = test(sub_matches) { if let Err(e) = test(sub_matches) {
handle_error(e); handle_error(e);
} }
} }
(_, _) => unreachable!(), _ => unreachable!(),
}; };
} }
// Build command implementation // Build command implementation
pub fn build(args: &ArgMatches<'_>) -> Result3<()> { pub fn build(args: &ArgMatches) -> Result3<()> {
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let mut book = load_book(&book_dir)?; let mut book = load_book(&book_dir)?;
@ -66,13 +67,13 @@ pub fn build(args: &ArgMatches<'_>) -> Result3<()> {
Ok(()) Ok(())
} }
fn test(args: &ArgMatches<'_>) -> Result3<()> { fn test(args: &ArgMatches) -> Result3<()> {
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let mut book = load_book(&book_dir)?; let mut book = load_book(&book_dir)?;
book.test(vec![]) book.test(vec![])
} }
fn get_book_dir(args: &ArgMatches<'_>) -> PathBuf { fn get_book_dir(args: &ArgMatches) -> PathBuf {
if let Some(dir) = args.value_of("dir") { if let Some(dir) = args.value_of("dir") {
// Check if path is relative from current dir, or absolute... // Check if path is relative from current dir, or absolute...
let p = Path::new(dir); let p = Path::new(dir);