Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 29 additions & 54 deletions clap_builder/src/builder/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,44 +359,38 @@ impl ArgAction {
/// processed.
pub fn takes_values(&self) -> bool {
match self {
Self::Set => true,
Self::Append => true,
Self::SetTrue => false,
Self::SetFalse => false,
Self::Count => false,
Self::Help => false,
Self::HelpShort => false,
Self::HelpLong => false,
Self::Version => false,
Self::Set | Self::Append => true,
Self::SetTrue
| Self::SetFalse
| Self::Count
| Self::Help
| Self::HelpShort
| Self::HelpLong
| Self::Version => false,
}
}

#[cfg(debug_assertions)]
pub(crate) fn max_num_args(&self) -> ValueRange {
match self {
Self::Set => ValueRange::FULL,
Self::Append => ValueRange::FULL,
Self::SetTrue => ValueRange::OPTIONAL,
Self::SetFalse => ValueRange::OPTIONAL,
Self::Count => ValueRange::EMPTY,
Self::Help => ValueRange::EMPTY,
Self::HelpShort => ValueRange::EMPTY,
Self::HelpLong => ValueRange::EMPTY,
Self::Version => ValueRange::EMPTY,
Self::Set | Self::Append => ValueRange::FULL,
Self::SetTrue | Self::SetFalse => ValueRange::OPTIONAL,
Self::Count | Self::Help | Self::HelpShort | Self::HelpLong | Self::Version => {
ValueRange::EMPTY
}
}
}

pub(crate) fn default_num_args(&self) -> ValueRange {
match self {
Self::Set => ValueRange::SINGLE,
Self::Append => ValueRange::SINGLE,
Self::SetTrue => ValueRange::EMPTY,
Self::SetFalse => ValueRange::EMPTY,
Self::Count => ValueRange::EMPTY,
Self::Help => ValueRange::EMPTY,
Self::HelpShort => ValueRange::EMPTY,
Self::HelpLong => ValueRange::EMPTY,
Self::Version => ValueRange::EMPTY,
Self::Set | Self::Append => ValueRange::SINGLE,
Self::SetTrue
| Self::SetFalse
| Self::Count
| Self::Help
| Self::HelpShort
| Self::HelpLong
| Self::Version => ValueRange::EMPTY,
}
}

Expand All @@ -407,53 +401,34 @@ impl ArgAction {
Self::SetTrue => Some(std::ffi::OsStr::new("false")),
Self::SetFalse => Some(std::ffi::OsStr::new("true")),
Self::Count => Some(std::ffi::OsStr::new("0")),
Self::Help => None,
Self::HelpShort => None,
Self::HelpLong => None,
Self::Version => None,
Self::Help | Self::HelpShort | Self::HelpLong | Self::Version => None,
}
}

pub(crate) fn default_missing_value(&self) -> Option<&'static std::ffi::OsStr> {
match self {
Self::Set => None,
Self::Append => None,
Self::Set | Self::Append => None,
Self::SetTrue => Some(std::ffi::OsStr::new("true")),
Self::SetFalse => Some(std::ffi::OsStr::new("false")),
Self::Count => None,
Self::Help => None,
Self::HelpShort => None,
Self::HelpLong => None,
Self::Version => None,
Self::Count | Self::Help | Self::HelpShort | Self::HelpLong | Self::Version => None,
}
}

pub(crate) fn default_value_parser(&self) -> Option<super::ValueParser> {
match self {
Self::Set => None,
Self::Append => None,
Self::SetTrue => Some(super::ValueParser::bool()),
Self::SetFalse => Some(super::ValueParser::bool()),
Self::Set | Self::Append => None,
Self::SetTrue | Self::SetFalse => Some(super::ValueParser::bool()),
Self::Count => Some(crate::value_parser!(u8).into()),
Self::Help => None,
Self::HelpShort => None,
Self::HelpLong => None,
Self::Version => None,
Self::Help | Self::HelpShort | Self::HelpLong | Self::Version => None,
}
}

