add -e flag to print, to print the value to stderr (#5935)

* Refactor: make stdout write all and flush as generic function

* support print to stderr
This commit is contained in:
WindSoilder
2022-07-02 22:54:49 +08:00
committed by GitHub
parent be7f35246e
commit 84caf8859f
8 changed files with 64 additions and 16 deletions

View File

@ -21,6 +21,7 @@ impl Command for Print {
"print without inserting a newline for the line ending",
Some('n'),
)
.switch("stderr", "print to stderr instead of stdout", Some('e'))
.category(Category::Strings)
}
@ -48,11 +49,12 @@ Since this command has no output, there is no point in piping it with other comm
) -> Result<PipelineData, ShellError> {
let args: Vec<Value> = call.rest(engine_state, stack, 0)?;
let no_newline = call.has_flag("no-newline");
let to_stderr = call.has_flag("stderr");
let head = call.head;
for arg in args {
arg.into_pipeline_data()
.print(engine_state, stack, no_newline)?;
.print(engine_state, stack, no_newline, to_stderr)?;
}
Ok(PipelineData::new(head))

View File

@ -247,7 +247,7 @@ pub fn eval_source(
set_last_exit_code(stack, 0);
}
if let Err(err) = pipeline_data.print(engine_state, stack, false) {
if let Err(err) = pipeline_data.print(engine_state, stack, false, false) {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err);