Declare input and output types of commands (#6796)

* Add failing test that list of ints and floats is List<Number>

* Start defining subtype relation

* Make it possible to declare input and output types for commands

- Enforce them in tests

* Declare input and output types of commands

* Add formatted signatures to `help commands` table

* Revert SyntaxShape::Table -> Type::Table change

* Revert unnecessary derive(Hash) on SyntaxShape

Co-authored-by: JT <547158+jntrnr@users.noreply.github.com>
This commit is contained in:
Dan Davison
2022-11-09 16:55:05 -05:00
committed by GitHub
parent f878276de7
commit df94052180
238 changed files with 2315 additions and 756 deletions

View File

@ -3,7 +3,7 @@ use nu_ansi_term::*;
use nu_engine::CallExt;
use nu_protocol::{
ast::Call, engine::Command, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData,
PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use std::collections::HashMap;
@ -202,6 +202,7 @@ impl Command for AnsiCommand {
fn signature(&self) -> Signature {
Signature::build("ansi")
.input_output_types(vec![(Type::Nothing, Type::String)])
.optional(
"code",
SyntaxShape::Any,
@ -293,9 +294,13 @@ Format: #
Example {
description: "Use ansi to color text (italic bright yellow on red 'Hello' with green bold 'Nu' and purple bold 'World')",
example: r#"[(ansi -e '3;93;41m') Hello (ansi reset) " " (ansi gb) Nu " " (ansi pb) World (ansi reset)] | str join"#,
result: Some(Value::test_string(
"\u{1b}[3;93;41mHello\u{1b}[0m \u{1b}[1;32mNu \u{1b}[1;35mWorld\u{1b}[0m",
)),
result: None,
// Test disabled because the final expression in the pipeline is
// not the command being tested, and this violated assumptions
// made by the run-time input/output type-checking tests.
// result: Some(Value::test_string(
// "\u{1b}[3;93;41mHello\u{1b}[0m \u{1b}[1;32mNu \u{1b}[1;35mWorld\u{1b}[0m",
// )),
},
Example {
description: "Use ansi to color text with a style (blue on red in bold)",

View File

@ -1,7 +1,7 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call, ast::CellPath, engine::Command, engine::EngineState, engine::Stack, Category,
Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -14,6 +14,7 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("ansi strip")
.input_output_types(vec![(Type::String, Type::String)])
.rest(
"column path",
SyntaxShape::CellPath,

View File

@ -2,7 +2,8 @@ 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, Value,
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
Type, Value,
};
use std::{
sync::atomic::Ordering,
@ -26,6 +27,7 @@ impl Command for Sleep {
fn signature(&self) -> Signature {
Signature::build("sleep")
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.required("duration", SyntaxShape::Duration, "time to sleep")
.rest("rest", SyntaxShape::Duration, "additional time")
.category(Category::Platform)
@ -75,18 +77,20 @@ impl Command for Sleep {
Example {
description: "Sleep for 1sec",
example: "sleep 1sec",
result: None,
},
Example {
description: "Sleep for 3sec",
example: "sleep 1sec 1sec 1sec",
result: None,
},
Example {
description: "Send output after 1sec",
example: "sleep 1sec; echo done",
result: Some(Value::test_string("done")),
result: Some(Value::Nothing {
span: Span::test_data(),
}),
},
// Example {
// description: "Sleep for 3sec",
// example: "sleep 1sec 1sec 1sec",
// result: None,
// },
// Example {
// description: "Send output after 1sec",
// example: "sleep 1sec; echo done",
// result: None,
// },
]
}
}