1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-03 14:37:45 +00:00

refactor/uucore ~ add OsString support for executable!()

This commit is contained in:
Roy Ivy III 2021-06-30 21:59:04 -05:00 committed by Michael Debertol
parent 6f58da00dd
commit be8f073217

View file

@ -18,11 +18,33 @@ macro_rules! util_name(
}) })
); );
/// Get the executable name. /// Get the executable path (as `OsString`).
#[macro_export]
macro_rules! executable_os(
() => ({
&std::env::args_os().next().unwrap()
})
);
/// Get the executable path (as `String`; lossless).
#[macro_export] #[macro_export]
macro_rules! executable( macro_rules! executable(
() => ({ () => ({
&std::env::args().next().unwrap() let exe = match executable_os!().to_str() {
// * UTF-8
Some(s) => s.to_string(),
// * "lossless" debug format if `executable_os!()` is not well-formed UTF-8
None => format!("{:?}", executable_os!())
};
&exe.to_owned()
})
);
/// Get the executable name.
#[macro_export]
macro_rules! executable_name(
() => ({
&std::path::Path::new(executable_os!()).file_stem().unwrap().to_string_lossy()
}) })
); );
@ -31,10 +53,7 @@ macro_rules! show(
($err:expr) => ({ ($err:expr) => ({
let e = $err; let e = $err;
uucore::error::set_exit_code(e.code()); uucore::error::set_exit_code(e.code());
eprintln!("{}: {}", executable!(), e); eprintln!("{}: {}", executable_name!(), e);
if e.usage() {
eprintln!("Try '{} --help' for more information.", executable!());
}
}) })
); );
@ -51,7 +70,7 @@ macro_rules! show_if_err(
#[macro_export] #[macro_export]
macro_rules! show_error( macro_rules! show_error(
($($args:tt)+) => ({ ($($args:tt)+) => ({
eprint!("{}: ", executable!()); eprint!("{}: ", executable_name!());
eprintln!($($args)+); eprintln!($($args)+);
}) })
); );
@ -60,7 +79,7 @@ macro_rules! show_error(
#[macro_export] #[macro_export]
macro_rules! show_error_custom_description ( macro_rules! show_error_custom_description (
($err:expr,$($args:tt)+) => ({ ($err:expr,$($args:tt)+) => ({
eprint!("{}: {}: ", executable!(), $err); eprint!("{}: {}: ", executable_name!(), $err);
eprintln!($($args)+); eprintln!($($args)+);
}) })
); );
@ -68,7 +87,7 @@ macro_rules! show_error_custom_description (
#[macro_export] #[macro_export]
macro_rules! show_warning( macro_rules! show_warning(
($($args:tt)+) => ({ ($($args:tt)+) => ({
eprint!("{}: warning: ", executable!()); eprint!("{}: warning: ", executable_name!());
eprintln!($($args)+); eprintln!($($args)+);
}) })
); );
@ -77,9 +96,9 @@ macro_rules! show_warning(
#[macro_export] #[macro_export]
macro_rules! show_usage_error( macro_rules! show_usage_error(
($($args:tt)+) => ({ ($($args:tt)+) => ({
eprint!("{}: ", executable!()); eprint!("{}: ", executable_name!());
eprintln!($($args)+); eprintln!($($args)+);
eprintln!("Try '{} --help' for more information.", executable!()); eprintln!("Try `{:?} --help` for more information.", executable!());
}) })
); );