Mercurial > public > mercurial-scm > hg
diff rust/rhg/src/commands/cat.rs @ 49640:37bc3edef76f
rhg: upgrade `clap` dependency
This one is the worst one to upgrade since v2 -> v4 broke a ton of API,
which thankfully seems saner now.
Contrary to what was done in the `hg-core/src/examples/nodemap` rewrite,
we're not switching from the "builder" pattern to the "derive" pattern,
since that would imply a much larger diff. It can be done incrementally.
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Tue, 15 Nov 2022 00:02:43 +0100 |
parents | c7fb9b74e753 |
children | c15b415d1bff |
line wrap: on
line diff
--- a/rust/rhg/src/commands/cat.rs Mon Nov 14 17:18:56 2022 +0100 +++ b/rust/rhg/src/commands/cat.rs Tue Nov 15 00:02:43 2022 +0100 @@ -4,27 +4,28 @@ use hg::operations::cat; use hg::utils::hg_path::HgPathBuf; use micro_timer::timed; +use std::ffi::OsString; +use std::os::unix::prelude::OsStrExt; pub const HELP_TEXT: &str = " Output the current or given revision of files "; -pub fn args() -> clap::App<'static, 'static> { - clap::SubCommand::with_name("cat") +pub fn args() -> clap::Command { + clap::command!("cat") .arg( - Arg::with_name("rev") + Arg::new("rev") .help("search the repository as it is in REV") - .short("-r") - .long("--rev") - .value_name("REV") - .takes_value(true), + .short('r') + .long("rev") + .value_name("REV"), ) .arg( - clap::Arg::with_name("files") + clap::Arg::new("files") .required(true) - .multiple(true) - .empty_values(false) + .num_args(1..) .value_name("FILE") + .value_parser(clap::value_parser!(std::ffi::OsString)) .help("Files to output"), ) .about(HELP_TEXT) @@ -41,11 +42,15 @@ )); } - let rev = invocation.subcommand_args.value_of("rev"); - let file_args = match invocation.subcommand_args.values_of("files") { - Some(files) => files.collect(), - None => vec![], - }; + let rev = invocation.subcommand_args.get_one::<String>("rev"); + let file_args = + match invocation.subcommand_args.get_many::<OsString>("files") { + Some(files) => files + .filter(|s| !s.is_empty()) + .map(|s| s.as_os_str()) + .collect(), + None => vec![], + }; let repo = invocation.repo?; let cwd = hg::utils::current_dir()?; @@ -53,8 +58,8 @@ let working_directory = cwd.join(working_directory); // Make it absolute let mut files = vec![]; - for file in file_args.iter() { - if file.starts_with("set:") { + for file in file_args { + if file.as_bytes().starts_with(b"set:") { let message = "fileset"; return Err(CommandError::unsupported(message)); } @@ -62,7 +67,7 @@ let normalized = cwd.join(&file); // TODO: actually normalize `..` path segments etc? let dotted = normalized.components().any(|c| c.as_os_str() == ".."); - if file == &"." || dotted { + if file.as_bytes() == b"." || dotted { let message = "`..` or `.` path segment"; return Err(CommandError::unsupported(message)); } @@ -74,7 +79,7 @@ .map_err(|_| { CommandError::abort(format!( "abort: {} not under root '{}'\n(consider using '--cwd {}')", - file, + String::from_utf8_lossy(file.as_bytes()), working_directory.display(), relative_path.display(), ))