Skip to content

Commit

Permalink
params: non strict positional should not suggest --
Browse files Browse the repository at this point in the history
While at it - make Positional into Copy and make match exhaustive
  • Loading branch information
pacak committed Jul 28, 2024
1 parent 0ce96d0 commit 9b07719
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
26 changes: 14 additions & 12 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ pub struct ParsePositional<T> {
ty: PhantomData<T>,
}

#[derive(Clone, PartialEq, Eq)]
#[derive(Copy, Clone, PartialEq, Eq)]
enum Position {
Unrestricted,
Strict,
Expand Down Expand Up @@ -811,22 +811,24 @@ fn parse_pos_word(
args: &mut State,
metavar: Metavar,
help: &Option<Doc>,
position: &Position,
position: Position,
) -> Result<OsString, Error> {
match args.take_positional_word(metavar) {
Ok((ix, is_strict, word)) => {
match position {
&Position::Strict if !is_strict => {
#[cfg(feature = "autocomplete")]
args.push_pos_sep();
return Err(Error(Message::StrictPos(ix, metavar)));
Position::Strict => {
if !is_strict {
#[cfg(feature = "autocomplete")]
args.push_pos_sep();
return Err(Error(Message::StrictPos(ix, metavar)));
}
}
&Position::NonStrict if is_strict => {
#[cfg(feature = "autocomplete")]
args.push_pos_sep();
return Err(Error(Message::NonStrictPos(ix, metavar)));
Position::NonStrict => {
if is_strict {
return Err(Error(Message::NonStrictPos(ix, metavar)));
}
}
_ => {}
Position::Unrestricted => {}
}

#[cfg(feature = "autocomplete")]
Expand All @@ -853,7 +855,7 @@ where
<T as std::str::FromStr>::Err: std::fmt::Display,
{
fn eval(&self, args: &mut State) -> Result<T, Error> {
let os = parse_pos_word(args, Metavar(self.metavar), &self.help, &self.position)?;
let os = parse_pos_word(args, Metavar(self.metavar), &self.help, self.position)?;
match parse_os_str::<T>(os) {
Ok(ok) => Ok(ok),
Err(err) => Err(Error(Message::ParseFailed(args.current, err))),
Expand Down
8 changes: 7 additions & 1 deletion tests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ fn should_be_able_to_suggest_double_dash() {
}

#[test]
fn dont_suggest_double_dash_automatically_for_nonstrictly_positional() {
fn non_strict_and_double_dash() {
let a = short('a').switch();
let b = positional::<String>("B").non_strict();
let parser = construct!(a, b).to_options();
Expand All @@ -953,6 +953,12 @@ fn dont_suggest_double_dash_automatically_for_nonstrictly_positional() {
-a\t-a\t\t
\tB\t\t\n\n"
);

let r = parser
.run_inner(Args::from(&["--", ""]).set_comp(0))
.unwrap_err()
.unwrap_stdout();
assert_eq!(r, "\n");
}

#[test]
Expand Down

0 comments on commit 9b07719

Please sign in to comment.