1
1
// Std
2
2
#[ cfg( feature = "env" ) ]
3
3
use std:: env;
4
+ #[ cfg( feature = "env" ) ]
5
+ use std:: ffi:: OsString ;
4
6
use std:: {
5
7
borrow:: Cow ,
6
8
cmp:: { Ord , Ordering } ,
7
- ffi:: OsStr ,
8
- ffi:: OsString ,
9
9
fmt:: { self , Display , Formatter } ,
10
10
str,
11
11
} ;
12
12
13
13
// Internal
14
14
use super :: { ArgFlags , ArgSettings } ;
15
15
use crate :: builder:: ArgPredicate ;
16
+ use crate :: builder:: IntoResettable ;
16
17
use crate :: builder:: PossibleValue ;
17
18
use crate :: builder:: ValueRange ;
18
19
use crate :: util:: Id ;
20
+ use crate :: util:: OsStr ;
19
21
use crate :: ArgAction ;
20
22
use crate :: ValueHint ;
21
23
use crate :: INTERNAL_ERROR_MSG ;
@@ -60,8 +62,8 @@ pub struct Arg<'help> {
60
62
pub ( crate ) overrides : Vec < Id > ,
61
63
pub ( crate ) groups : Vec < Id > ,
62
64
pub ( crate ) requires : Vec < ( ArgPredicate , Id ) > ,
63
- pub ( crate ) r_ifs : Vec < ( Id , OsString ) > ,
64
- pub ( crate ) r_ifs_all : Vec < ( Id , OsString ) > ,
65
+ pub ( crate ) r_ifs : Vec < ( Id , OsStr ) > ,
66
+ pub ( crate ) r_ifs_all : Vec < ( Id , OsStr ) > ,
65
67
pub ( crate ) r_unless : Vec < Id > ,
66
68
pub ( crate ) r_unless_all : Vec < Id > ,
67
69
pub ( crate ) short : Option < char > ,
@@ -72,11 +74,11 @@ pub struct Arg<'help> {
72
74
pub ( crate ) val_names : Vec < & ' help str > ,
73
75
pub ( crate ) num_vals : Option < ValueRange > ,
74
76
pub ( crate ) val_delim : Option < char > ,
75
- pub ( crate ) default_vals : Vec < & ' help OsStr > ,
76
- pub ( crate ) default_vals_ifs : Vec < ( Id , ArgPredicate , Option < & ' help OsStr > ) > ,
77
- pub ( crate ) default_missing_vals : Vec < & ' help OsStr > ,
77
+ pub ( crate ) default_vals : Vec < OsStr > ,
78
+ pub ( crate ) default_vals_ifs : Vec < ( Id , ArgPredicate , Option < OsStr > ) > ,
79
+ pub ( crate ) default_missing_vals : Vec < OsStr > ,
78
80
#[ cfg( feature = "env" ) ]
79
- pub ( crate ) env : Option < ( & ' help OsStr , Option < OsString > ) > ,
81
+ pub ( crate ) env : Option < ( OsStr , Option < OsString > ) > ,
80
82
pub ( crate ) terminator : Option < & ' help str > ,
81
83
pub ( crate ) index : Option < usize > ,
82
84
pub ( crate ) help_heading : Option < Option < & ' help str > > ,
@@ -1514,8 +1516,8 @@ impl<'help> Arg<'help> {
1514
1516
/// [`Arg::default_value_if`]: Arg::default_value_if()
1515
1517
#[ inline]
1516
1518
#[ must_use]
1517
- pub fn default_value ( self , val : & ' help str ) -> Self {
1518
- self . default_values_os ( [ OsStr :: new ( val) ] )
1519
+ pub fn default_value ( self , val : impl Into < OsStr > ) -> Self {
1520
+ self . default_values_os ( [ val] )
1519
1521
}
1520
1522
1521
1523
/// Value for the argument when not present.
@@ -1526,7 +1528,7 @@ impl<'help> Arg<'help> {
1526
1528
/// [`OsStr`]: std::ffi::OsStr
1527
1529
#[ inline]
1528
1530
#[ must_use]
1529
- pub fn default_value_os ( self , val : & ' help OsStr ) -> Self {
1531
+ pub fn default_value_os ( self , val : impl Into < OsStr > ) -> Self {
1530
1532
self . default_values_os ( [ val] )
1531
1533
}
1532
1534
@@ -1537,9 +1539,8 @@ impl<'help> Arg<'help> {
1537
1539
/// [`Arg::default_value`]: Arg::default_value()
1538
1540
#[ inline]
1539
1541
#[ must_use]
1540
- pub fn default_values ( self , vals : impl IntoIterator < Item = impl Into < & ' help str > > ) -> Self {
1541
- let vals_vec = vals. into_iter ( ) . map ( |val| OsStr :: new ( val. into ( ) ) ) ;
1542
- self . default_values_os ( vals_vec)
1542
+ pub fn default_values ( self , vals : impl IntoIterator < Item = impl Into < OsStr > > ) -> Self {
1543
+ self . default_values_os ( vals)
1543
1544
}
1544
1545
1545
1546
/// Value for the argument when not present.
@@ -1550,10 +1551,7 @@ impl<'help> Arg<'help> {
1550
1551
/// [`OsStr`]: std::ffi::OsStr
1551
1552
#[ inline]
1552
1553
#[ must_use]
1553
- pub fn default_values_os (
1554
- mut self ,
1555
- vals : impl IntoIterator < Item = impl Into < & ' help OsStr > > ,
1556
- ) -> Self {
1554
+ pub fn default_values_os ( mut self , vals : impl IntoIterator < Item = impl Into < OsStr > > ) -> Self {
1557
1555
self . default_vals = vals. into_iter ( ) . map ( |s| s. into ( ) ) . collect ( ) ;
1558
1556
self
1559
1557
}
@@ -1649,8 +1647,8 @@ impl<'help> Arg<'help> {
1649
1647
/// [`Arg::default_value`]: Arg::default_value()
1650
1648
#[ inline]
1651
1649
#[ must_use]
1652
- pub fn default_missing_value ( self , val : & ' help str ) -> Self {
1653
- self . default_missing_values_os ( [ OsStr :: new ( val) ] )
1650
+ pub fn default_missing_value ( self , val : impl Into < OsStr > ) -> Self {
1651
+ self . default_missing_values_os ( [ val] )
1654
1652
}
1655
1653
1656
1654
/// Value for the argument when the flag is present but no value is specified.
@@ -1661,7 +1659,7 @@ impl<'help> Arg<'help> {
1661
1659
/// [`OsStr`]: std::ffi::OsStr
1662
1660
#[ inline]
1663
1661
#[ must_use]
1664
- pub fn default_missing_value_os ( self , val : & ' help OsStr ) -> Self {
1662
+ pub fn default_missing_value_os ( self , val : impl Into < OsStr > ) -> Self {
1665
1663
self . default_missing_values_os ( [ val] )
1666
1664
}
1667
1665
@@ -1672,12 +1670,8 @@ impl<'help> Arg<'help> {
1672
1670
/// [`Arg::default_missing_value`]: Arg::default_missing_value()
1673
1671
#[ inline]
1674
1672
#[ must_use]
1675
- pub fn default_missing_values (
1676
- self ,
1677
- vals : impl IntoIterator < Item = impl Into < & ' help str > > ,
1678
- ) -> Self {
1679
- let vals_vec = vals. into_iter ( ) . map ( |val| OsStr :: new ( val. into ( ) ) ) ;
1680
- self . default_missing_values_os ( vals_vec)
1673
+ pub fn default_missing_values ( self , vals : impl IntoIterator < Item = impl Into < OsStr > > ) -> Self {
1674
+ self . default_missing_values_os ( vals)
1681
1675
}
1682
1676
1683
1677
/// Value for the argument when the flag is present but no value is specified.
@@ -1690,7 +1684,7 @@ impl<'help> Arg<'help> {
1690
1684
#[ must_use]
1691
1685
pub fn default_missing_values_os (
1692
1686
mut self ,
1693
- vals : impl IntoIterator < Item = impl Into < & ' help OsStr > > ,
1687
+ vals : impl IntoIterator < Item = impl Into < OsStr > > ,
1694
1688
) -> Self {
1695
1689
self . default_missing_vals = vals. into_iter ( ) . map ( |s| s. into ( ) ) . collect ( ) ;
1696
1690
self
@@ -1844,8 +1838,8 @@ impl<'help> Arg<'help> {
1844
1838
#[ cfg( feature = "env" ) ]
1845
1839
#[ inline]
1846
1840
#[ must_use]
1847
- pub fn env ( self , name : & ' help str ) -> Self {
1848
- self . env_os ( OsStr :: new ( name) )
1841
+ pub fn env ( self , name : impl Into < OsStr > ) -> Self {
1842
+ self . env_os ( name)
1849
1843
}
1850
1844
1851
1845
/// Read from `name` environment variable when argument is not present.
@@ -1854,8 +1848,10 @@ impl<'help> Arg<'help> {
1854
1848
#[ cfg( feature = "env" ) ]
1855
1849
#[ inline]
1856
1850
#[ must_use]
1857
- pub fn env_os ( mut self , name : & ' help OsStr ) -> Self {
1858
- self . env = Some ( ( name, env:: var_os ( name) ) ) ;
1851
+ pub fn env_os ( mut self , name : impl Into < OsStr > ) -> Self {
1852
+ let name = name. into ( ) ;
1853
+ let value = env:: var_os ( & name) ;
1854
+ self . env = Some ( ( name, value) ) ;
1859
1855
self
1860
1856
}
1861
1857
}
@@ -2615,9 +2611,9 @@ impl<'help> Arg<'help> {
2615
2611
self ,
2616
2612
arg_id : impl Into < Id > ,
2617
2613
val : impl Into < ArgPredicate > ,
2618
- default : Option < & ' help str > ,
2614
+ default : impl IntoResettable < OsStr > ,
2619
2615
) -> Self {
2620
- self . default_value_if_os ( arg_id, val. into ( ) , default. map ( OsStr :: new ) )
2616
+ self . default_value_if_os ( arg_id, val, default)
2621
2617
}
2622
2618
2623
2619
/// Provides a conditional default value in the exact same manner as [`Arg::default_value_if`]
@@ -2630,10 +2626,13 @@ impl<'help> Arg<'help> {
2630
2626
mut self ,
2631
2627
arg_id : impl Into < Id > ,
2632
2628
val : impl Into < ArgPredicate > ,
2633
- default : Option < & ' help OsStr > ,
2629
+ default : impl IntoResettable < OsStr > ,
2634
2630
) -> Self {
2635
- self . default_vals_ifs
2636
- . push ( ( arg_id. into ( ) , val. into ( ) , default) ) ;
2631
+ self . default_vals_ifs . push ( (
2632
+ arg_id. into ( ) ,
2633
+ val. into ( ) ,
2634
+ default. into_resettable ( ) . into_option ( ) ,
2635
+ ) ) ;
2637
2636
self
2638
2637
}
2639
2638
@@ -2721,10 +2720,16 @@ impl<'help> Arg<'help> {
2721
2720
#[ must_use]
2722
2721
pub fn default_value_ifs (
2723
2722
mut self ,
2724
- ifs : impl IntoIterator < Item = ( impl Into < Id > , impl Into < ArgPredicate > , Option < & ' help str > ) > ,
2723
+ ifs : impl IntoIterator <
2724
+ Item = (
2725
+ impl Into < Id > ,
2726
+ impl Into < ArgPredicate > ,
2727
+ impl IntoResettable < OsStr > ,
2728
+ ) ,
2729
+ > ,
2725
2730
) -> Self {
2726
2731
for ( arg, val, default) in ifs {
2727
- self = self . default_value_if_os ( arg, val, default. map ( OsStr :: new ) ) ;
2732
+ self = self . default_value_if_os ( arg, val, default) ;
2728
2733
}
2729
2734
self
2730
2735
}
@@ -2737,10 +2742,16 @@ impl<'help> Arg<'help> {
2737
2742
#[ must_use]
2738
2743
pub fn default_value_ifs_os (
2739
2744
mut self ,
2740
- ifs : impl IntoIterator < Item = ( impl Into < Id > , impl Into < ArgPredicate > , Option < & ' help OsStr > ) > ,
2745
+ ifs : impl IntoIterator <
2746
+ Item = (
2747
+ impl Into < Id > ,
2748
+ impl Into < ArgPredicate > ,
2749
+ impl IntoResettable < OsStr > ,
2750
+ ) ,
2751
+ > ,
2741
2752
) -> Self {
2742
2753
for ( arg, val, default) in ifs {
2743
- self = self . default_value_if_os ( arg. into ( ) , val, default) ;
2754
+ self = self . default_value_if_os ( arg, val, default) ;
2744
2755
}
2745
2756
self
2746
2757
}
@@ -3038,7 +3049,7 @@ impl<'help> Arg<'help> {
3038
3049
/// [Conflicting]: Arg::conflicts_with()
3039
3050
/// [required]: Arg::required()
3040
3051
#[ must_use]
3041
- pub fn required_if_eq ( mut self , arg_id : impl Into < Id > , val : impl Into < OsString > ) -> Self {
3052
+ pub fn required_if_eq ( mut self , arg_id : impl Into < Id > , val : impl Into < OsStr > ) -> Self {
3042
3053
self . r_ifs . push ( ( arg_id. into ( ) , val. into ( ) ) ) ;
3043
3054
self
3044
3055
}
@@ -3119,7 +3130,7 @@ impl<'help> Arg<'help> {
3119
3130
#[ must_use]
3120
3131
pub fn required_if_eq_any (
3121
3132
mut self ,
3122
- ifs : impl IntoIterator < Item = ( impl Into < Id > , impl Into < OsString > ) > ,
3133
+ ifs : impl IntoIterator < Item = ( impl Into < Id > , impl Into < OsStr > ) > ,
3123
3134
) -> Self {
3124
3135
self . r_ifs
3125
3136
. extend ( ifs. into_iter ( ) . map ( |( id, val) | ( id. into ( ) , val. into ( ) ) ) ) ;
@@ -3200,7 +3211,7 @@ impl<'help> Arg<'help> {
3200
3211
#[ must_use]
3201
3212
pub fn required_if_eq_all (
3202
3213
mut self ,
3203
- ifs : impl IntoIterator < Item = ( impl Into < Id > , impl Into < OsString > ) > ,
3214
+ ifs : impl IntoIterator < Item = ( impl Into < Id > , impl Into < OsStr > ) > ,
3204
3215
) -> Self {
3205
3216
self . r_ifs_all
3206
3217
. extend ( ifs. into_iter ( ) . map ( |( id, val) | ( id. into ( ) , val. into ( ) ) ) ) ;
@@ -3713,14 +3724,14 @@ impl<'help> Arg<'help> {
3713
3724
/// # Examples
3714
3725
///
3715
3726
/// ```rust
3716
- /// # use std::ffi ::OsStr;
3727
+ /// # use clap ::OsStr;
3717
3728
/// # use clap::Arg;
3718
3729
/// let arg = Arg::new("foo").env("ENVIRONMENT");
3719
- /// assert_eq!(Some(OsStr::new ("ENVIRONMENT")), arg.get_env( ));
3730
+ /// assert_eq!(arg.get_env(), Some(& OsStr::from ("ENVIRONMENT")));
3720
3731
/// ```
3721
3732
#[ cfg( feature = "env" ) ]
3722
3733
pub fn get_env ( & self ) -> Option < & OsStr > {
3723
- self . env . as_ref ( ) . map ( |x| x. 0 )
3734
+ self . env . as_ref ( ) . map ( |x| & x. 0 )
3724
3735
}
3725
3736
3726
3737
/// Get the default values specified for this argument, if any
@@ -3730,9 +3741,9 @@ impl<'help> Arg<'help> {
3730
3741
/// ```rust
3731
3742
/// # use clap::Arg;
3732
3743
/// let arg = Arg::new("foo").default_value("default value");
3733
- /// assert_eq!(&["default value"], arg.get_default_values() );
3744
+ /// assert_eq!(arg.get_default_values(), &["default value"]);
3734
3745
/// ```
3735
- pub fn get_default_values ( & self ) -> & [ & OsStr ] {
3746
+ pub fn get_default_values ( & self ) -> & [ OsStr ] {
3736
3747
& self . default_vals
3737
3748
}
3738
3749
@@ -3743,10 +3754,10 @@ impl<'help> Arg<'help> {
3743
3754
/// ```
3744
3755
/// # use clap::Arg;
3745
3756
/// let arg = Arg::new("foo");
3746
- /// assert_eq!(true, arg.is_positional());
3757
+ /// assert_eq!(arg.is_positional(), true );
3747
3758
///
3748
3759
/// let arg = Arg::new("foo").long("foo");
3749
- /// assert_eq!(false, arg.is_positional());
3760
+ /// assert_eq!(arg.is_positional(), false );
3750
3761
/// ```
3751
3762
pub fn is_positional ( & self ) -> bool {
3752
3763
self . long . is_none ( ) && self . short . is_none ( )
@@ -3892,12 +3903,12 @@ impl<'help> Arg<'help> {
3892
3903
if let Some ( action) = self . action . as_ref ( ) {
3893
3904
if let Some ( default_value) = action. default_value ( ) {
3894
3905
if self . default_vals . is_empty ( ) {
3895
- self . default_vals = vec ! [ default_value] ;
3906
+ self . default_vals = vec ! [ default_value. into ( ) ] ;
3896
3907
}
3897
3908
}
3898
3909
if let Some ( default_value) = action. default_missing_value ( ) {
3899
3910
if self . default_missing_vals . is_empty ( ) {
3900
- self . default_missing_vals = vec ! [ default_value] ;
3911
+ self . default_missing_vals = vec ! [ default_value. into ( ) ] ;
3901
3912
}
3902
3913
}
3903
3914
}
0 commit comments