#[cfg(debug_assertions)]
pub(crate) fn value_type_id(&self) -> Option<AnyValueId> {
match self {
Self::Set => None,
Self::Append => None,
Self::SetTrue => None,
Self::SetFalse => None,
Self::Set | Self::Append | Self::SetTrue | Self::SetFalse => None,
Self::Count => Some(AnyValueId::of::<CountType>()),
Self::Help => None,
Self::HelpShort => None,
Self::HelpLong => None,
Self::Version => None,
Self::Help | Self::HelpShort | Self::HelpLong | Self::Version => None,
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions clap_builder/src/builder/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Arg {
/// ```
/// [`Arg::action(ArgAction::Set)`]: Arg::action()
pub fn new(id: impl Into<Id>) -> Self {
Arg::default().id(id)
Self::default().id(id)
}

/// Set the identifier used for referencing this argument in the clap API.
Expand Down Expand Up @@ -4678,14 +4678,14 @@ impl Arg {
}
}

impl From<&'_ Arg> for Arg {
fn from(a: &Arg) -> Self {
impl From<&'_ Self> for Arg {
fn from(a: &Self) -> Self {
a.clone()
}
}

impl PartialEq for Arg {
fn eq(&self, other: &Arg) -> bool {
fn eq(&self, other: &Self) -> bool {
self.get_id() == other.get_id()
}
}
Expand All @@ -4697,7 +4697,7 @@ impl PartialOrd for Arg {
}

impl Ord for Arg {
fn cmp(&self, other: &Arg) -> Ordering {
fn cmp(&self, other: &Self) -> Ordering {
self.get_id().cmp(other.get_id())
}
}
Expand Down
6 changes: 3 additions & 3 deletions clap_builder/src/builder/arg_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl ArgGroup {
/// # ;
/// ```
pub fn new(id: impl Into<Id>) -> Self {
ArgGroup::default().id(id)
Self::default().id(id)
}

/// Sets the group name.
Expand Down Expand Up @@ -548,8 +548,8 @@ impl ArgGroup {
}
}

impl From<&'_ ArgGroup> for ArgGroup {
fn from(g: &ArgGroup) -> Self {
impl From<&'_ Self> for ArgGroup {
fn from(g: &Self) -> Self {
g.clone()
}
}
Expand Down
33 changes: 15 additions & 18 deletions clap_builder/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ impl Command {
/// ```
#[inline]
#[must_use]
pub fn subcommand(self, subcmd: impl Into<Command>) -> Self {
pub fn subcommand(self, subcmd: impl Into<Self>) -> Self {
let subcmd = subcmd.into();
self.subcommand_internal(subcmd)
}
Expand Down Expand Up @@ -536,7 +536,7 @@ impl Command {
/// )
/// # ;
/// ```
pub fn defer(mut self, deferred: fn(Command) -> Command) -> Self {
pub fn defer(mut self, deferred: fn(Self) -> Self) -> Self {
self.deferred = Some(deferred);
self
}
Expand Down Expand Up @@ -3836,13 +3836,13 @@ impl Command {

/// Iterate through the set of subcommands, getting a reference to each.
#[inline]
pub fn get_subcommands(&self) -> impl Iterator<Item = &Command> {
pub fn get_subcommands(&self) -> impl Iterator<Item = &Self> {
self.subcommands.iter()
}

/// Iterate through the set of subcommands, getting a mutable reference to each.
#[inline]
pub fn get_subcommands_mut(&mut self) -> impl Iterator<Item = &mut Command> {
pub fn get_subcommands_mut(&mut self) -> impl Iterator<Item = &mut Self> {
self.subcommands.iter_mut()
}

Expand Down Expand Up @@ -3892,7 +3892,7 @@ impl Command {
///
/// This does not recurse through subcommands of subcommands.
#[inline]
pub fn find_subcommand(&self, name: impl AsRef<std::ffi::OsStr>) -> Option<&Command> {
pub fn find_subcommand(&self, name: impl AsRef<std::ffi::OsStr>) -> Option<&Self> {
let name = name.as_ref();
self.get_subcommands().find(|s| s.aliases_to(name))
}
Expand All @@ -3902,10 +3902,7 @@ impl Command {
///
/// This does not recurse through subcommands of subcommands.
#[inline]
pub fn find_subcommand_mut(
&mut self,
name: impl AsRef<std::ffi::OsStr>,
) -> Option<&mut Command> {
pub fn find_subcommand_mut(&mut self, name: impl AsRef<std::ffi::OsStr>) -> Option<&mut Self> {
let name = name.as_ref();
self.get_subcommands_mut().find(|s| s.aliases_to(name))
}
Expand Down Expand Up @@ -4754,12 +4751,12 @@ impl Command {

let mut help_subcmd = if expand_help_tree {
// Slow code path to recursively clone all other subcommand subtrees under help
let help_subcmd = Command::new("help")
let help_subcmd = Self::new("help")
.about(help_about)
.global_setting(AppSettings::DisableHelpSubcommand)
.subcommands(self.get_subcommands().map(Command::_copy_subtree_for_help));
.subcommands(self.get_subcommands().map(Self::_copy_subtree_for_help));

let mut help_help_subcmd = Command::new("help").about(help_about);
let mut help_help_subcmd = Self::new("help").about(help_about);
help_help_subcmd.version = None;
help_help_subcmd.long_version = None;
help_help_subcmd = help_help_subcmd
Expand All @@ -4768,7 +4765,7 @@ impl Command {

help_subcmd.subcommand(help_help_subcmd)
} else {
Command::new("help").about(help_about).arg(
Self::new("help").about(help_about).arg(
Arg::new("subcommand")
.action(ArgAction::Append)
.num_args(..)
Expand All @@ -4791,12 +4788,12 @@ impl Command {
}
}

fn _copy_subtree_for_help(&self) -> Command {
let mut cmd = Command::new(self.name.clone())
fn _copy_subtree_for_help(&self) -> Self {
let mut cmd = Self::new(self.name.clone())
.hide(self.is_hide_set())
.global_setting(AppSettings::DisableHelpFlag)
.global_setting(AppSettings::DisableVersionFlag)
.subcommands(self.get_subcommands().map(Command::_copy_subtree_for_help));
.subcommands(self.get_subcommands().map(Self::_copy_subtree_for_help));
if self.get_about().is_some() {
cmd = cmd.about(self.get_about().unwrap().clone());
}
Expand Down Expand Up @@ -5154,8 +5151,8 @@ impl Index<&'_ Id> for Command {
}
}

impl From<&'_ Command> for Command {
fn from(cmd: &'_ Command) -> Self {
impl From<&'_ Self> for Command {
fn from(cmd: &'_ Self) -> Self {
cmd.clone()
}
}
Expand Down
8 changes: 4 additions & 4 deletions clap_builder/src/builder/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ impl OsStr {
}
}

impl From<&'_ OsStr> for OsStr {
fn from(id: &'_ OsStr) -> Self {
impl From<&'_ Self> for OsStr {
fn from(id: &'_ Self) -> Self {
id.clone()
}
}
Expand Down Expand Up @@ -303,7 +303,7 @@ impl Default for Inner {
}

impl PartialEq for Inner {
fn eq(&self, other: &Inner) -> bool {
fn eq(&self, other: &Self) -> bool {
self.as_os_str() == other.as_os_str()
}
}
Expand All @@ -315,7 +315,7 @@ impl PartialOrd for Inner {
}

impl Ord for Inner {
fn cmp(&self, other: &Inner) -> std::cmp::Ordering {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_os_str().cmp(other.as_os_str())
}
}
Expand Down
2 changes: 1 addition & 1 deletion clap_builder/src/builder/possible_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl PossibleValue {
/// [possible value]: crate::builder::PossibleValuesParser
/// [`Arg::hide_possible_values(true)`]: crate::Arg::hide_possible_values()
pub fn new(name: impl Into<Str>) -> Self {
PossibleValue {
Self {
name: name.into(),
..Default::default()
}
Expand Down
18 changes: 9 additions & 9 deletions clap_builder/src/builder/resettable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,31 +140,31 @@ impl IntoResettable<Str> for Option<&'static str> {
}

impl<T> IntoResettable<T> for Resettable<T> {
fn into_resettable(self) -> Resettable<T> {
fn into_resettable(self) -> Self {
self
}
}

impl IntoResettable<char> for char {
fn into_resettable(self) -> Resettable<char> {
impl IntoResettable<Self> for char {
fn into_resettable(self) -> Resettable<Self> {
Resettable::Value(self)
}
}

impl IntoResettable<usize> for usize {
fn into_resettable(self) -> Resettable<usize> {
impl IntoResettable<Self> for usize {
fn into_resettable(self) -> Resettable<Self> {
Resettable::Value(self)
}
}

impl IntoResettable<ArgAction> for ArgAction {
fn into_resettable(self) -> Resettable<ArgAction> {
impl IntoResettable<Self> for ArgAction {
fn into_resettable(self) -> Resettable<Self> {
Resettable::Value(self)
}
}

impl IntoResettable<ValueHint> for ValueHint {
fn into_resettable(self) -> Resettable<ValueHint> {
impl IntoResettable<Self> for ValueHint {
fn into_resettable(self) -> Resettable<Self> {
Resettable::Value(self)
}
}
Expand Down
8 changes: 4 additions & 4 deletions clap_builder/src/builder/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl Str {
}
}

impl From<&'_ Str> for Str {
fn from(id: &'_ Str) -> Self {
impl From<&'_ Self> for Str {
fn from(id: &'_ Self) -> Self {
id.clone()
}
}
Expand Down Expand Up @@ -287,7 +287,7 @@ impl Default for Inner {
}

impl PartialEq for Inner {
fn eq(&self, other: &Inner) -> bool {
fn eq(&self, other: &Self) -> bool {
self.as_str() == other.as_str()
}
}
Expand All @@ -299,7 +299,7 @@ impl PartialOrd for Inner {
}

impl Ord for Inner {
fn cmp(&self, other: &Inner) -> std::cmp::Ordering {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_str().cmp(other.as_str())
}
}
Expand Down
Loading
Loading