Skip to content

Commit b4f111a

Browse files
committed
feat: Stablize ArgMatches::get_occurrences
This let's you get an arguments values, grouped by the occurrence of the argument. Note: this does not stablize derive support. That requires a blocking change and can be enabled via `unstable-v5` flag. See #4626 for an exploration of how we can make this easier in the future. Fixes #2924
1 parent b86c259 commit b4f111a

File tree

9 files changed

+5
-21
lines changed

9 files changed

+5
-21
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ default = [
6969
"suggestions",
7070
]
7171
debug = ["clap_derive?/debug", "dep:backtrace"] # Enables debug messages
72-
unstable-doc = ["derive", "cargo", "wrap_help", "env", "unicode", "string", "unstable-replace", "unstable-grouped"] # for docs.rs
72+
unstable-doc = ["derive", "cargo", "wrap_help", "env", "unicode", "string", "unstable-replace"] # for docs.rs
7373

7474
# Used in default
7575
std = [] # support for no_std in a backwards-compatible way

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ MSRV?=1.64.0
1515
_FEATURES = minimal default wasm full debug release
1616
_FEATURES_minimal = --no-default-features --features "std"
1717
_FEATURES_default =
18-
_FEATURES_wasm = --no-default-features --features "std help usage error-context suggestions" --features "deprecated derive cargo env unicode string unstable-replace unstable-grouped"
19-
_FEATURES_full = --features "deprecated derive cargo env unicode string unstable-replace unstable-grouped wrap_help"
18+
_FEATURES_wasm = --no-default-features --features "std help usage error-context suggestions" --features "deprecated derive cargo env unicode string unstable-replace"
19+
_FEATURES_full = --features "deprecated derive cargo env unicode string unstable-replace wrap_help"
2020
_FEATURES_next = ${_FEATURES_full} --features unstable-v5
2121
_FEATURES_debug = ${_FEATURES_full} --features debug --features clap_complete/debug
2222
_FEATURES_release = ${_FEATURES_full} --release

src/_features.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@
2626
//! **Warning:** These may contain breaking changes between minor releases.
2727
//!
2828
//! * **unstable-replace**: Enable [`Command::replace`](https://github.com/clap-rs/clap/issues/2836)
29-
//! * **unstable-grouped**: Enable [`ArgMatches::grouped_values_of`](https://github.com/clap-rs/clap/issues/2924)
3029
//! * **unstable-v5**: Preview features which will be stable on the v5.0 release

