clap
clap copied to clipboard
Allow visible aliases for PossibleValue
Please complete the following tasks
- [X] I have searched the discussions
- [X] I have searched the open and rejected issues
Clap Version
4.0.18
Describe your use case
Tealdeer has a parameter -p, --platform <PLATFORM>
to select the platform. For historic reasons, this list must include "osx", but should also include the alias "macos". This alias should not be hidden, but should be visible.
With Clap 3, this was implemented using FromStr
:
#[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[allow(dead_code)]
pub enum PlatformType {
Linux,
OsX,
SunOs,
Windows,
Android,
}
impl str::FromStr for PlatformType {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"linux" => Ok(Self::Linux),
"osx" | "macos" => Ok(Self::OsX),
"sunos" => Ok(Self::SunOs),
"windows" => Ok(Self::Windows),
"android" => Ok(Self::Android),
other => Err(anyhow!(
"Unknown OS: {}. Possible values: linux, macos, osx, sunos, windows, android",
other
)),
}
}
}
/// Override the operating system
#[clap(
short = 'p',
long = "platform",
possible_values = ["linux", "macos", "windows", "sunos", "osx", "android"],
)]
pub platform: Option<PlatformType>,
With Clap 4, I tried to migrate to EnumValueParser
:
impl clap::ValueEnum for PlatformType {
fn value_variants<'a>() -> &'a [Self] {
&[Self::Linux, Self::OsX, Self::Windows, Self::SunOs, Self::Android]
}
fn to_possible_value<'a>(&self) -> Option<clap::builder::PossibleValue> {
match self {
Self::Linux => Some(clap::builder::PossibleValue::new("linux")),
Self::OsX => Some(clap::builder::PossibleValue::new("osx").alias("macos")),
Self::Windows => Some(clap::builder::PossibleValue::new("windows")),
Self::SunOs => Some(clap::builder::PossibleValue::new("sunos")),
Self::Android => Some(clap::builder::PossibleValue::new("android")),
}
}
}
This works, but the alias is not visible in help output or error messages.
Describe the solution you'd like
Since Command
has both .alias(...)
and .visible_alias(...)
, would this be possible for PossibleValue
as well?
Alternatives, if applicable
No response
Additional Context
No response