mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 16:58:39 +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:
@ -653,9 +653,9 @@ Operating system commands:
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let list: bool = call.has_flag("list");
|
||||
let escape: bool = call.has_flag("escape");
|
||||
let osc: bool = call.has_flag("osc");
|
||||
let list: bool = call.has_flag(engine_state, stack, "list")?;
|
||||
let escape: bool = call.has_flag(engine_state, stack, "escape")?;
|
||||
let osc: bool = call.has_flag(engine_state, stack, "osc")?;
|
||||
let use_ansi_coloring = engine_state.get_config().use_ansi_coloring;
|
||||
|
||||
if list {
|
||||
|
@ -98,8 +98,8 @@ impl Command for Du {
|
||||
|
||||
let args = DuArgs {
|
||||
path: call.opt(engine_state, stack, 0)?,
|
||||
all: call.has_flag("all"),
|
||||
deref: call.has_flag("deref"),
|
||||
all: call.has_flag(engine_state, stack, "all")?,
|
||||
deref: call.has_flag(engine_state, stack, "deref")?,
|
||||
exclude: call.get_flag(engine_state, stack, "exclude")?,
|
||||
max_depth,
|
||||
min_size,
|
||||
|
@ -65,7 +65,7 @@ impl Command for Input {
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let prompt: Option<String> = call.opt(engine_state, stack, 0)?;
|
||||
let bytes_until: Option<String> = call.get_flag(engine_state, stack, "bytes-until-any")?;
|
||||
let suppress_output = call.has_flag("suppress-output");
|
||||
let suppress_output = call.has_flag(engine_state, stack, "suppress-output")?;
|
||||
let numchar: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "numchar")?;
|
||||
let numchar: Spanned<i64> = numchar.unwrap_or(Spanned {
|
||||
item: i64::MAX,
|
||||
|
@ -85,7 +85,7 @@ There are 4 `key_type` variants:
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let event_type_filter = get_event_type_filter(engine_state, stack, call, head)?;
|
||||
let add_raw = call.has_flag("raw");
|
||||
let add_raw = call.has_flag(engine_state, stack, "raw")?;
|
||||
|
||||
terminal::enable_raw_mode()?;
|
||||
let console_state = event_type_filter.enable_events()?;
|
||||
|
@ -78,6 +78,8 @@ impl Command for InputList {
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let prompt: Option<String> = call.opt(engine_state, stack, 0)?;
|
||||
let multi = call.has_flag(engine_state, stack, "multi")?;
|
||||
let fuzzy = call.has_flag(engine_state, stack, "fuzzy")?;
|
||||
|
||||
let options: Vec<Options> = match input {
|
||||
PipelineData::Value(Value::Range { .. }, ..)
|
||||
@ -105,7 +107,7 @@ impl Command for InputList {
|
||||
});
|
||||
}
|
||||
|
||||
if call.has_flag("multi") && call.has_flag("fuzzy") {
|
||||
if multi && fuzzy {
|
||||
return Err(ShellError::TypeMismatch {
|
||||
err_message: "Fuzzy search is not supported for multi select".to_string(),
|
||||
span: head,
|
||||
@ -118,7 +120,7 @@ impl Command for InputList {
|
||||
// ..Default::default()
|
||||
// };
|
||||
|
||||
let ans: InteractMode = if call.has_flag("multi") {
|
||||
let ans: InteractMode = if multi {
|
||||
let multi_select = MultiSelect::new(); //::with_theme(&theme);
|
||||
|
||||
InteractMode::Multi(
|
||||
@ -134,7 +136,7 @@ impl Command for InputList {
|
||||
msg: format!("{}: {}", INTERACT_ERROR, err),
|
||||
})?,
|
||||
)
|
||||
} else if call.has_flag("fuzzy") {
|
||||
} else if fuzzy {
|
||||
let fuzzy_select = FuzzySelect::new(); //::with_theme(&theme);
|
||||
|
||||
InteractMode::Single(
|
||||
|
@ -1,3 +1,4 @@
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EngineState, Stack},
|
||||
@ -40,14 +41,14 @@ impl Command for IsTerminal {
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let stdin = call.has_flag("stdin");
|
||||
let stdout = call.has_flag("stdout");
|
||||
let stderr = call.has_flag("stderr");
|
||||
let stdin = call.has_flag(engine_state, stack, "stdin")?;
|
||||
let stdout = call.has_flag(engine_state, stack, "stdout")?;
|
||||
let stderr = call.has_flag(engine_state, stack, "stderr")?;
|
||||
|
||||
let is_terminal = match (stdin, stdout, stderr) {
|
||||
(true, false, false) => std::io::stdin().is_terminal(),
|
||||
|
@ -58,9 +58,9 @@ impl Command for Kill {
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let pid: i64 = call.req(engine_state, stack, 0)?;
|
||||
let rest: Vec<i64> = call.rest(engine_state, stack, 1)?;
|
||||
let force: bool = call.has_flag("force");
|
||||
let force: bool = call.has_flag(engine_state, stack, "force")?;
|
||||
let signal: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "signal")?;
|
||||
let quiet: bool = call.has_flag("quiet");
|
||||
let quiet: bool = call.has_flag(engine_state, stack, "quiet")?;
|
||||
|
||||
let mut cmd = if cfg!(windows) {
|
||||
let mut cmd = CommandSys::new("taskkill");
|
||||
|
@ -267,12 +267,17 @@ fn limit_to_value(limit: rlim_t, multiplier: rlim_t, span: Span) -> Result<Value
|
||||
}
|
||||
|
||||
/// Get maximum length of all flag descriptions
|
||||
fn max_desc_len(call: &Call, print_all: bool) -> usize {
|
||||
fn max_desc_len(
|
||||
call: &Call,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
print_all: bool,
|
||||
) -> Result<usize, ShellError> {
|
||||
let mut desc_len = 0;
|
||||
let mut unit_len = 0;
|
||||
|
||||
for res in RESOURCE_ARRAY.iter() {
|
||||
if !print_all && !call.has_flag(res.name) {
|
||||
if !print_all && !call.has_flag(engine_state, stack, res.name)? {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -288,7 +293,7 @@ fn max_desc_len(call: &Call, print_all: bool) -> usize {
|
||||
}
|
||||
|
||||
// desc.len() + unit.len() + '-X)'.len()
|
||||
desc_len + unit_len + 3
|
||||
Ok(desc_len + unit_len + 3)
|
||||
}
|
||||
|
||||
/// Fill `ResourceInfo` to the record entry
|
||||
@ -359,18 +364,20 @@ fn set_limits(
|
||||
/// Print limits
|
||||
fn print_limits(
|
||||
call: &Call,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
print_all: bool,
|
||||
soft: bool,
|
||||
hard: bool,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let mut output = Vec::new();
|
||||
let mut print_default_limit = true;
|
||||
let max_len = max_desc_len(call, print_all);
|
||||
let max_len = max_desc_len(call, engine_state, stack, print_all)?;
|
||||
|
||||
for res in RESOURCE_ARRAY.iter() {
|
||||
if !print_all {
|
||||
// Print specified limit.
|
||||
if !call.has_flag(res.name) {
|
||||
if !call.has_flag(engine_state, stack, res.name)? {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@ -527,9 +534,9 @@ impl Command for ULimit {
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let mut soft = call.has_flag("soft");
|
||||
let mut hard = call.has_flag("hard");
|
||||
let all = call.has_flag("all");
|
||||
let mut soft = call.has_flag(engine_state, stack, "soft")?;
|
||||
let mut hard = call.has_flag(engine_state, stack, "hard")?;
|
||||
let all = call.has_flag(engine_state, stack, "all")?;
|
||||
|
||||
if !hard && !soft {
|
||||
// Set both hard and soft limits if neither was specified.
|
||||
@ -541,7 +548,7 @@ impl Command for ULimit {
|
||||
let mut set_default_limit = true;
|
||||
|
||||
for res in RESOURCE_ARRAY.iter() {
|
||||
if call.has_flag(res.name) {
|
||||
if call.has_flag(engine_state, stack, res.name)? {
|
||||
set_limits(&limit_value, res, soft, hard, call.head)?;
|
||||
|
||||
if set_default_limit {
|
||||
@ -558,7 +565,7 @@ impl Command for ULimit {
|
||||
|
||||
Ok(PipelineData::Empty)
|
||||
} else {
|
||||
print_limits(call, all, soft, hard)
|
||||
print_limits(call, engine_state, stack, all, soft, hard)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user