src/parser/matches/arg_matches.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ impl ArgMatches {
246246
/// assert_eq!(vals, [["a", "b"], ["c", "d"]]);
247247
/// ```
248248
#[cfg_attr(debug_assertions, track_caller)]
249-
#[cfg(feature = "unstable-grouped")]
250249
pub fn get_occurrences<T: Any + Clone + Send + Sync + 'static>(
251250
&self,
252251
id: &str,
@@ -348,7 +347,6 @@ impl ArgMatches {
348347
/// [`OsStr`]: std::ffi::OsStr
349348
/// [values]: OsValues
350349
/// [`String`]: std::string::String
351-
#[cfg(feature = "unstable-grouped")]
352350
#[cfg_attr(debug_assertions, track_caller)]
353351
pub fn get_raw_occurrences(&self, id: &str) -> Option<RawOccurrences<'_>> {
354352
MatchesError::unwrap(id, self.try_get_raw_occurrences(id))
@@ -460,7 +458,6 @@ impl ArgMatches {
460458
/// let vals: Vec<Vec<String>> = m.remove_occurrences("x").unwrap().map(Iterator::collect).collect();
461459
/// assert_eq!(vals, [["a", "b"], ["c", "d"]]);
462460
/// ```
463-
#[cfg(feature = "unstable-grouped")]
464461
#[cfg_attr(debug_assertions, track_caller)]
465462
pub fn remove_occurrences<T: Any + Clone + Send + Sync + 'static>(
466463
&mut self,
@@ -1110,7 +1107,6 @@ impl ArgMatches {
11101107
}
11111108

11121109
/// Non-panicking version of [`ArgMatches::get_occurrences`]
1113-
#[cfg(feature = "unstable-grouped")]
11141110
pub fn try_get_occurrences<T: Any + Clone + Send + Sync + 'static>(
11151111
&self,
11161112
id: &str,
@@ -1143,7 +1139,6 @@ impl ArgMatches {
11431139
}
11441140

11451141
/// Non-panicking version of [`ArgMatches::get_raw_occurrences`]
1146-
#[cfg(feature = "unstable-grouped")]
11471142
pub fn try_get_raw_occurrences(
11481143
&self,
11491144
id: &str,
@@ -1196,7 +1191,6 @@ impl ArgMatches {
11961191
}
11971192

11981193
/// Non-panicking version of [`ArgMatches::remove_occurrences`]
1199-
#[cfg(feature = "unstable-grouped")]
12001194
pub fn try_remove_occurrences<T: Any + Clone + Send + Sync + 'static>(
12011195
&mut self,
12021196
id: &str,
@@ -1868,9 +1862,9 @@ impl<'a> Default for Indices<'a> {
18681862
}
18691863
}
18701864

1871-
#[cfg(feature = "unstable-grouped")]
18721865
#[cfg_attr(debug_assertions, track_caller)]
18731866
#[inline]
1867+
#[cfg(feature = "unstable-grouped")]
18741868
fn unwrap_string(value: &AnyValue) -> &str {
18751869
match value.downcast_ref::<String>() {
18761870
Some(value) => value,

src/parser/matches/matched_arg.rs

-3
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,10 @@ impl MatchedArg {
7575
self.indices.push(index)
7676
}
7777

78-
#[cfg(feature = "unstable-grouped")]
7978
pub(crate) fn vals(&self) -> Iter<Vec<AnyValue>> {
8079
self.vals.iter()
8180
}
8281

83-
#[cfg(feature = "unstable-grouped")]
8482
pub(crate) fn into_vals(self) -> Vec<Vec<AnyValue>> {
8583
self.vals
8684
}
@@ -93,7 +91,6 @@ impl MatchedArg {
9391
self.vals.into_iter().flatten()
9492
}
9593

96-
#[cfg(feature = "unstable-grouped")]
9794
pub(crate) fn raw_vals(&self) -> Iter<Vec<OsString>> {
9895
self.raw_vals.iter()
9996
}

tests/builder/occurrences.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![cfg(feature = "unstable-grouped")]
2-
31
use clap::{Arg, ArgAction, ArgMatches, Command};
42

53
fn occurrences_as_vec_vec<'a>(m: &'a ArgMatches, name: &str) -> Vec<Vec<&'a String>> {

tests/derive/occurrences.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg(all(feature = "unstable-grouped", feature = "unstable-v5"))]
1+
#![cfg(feature = "unstable-v5")]
22
use clap::Parser;
33

44
#[test]

tests/examples.rs

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ fn example_tests() {
2727
"wrap_help",
2828
#[cfg(feature = "unstable-replace")]
2929
"unstable-replace",
30-
#[cfg(feature = "unstable-grouped")]
31-
"unstable-grouped",
3230
]
3331
.join(" ");
3432
t.register_bins(trycmd::cargo::compile_examples(["--features", &features]).unwrap());

tests/ui.rs

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ fn ui_tests() {
2727
"wrap_help",
2828
#[cfg(feature = "unstable-replace")]
2929
"unstable-replace",
30-
#[cfg(feature = "unstable-grouped")]
31-
"unstable-grouped",
3230
]
3331
.join(" ");
3432
t.register_bins(trycmd::cargo::compile_examples(["--features", &features]).unwrap());

0 commit comments

Comments
 (0)