remove --not flag for 'str contains' (#12837)

# Description
This PR resolves an inconsistency between different `str` subcommands,
notably `str contains`, `str starts-with` and `str ends-with`. Only the
`str contains` command has the `--not` flag and a desicion was made in
this #12781 PR to remove the `--not` flag and use the `not` operator
instead.

Before:
`"blob" | str contains --not o`
After:
`not ("blob" | str contains o)` OR `"blob" | str contains o | not $in`

> Note, you can currently do all three, but the first will be broken
after this PR is merged.

# User-Facing Changes
- remove `--not(-n)` flag from `str contains` command
  - This is a breaking change!

# Tests + Formatting
- [x] Added tests
- [x] Ran `cargo fmt --all`
- [x] Ran `cargo clippy --workspace -- -D warnings -D
clippy::unwrap_used`
- [x] Ran `cargo test --workspace`
- [ ] Ran `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"`
    - I was unable to get this working.
```
Error: nu::parser::export_not_found

  × Export not found.
   ╭─[source:1:9]
 1 │ use std testing; testing run-tests --path crates/nu-std
   ·         ───┬───
   ·            ╰── could not find imports
   ╰────
```
^ I still can't figure out how to make this work 😂 

# After Submitting
Requires update of documentation
This commit is contained in:
Brage Ingebrigtsen 2024-05-12 01:13:36 +02:00 committed by GitHub
parent cab86f49c0
commit 075535f869
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -40,7 +40,7 @@ impl Command for SubCommand {
"For a data structure input, check strings at the given cell paths, and replace with result.", "For a data structure input, check strings at the given cell paths, and replace with result.",
) )
.switch("ignore-case", "search is case insensitive", Some('i')) .switch("ignore-case", "search is case insensitive", Some('i'))
.switch("not", "does not contain", Some('n')) .switch("not", "DEPRECATED OPTION: does not contain", Some('n'))
.category(Category::Strings) .category(Category::Strings)
} }
@ -59,6 +59,20 @@ impl Command for SubCommand {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
if call.has_flag(engine_state, stack, "not")? {
nu_protocol::report_error_new(
engine_state,
&ShellError::GenericError {
error: "Deprecated option".into(),
msg: "`str contains --not {string}` is deprecated and will be removed in 0.95."
.into(),
span: Some(call.head),
help: Some("Please use the `not` operator instead.".into()),
inner: vec![],
},
);
}
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?; let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths); let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
let args = Arguments { let args = Arguments {
@ -120,15 +134,6 @@ impl Command for SubCommand {
Value::test_bool(false), Value::test_bool(false),
])), ])),
}, },
Example {
description: "Check if list does not contain string",
example: "[one two three] | str contains --not o",
result: Some(Value::test_list(vec![
Value::test_bool(false),
Value::test_bool(false),
Value::test_bool(true),
])),
},
] ]
} }
} }