mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 08:26:22 +02:00
Fix incorrect handling of boolean flags for builtin commands (#11492)
# Description
Possible fix of #11456
This PR fixes a bug where builtin commands did not respect the logic of
dynamically passed boolean flags. The reason is
[has_flag](6f59abaf43/crates/nu-protocol/src/ast/call.rs (L204C5-L212C6)
)
method did not evaluate and take into consideration expression used with
flag.
To address this issue a solution is proposed:
1. `has_flag` method is moved to `CallExt` and new logic to evaluate
expression and check if it is a boolean value is added
2. `has_flag_const` method is added to `CallExt` which is a constant
version of `has_flag`
3. `has_named` method is added to `Call` which is basically the old
logic of `has_flag`
4. All usages of `has_flag` in code are updated, mostly to pass
`engine_state` and `stack` to new `has_flag`. In `run_const` commands it
is replaced with `has_flag_const`. And in a few select places: parser,
`to nuon` and `into string` old logic via `has_named` is used.
# User-Facing Changes
Explicit values of boolean flags are now respected in builtin commands.
Before:

After:

Another example:
Before:

After:

# Tests + Formatting
Added test reproducing some variants of original issue.
This commit is contained in:
@ -229,7 +229,7 @@ impl Command for Char {
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let call_span = call.head;
|
||||
// handle -l flag
|
||||
if call.has_flag("list") {
|
||||
if call.has_flag(engine_state, stack, "list")? {
|
||||
return Ok(CHAR_MAP
|
||||
.iter()
|
||||
.map(move |(name, s)| {
|
||||
@ -251,7 +251,7 @@ impl Command for Char {
|
||||
.into_pipeline_data(engine_state.ctrlc.clone()));
|
||||
}
|
||||
// handle -u flag
|
||||
if call.has_flag("integer") {
|
||||
if call.has_flag(engine_state, stack, "integer")? {
|
||||
let args: Vec<i64> = call.rest(engine_state, stack, 0)?;
|
||||
if args.is_empty() {
|
||||
return Err(ShellError::MissingParameter {
|
||||
@ -268,7 +268,7 @@ impl Command for Char {
|
||||
multi_byte.push(integer_to_unicode_char(arg, span)?)
|
||||
}
|
||||
Ok(Value::string(multi_byte, call_span).into_pipeline_data())
|
||||
} else if call.has_flag("unicode") {
|
||||
} else if call.has_flag(engine_state, stack, "unicode")? {
|
||||
let args: Vec<String> = call.rest(engine_state, stack, 0)?;
|
||||
if args.is_empty() {
|
||||
return Err(ShellError::MissingParameter {
|
||||
|
@ -103,7 +103,7 @@ fn detect_columns(
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let name_span = call.head;
|
||||
let num_rows_to_skip: Option<usize> = call.get_flag(engine_state, stack, "skip")?;
|
||||
let noheader = call.has_flag("no-headers");
|
||||
let noheader = call.has_flag(engine_state, stack, "no-headers")?;
|
||||
let range: Option<Range> = call.get_flag(engine_state, stack, "combine-columns")?;
|
||||
let ctrlc = engine_state.ctrlc.clone();
|
||||
let config = engine_state.get_config();
|
||||
|
@ -46,7 +46,7 @@ pub fn operate(
|
||||
let head = call.head;
|
||||
let character_set: Option<Spanned<String>> =
|
||||
call.get_flag(engine_state, stack, "character-set")?;
|
||||
let binary = call.has_flag("binary");
|
||||
let binary = call.has_flag(engine_state, stack, "binary")?;
|
||||
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
||||
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
|
||||
|
||||
|
@ -83,7 +83,7 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"#
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let encoding: Spanned<String> = call.req(engine_state, stack, 0)?;
|
||||
let ignore_errors = call.has_flag("ignore-errors");
|
||||
let ignore_errors = call.has_flag(engine_state, stack, "ignore-errors")?;
|
||||
|
||||
match input {
|
||||
PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::empty()),
|
||||
|
@ -53,7 +53,7 @@ impl Command for FormatDate {
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
if call.has_flag("list") {
|
||||
if call.has_flag(engine_state, stack, "list")? {
|
||||
return Ok(PipelineData::Value(
|
||||
generate_strftime_list(head, false),
|
||||
None,
|
||||
|
@ -10,26 +10,35 @@ pub use char_::Char;
|
||||
pub use detect_columns::*;
|
||||
pub use encode_decode::*;
|
||||
pub use format::*;
|
||||
use nu_engine::CallExt;
|
||||
pub use parse::*;
|
||||
pub use split::*;
|
||||
pub use str_::*;
|
||||
|
||||
use nu_protocol::{ast::Call, ShellError};
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{EngineState, Stack, StateWorkingSet},
|
||||
ShellError,
|
||||
};
|
||||
|
||||
// For handling the grapheme_cluster related flags on some commands.
|
||||
// This ensures the error messages are consistent.
|
||||
pub fn grapheme_flags(call: &Call) -> Result<bool, ShellError> {
|
||||
let g_flag = call.has_flag("grapheme-clusters");
|
||||
pub fn grapheme_flags(
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
) -> Result<bool, ShellError> {
|
||||
let g_flag = call.has_flag(engine_state, stack, "grapheme-clusters")?;
|
||||
// Check for the other flags and produce errors if they exist.
|
||||
// Note that Nushell already prevents nonexistent flags from being used with commands,
|
||||
// so this function can be reused for both the --utf-8-bytes commands and the --code-points commands.
|
||||
if g_flag && call.has_flag("utf-8-bytes") {
|
||||
if g_flag && call.has_flag(engine_state, stack, "utf-8-bytes")? {
|
||||
Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
||||
span: call.head,
|
||||
})?
|
||||
}
|
||||
if g_flag && call.has_flag("code-points") {
|
||||
if g_flag && call.has_flag(engine_state, stack, "code-points")? {
|
||||
Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
||||
span: call.head,
|
||||
@ -38,3 +47,24 @@ pub fn grapheme_flags(call: &Call) -> Result<bool, ShellError> {
|
||||
// Grapheme cluster usage is decided by the non-default -g flag
|
||||
Ok(g_flag)
|
||||
}
|
||||
|
||||
// Const version of grapheme_flags
|
||||
pub fn grapheme_flags_const(
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
) -> Result<bool, ShellError> {
|
||||
let g_flag = call.has_flag_const(working_set, "grapheme-clusters")?;
|
||||
if g_flag && call.has_flag_const(working_set, "utf-8-bytes")? {
|
||||
Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
||||
span: call.head,
|
||||
})?
|
||||
}
|
||||
if g_flag && call.has_flag_const(working_set, "code-points")? {
|
||||
Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
||||
span: call.head,
|
||||
})?
|
||||
}
|
||||
Ok(g_flag)
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ fn operate(
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let pattern: Spanned<String> = call.req(engine_state, stack, 0)?;
|
||||
let regex: bool = call.has_flag("regex");
|
||||
let regex: bool = call.has_flag(engine_state, stack, "regex")?;
|
||||
let ctrlc = engine_state.ctrlc.clone();
|
||||
|
||||
let pattern_item = pattern.item;
|
||||
|
@ -95,22 +95,23 @@ impl Command for SubCommand {
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
split_chars(engine_state, call, input)
|
||||
split_chars(engine_state, stack, call, input)
|
||||
}
|
||||
}
|
||||
|
||||
fn split_chars(
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let span = call.head;
|
||||
|
||||
let graphemes = grapheme_flags(call)?;
|
||||
let graphemes = grapheme_flags(engine_state, stack, call)?;
|
||||
input.map(
|
||||
move |x| split_chars_helper(&x, span, graphemes),
|
||||
engine_state.ctrlc.clone(),
|
||||
|
@ -119,9 +119,9 @@ fn split_column(
|
||||
let name_span = call.head;
|
||||
let separator: Spanned<String> = call.req(engine_state, stack, 0)?;
|
||||
let rest: Vec<Spanned<String>> = call.rest(engine_state, stack, 1)?;
|
||||
let collapse_empty = call.has_flag("collapse-empty");
|
||||
let collapse_empty = call.has_flag(engine_state, stack, "collapse-empty")?;
|
||||
|
||||
let regex = if call.has_flag("regex") {
|
||||
let regex = if call.has_flag(engine_state, stack, "regex")? {
|
||||
Regex::new(&separator.item)
|
||||
} else {
|
||||
let escaped = regex::escape(&separator.item);
|
||||
|
@ -202,7 +202,7 @@ fn split_list(
|
||||
let mut returned_list = Vec::new();
|
||||
|
||||
let iter = input.into_interruptible_iter(engine_state.ctrlc.clone());
|
||||
let matcher = Matcher::new(call.has_flag("regex"), separator)?;
|
||||
let matcher = Matcher::new(call.has_flag(engine_state, stack, "regex")?, separator)?;
|
||||
for val in iter {
|
||||
if matcher.compare(&val)? {
|
||||
if !temp_list.is_empty() {
|
||||
|
@ -123,7 +123,7 @@ fn split_row(
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let name_span = call.head;
|
||||
let separator: Spanned<String> = call.req(engine_state, stack, 0)?;
|
||||
let regex = if call.has_flag("regex") {
|
||||
let regex = if call.has_flag(engine_state, stack, "regex")? {
|
||||
Regex::new(&separator.item)
|
||||
} else {
|
||||
let escaped = regex::escape(&separator.item);
|
||||
|
@ -118,26 +118,26 @@ fn split_words(
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let span = call.head;
|
||||
// let ignore_hyphenated = call.has_flag("ignore-hyphenated");
|
||||
// let ignore_apostrophes = call.has_flag("ignore-apostrophes");
|
||||
// let ignore_punctuation = call.has_flag("ignore-punctuation");
|
||||
// let ignore_hyphenated = call.has_flag(engine_state, stack, "ignore-hyphenated")?;
|
||||
// let ignore_apostrophes = call.has_flag(engine_state, stack, "ignore-apostrophes")?;
|
||||
// let ignore_punctuation = call.has_flag(engine_state, stack, "ignore-punctuation")?;
|
||||
let word_length: Option<usize> = call.get_flag(engine_state, stack, "min-word-length")?;
|
||||
|
||||
if word_length.is_none() {
|
||||
if call.has_flag("grapheme-clusters") {
|
||||
if call.has_flag(engine_state, stack, "grapheme-clusters")? {
|
||||
return Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: "--grapheme-clusters (-g) requires --min-word-length (-l)".to_string(),
|
||||
span,
|
||||
});
|
||||
}
|
||||
if call.has_flag("utf-8-bytes") {
|
||||
if call.has_flag(engine_state, stack, "utf-8-bytes")? {
|
||||
return Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: "--utf-8-bytes (-b) requires --min-word-length (-l)".to_string(),
|
||||
span,
|
||||
});
|
||||
}
|
||||
}
|
||||
let graphemes = grapheme_flags(call)?;
|
||||
let graphemes = grapheme_flags(engine_state, stack, call)?;
|
||||
|
||||
input.map(
|
||||
move |x| split_words_helper(&x, word_length, span, graphemes),
|
||||
|
@ -70,8 +70,8 @@ impl Command for SubCommand {
|
||||
let args = Arguments {
|
||||
substring: call.req::<String>(engine_state, stack, 0)?,
|
||||
cell_paths,
|
||||
case_insensitive: call.has_flag("ignore-case"),
|
||||
not_contain: call.has_flag("not"),
|
||||
case_insensitive: call.has_flag(engine_state, stack, "ignore-case")?,
|
||||
not_contain: call.has_flag(engine_state, stack, "not")?,
|
||||
};
|
||||
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ impl Command for SubCommand {
|
||||
let args = Arguments {
|
||||
substring: call.req::<String>(engine_state, stack, 0)?,
|
||||
cell_paths,
|
||||
case_insensitive: call.has_flag("ignore-case"),
|
||||
case_insensitive: call.has_flag(engine_state, stack, "ignore-case")?,
|
||||
};
|
||||
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EngineState, Stack},
|
||||
@ -186,7 +187,7 @@ impl Command for SubCommand {
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
@ -194,7 +195,7 @@ impl Command for SubCommand {
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: span });
|
||||
}
|
||||
let is_path = call.has_flag("path");
|
||||
let is_path = call.has_flag(engine_state, stack, "path")?;
|
||||
input.map(
|
||||
move |v| {
|
||||
let value_span = v.span();
|
||||
|
@ -91,9 +91,9 @@ impl Command for SubCommand {
|
||||
let args = Arguments {
|
||||
substring: substring.item,
|
||||
range: call.get_flag(engine_state, stack, "range")?,
|
||||
end: call.has_flag("end"),
|
||||
end: call.has_flag(engine_state, stack, "end")?,
|
||||
cell_paths,
|
||||
graphemes: grapheme_flags(call)?,
|
||||
graphemes: grapheme_flags(engine_state, stack, call)?,
|
||||
};
|
||||
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::grapheme_flags;
|
||||
use crate::grapheme_flags_const;
|
||||
use nu_cmd_base::input_handler::{operate, CmdArgument};
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
@ -74,7 +75,13 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
||||
run(cell_paths, engine_state, call, input)
|
||||
run(
|
||||
cell_paths,
|
||||
engine_state,
|
||||
call,
|
||||
input,
|
||||
grapheme_flags(engine_state, stack, call)?,
|
||||
)
|
||||
}
|
||||
|
||||
fn run_const(
|
||||
@ -84,7 +91,13 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let cell_paths: Vec<CellPath> = call.rest_const(working_set, 0)?;
|
||||
run(cell_paths, working_set.permanent(), call, input)
|
||||
run(
|
||||
cell_paths,
|
||||
working_set.permanent(),
|
||||
call,
|
||||
input,
|
||||
grapheme_flags_const(working_set, call)?,
|
||||
)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
@ -116,10 +129,11 @@ fn run(
|
||||
engine_state: &EngineState,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
graphemes: bool,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let args = Arguments {
|
||||
cell_paths: (!cell_paths.is_empty()).then_some(cell_paths),
|
||||
graphemes: grapheme_flags(call)?,
|
||||
graphemes,
|
||||
};
|
||||
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
}
|
||||
|
@ -90,12 +90,13 @@ impl Command for SubCommand {
|
||||
let replace: Spanned<String> = call.req(engine_state, stack, 1)?;
|
||||
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 2)?;
|
||||
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
|
||||
let literal_replace = call.has_flag("no-expand");
|
||||
let no_regex = !call.has_flag("regex") && !call.has_flag("multiline");
|
||||
let multiline = call.has_flag("multiline");
|
||||
let literal_replace = call.has_flag(engine_state, stack, "no-expand")?;
|
||||
let no_regex = !call.has_flag(engine_state, stack, "regex")?
|
||||
&& !call.has_flag(engine_state, stack, "multiline")?;
|
||||
let multiline = call.has_flag(engine_state, stack, "multiline")?;
|
||||
|
||||
let args = Arguments {
|
||||
all: call.has_flag("all"),
|
||||
all: call.has_flag(engine_state, stack, "all")?,
|
||||
find,
|
||||
replace,
|
||||
cell_paths,
|
||||
|
@ -69,7 +69,7 @@ impl Command for SubCommand {
|
||||
let args = Arguments {
|
||||
substring: substring.item,
|
||||
cell_paths,
|
||||
case_insensitive: call.has_flag("ignore-case"),
|
||||
case_insensitive: call.has_flag(engine_state, stack, "ignore-case")?,
|
||||
};
|
||||
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
}
|
||||
|
@ -102,7 +102,7 @@ impl Command for SubCommand {
|
||||
let args = Arguments {
|
||||
indexes,
|
||||
cell_paths,
|
||||
graphemes: grapheme_flags(call)?,
|
||||
graphemes: grapheme_flags(engine_state, stack, call)?,
|
||||
};
|
||||
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
}
|
||||
|
@ -107,8 +107,8 @@ impl Command for SubCommand {
|
||||
Some(_) => ActionMode::Local,
|
||||
};
|
||||
|
||||
let left = call.has_flag("left");
|
||||
let right = call.has_flag("right");
|
||||
let left = call.has_flag(engine_state, stack, "left")?;
|
||||
let right = call.has_flag(engine_state, stack, "right")?;
|
||||
let trim_side = match (left, right) {
|
||||
(true, true) => TrimSide::Both,
|
||||
(true, false) => TrimSide::Left,
|
||||
|
Reference in New Issue
Block a user