From 8983863ffaae9aeb62b26f1f8b837381301583c7 Mon Sep 17 00:00:00 2001 From: Arthur Pinheiro Date: Wed, 12 Mar 2025 13:26:31 +0100 Subject: [PATCH] fix: allow relative paths in allowlist and blocklist --- src/args.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/args.rs b/src/args.rs index af165ae..7a00d3b 100644 --- a/src/args.rs +++ b/src/args.rs @@ -3,7 +3,29 @@ use std::path::PathBuf; -use clap::Parser; +use clap::{builder::{PathBufValueParser, TypedValueParser}, Parser}; + +#[derive(Clone, Default)] +struct AbsolutePathBufValueParser; + +impl TypedValueParser for AbsolutePathBufValueParser { + type Value = PathBuf; + + fn parse_ref( + &self, + cmd: &clap::Command, + arg: Option<&clap::Arg>, + value: &std::ffi::OsStr, + ) -> Result { + let pathbuf_parser = PathBufValueParser::new(); + let v = pathbuf_parser.parse_ref(cmd, arg, value)?; + + v.canonicalize().map_err(|e| clap::Error::raw( + clap::error::ErrorKind::Io, + e, + )) + } +} /// A tool to clean up sysroots for Linux embedded devices to save storage space. #[derive(Parser, Debug)] @@ -20,7 +42,7 @@ pub struct Args { /// An allowlist of files to keep, in .gitignore format. /// Note: this will take precedence over all other removal decisions. - #[arg(long)] + #[arg(long, value_parser = AbsolutePathBufValueParser::default())] pub allowlist: Option, /// A blocklist of files to remove, in .gitignore format. @@ -35,3 +57,4 @@ pub struct Args { /// The location of the sysroot to clean up pub sysroot_location: PathBuf, } +