mirror of
https://github.com/nushell/nushell.git
synced 2025-05-21 02:10:47 +02:00
# 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.
88 lines
2.7 KiB
Rust
88 lines
2.7 KiB
Rust
use nu_engine::CallExt;
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{
|
|
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type,
|
|
Value,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct Print;
|
|
|
|
impl Command for Print {
|
|
fn name(&self) -> &str {
|
|
"print"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("print")
|
|
.input_output_types(vec![
|
|
(Type::Nothing, Type::Nothing),
|
|
(Type::Any, Type::Nothing),
|
|
])
|
|
.allow_variants_without_examples(true)
|
|
.rest("rest", SyntaxShape::Any, "the values to print")
|
|
.switch(
|
|
"no-newline",
|
|
"print without inserting a newline for the line ending",
|
|
Some('n'),
|
|
)
|
|
.switch("stderr", "print to stderr instead of stdout", Some('e'))
|
|
.category(Category::Strings)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Print the given values to stdout."
|
|
}
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
r#"Unlike `echo`, this command does not return any value (`print | describe` will return "nothing").
|
|
Since this command has no output, there is no point in piping it with other commands.
|
|
|
|
`print` may be used inside blocks of code (e.g.: hooks) to display text during execution without interfering with the pipeline."#
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec!["display"]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let args: Vec<Value> = call.rest(engine_state, stack, 0)?;
|
|
let no_newline = call.has_flag(engine_state, stack, "no-newline")?;
|
|
let to_stderr = call.has_flag(engine_state, stack, "stderr")?;
|
|
|
|
// This will allow for easy printing of pipelines as well
|
|
if !args.is_empty() {
|
|
for arg in args {
|
|
arg.into_pipeline_data()
|
|
.print(engine_state, stack, no_newline, to_stderr)?;
|
|
}
|
|
} else if !input.is_nothing() {
|
|
input.print(engine_state, stack, no_newline, to_stderr)?;
|
|
}
|
|
|
|
Ok(PipelineData::empty())
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Print 'hello world'",
|
|
example: r#"print "hello world""#,
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Print the sum of 2 and 3",
|
|
example: r#"print (2 + 3)"#,
|
|
result: None,
|
|
},
|
|
]
|
|
}
|
|
}
|