mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +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:
@ -67,7 +67,7 @@ impl Command for ToCsv {
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let noheaders = call.has_flag("noheaders");
|
||||
let noheaders = call.has_flag(engine_state, stack, "noheaders")?;
|
||||
let separator: Option<Spanned<String>> = call.get_flag(engine_state, stack, "separator")?;
|
||||
let config = engine_state.get_config();
|
||||
to_csv(input, noheaders, separator, head, config)
|
||||
|
@ -44,8 +44,8 @@ impl Command for ToJson {
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let raw = call.has_flag("raw");
|
||||
let use_tabs = call.has_flag("tabs");
|
||||
let raw = call.has_flag(engine_state, stack, "raw")?;
|
||||
let use_tabs = call.has_flag(engine_state, stack, "tabs")?;
|
||||
|
||||
let span = call.head;
|
||||
// allow ranges to expand and turn into array
|
||||
|
@ -1,5 +1,6 @@
|
||||
use indexmap::map::IndexMap;
|
||||
use nu_cmd_base::formats::to::delimited::merge_descriptors;
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
@ -67,13 +68,13 @@ impl Command for ToMd {
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let pretty = call.has_flag("pretty");
|
||||
let per_element = call.has_flag("per-element");
|
||||
let pretty = call.has_flag(engine_state, stack, "pretty")?;
|
||||
let per_element = call.has_flag(engine_state, stack, "per-element")?;
|
||||
let config = engine_state.get_config();
|
||||
to_md(input, pretty, per_element, config, head)
|
||||
}
|
||||
|
@ -53,20 +53,18 @@ impl Command for ToNuon {
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let raw = call.has_flag("raw");
|
||||
let use_tabs = call.has_flag("tabs");
|
||||
let use_indent = call.has_flag("indent");
|
||||
let raw = call.has_flag(engine_state, stack, "raw")?;
|
||||
let tabs: Option<usize> = call.get_flag(engine_state, stack, "tabs")?;
|
||||
let indent: Option<usize> = call.get_flag(engine_state, stack, "indent")?;
|
||||
|
||||
let span = call.head;
|
||||
let value = input.into_value(span);
|
||||
|
||||
let nuon_result = if raw {
|
||||
value_to_string(&value, span, 0, None)
|
||||
} else if use_tabs {
|
||||
let tab_count: usize = call.get_flag(engine_state, stack, "tabs")?.unwrap_or(1);
|
||||
} else if let Some(tab_count) = tabs {
|
||||
value_to_string(&value, span, 0, Some(&"\t".repeat(tab_count)))
|
||||
} else if use_indent {
|
||||
let indent: usize = call.get_flag(engine_state, stack, "indent")?.unwrap_or(2);
|
||||
} else if let Some(indent) = indent {
|
||||
value_to_string(&value, span, 0, Some(&" ".repeat(indent)))
|
||||
} else {
|
||||
value_to_string(&value, span, 0, None)
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::formats::to::delimited::to_delimited_data;
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
@ -49,12 +50,12 @@ impl Command for ToTsv {
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let noheaders = call.has_flag("noheaders");
|
||||
let noheaders = call.has_flag(engine_state, stack, "noheaders")?;
|
||||
let config = engine_state.get_config();
|
||||
to_tsv(input, noheaders, head, config)
|
||||
}
|
||||
|
Reference in New Issue
Block a user