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

@@ -1,7 +1,7 @@
use std::path::Path;
use nu_engine::CallExt;
use nu_protocol::{engine::Command, Example, Signature, Span, Spanned, SyntaxShape, Value};
use nu_protocol::{engine::Command, Example, Signature, Span, Spanned, SyntaxShape, Type, Value};
use super::PathSubcommandArguments;
@@ -26,6 +26,11 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("path basename")
.input_output_types(vec![
(Type::String, Type::String),
// TODO: Why do these commands not use CellPaths in a standard way?
(Type::Table(vec![]), Type::Table(vec![])),
])
.named(
"columns",
SyntaxShape::Table,

View File

@@ -1,7 +1,7 @@
use std::path::Path;
use nu_engine::CallExt;
use nu_protocol::{engine::Command, Example, Signature, Span, Spanned, SyntaxShape, Value};
use nu_protocol::{engine::Command, Example, Signature, Span, Spanned, SyntaxShape, Type, Value};
use super::PathSubcommandArguments;
@@ -27,6 +27,7 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("path dirname")
.input_output_types(vec![(Type::String, Type::String)])
.named(
"columns",
SyntaxShape::Table,

View File

@@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
use nu_engine::{current_dir, CallExt};
use nu_path::expand_path_with;
use nu_protocol::{engine::Command, Example, Signature, Span, SyntaxShape, Value};
use nu_protocol::{engine::Command, Example, Signature, Span, SyntaxShape, Type, Value};
use super::PathSubcommandArguments;
@@ -26,12 +26,14 @@ impl Command for SubCommand {
}
fn signature(&self) -> Signature {
Signature::build("path exists").named(
"columns",
SyntaxShape::Table,
"Optionally operate by column path",
Some('c'),
)
Signature::build("path exists")
.input_output_types(vec![(Type::String, Type::Bool)])
.named(
"columns",
SyntaxShape::Table,
"Optionally operate by column path",
Some('c'),
)
}
fn usage(&self) -> &str {

View File

@@ -3,7 +3,9 @@ use std::path::Path;
use nu_engine::env::current_dir_str;
use nu_engine::CallExt;
use nu_path::{canonicalize_with, expand_path_with};
use nu_protocol::{engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Value};
use nu_protocol::{
engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::PathSubcommandArguments;
@@ -30,6 +32,7 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("path expand")
.input_output_types(vec![(Type::String, Type::String)])
.switch(
"strict",
"Throw an error if the path could not be expanded",

View File

@@ -6,7 +6,7 @@ use std::{
use nu_engine::CallExt;
use nu_protocol::{
engine::Command, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
Value,
Type, Value,
};
use super::PathSubcommandArguments;
@@ -32,6 +32,11 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("path join")
.input_output_types(vec![
(Type::String, Type::String),
(Type::List(Box::new(Type::String)), Type::String),
(Type::Table(vec![]), Type::List(Box::new(Type::String))),
])
.named(
"columns",
SyntaxShape::Table,

View File

@@ -3,7 +3,7 @@ use std::path::Path;
use indexmap::IndexMap;
use nu_engine::CallExt;
use nu_protocol::{
engine::Command, Example, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
engine::Command, Example, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
use super::PathSubcommandArguments;
@@ -29,6 +29,7 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("path parse")
.input_output_types(vec![(Type::String, Type::Record(vec![]))])
.named(
"columns",
SyntaxShape::Table,

View File

@@ -3,7 +3,7 @@ use std::path::Path;
use nu_engine::CallExt;
use nu_path::expand_to_real_path;
use nu_protocol::{
engine::Command, Example, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
engine::Command, Example, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
use super::PathSubcommandArguments;
@@ -29,6 +29,7 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("path relative-to")
.input_output_types(vec![(Type::String, Type::String)])
.required(
"path",
SyntaxShape::String,

View File

@@ -1,7 +1,9 @@
use std::path::{Component, Path};
use nu_engine::CallExt;
use nu_protocol::{engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Value};
use nu_protocol::{
engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::PathSubcommandArguments;
@@ -24,12 +26,14 @@ impl Command for SubCommand {
}
fn signature(&self) -> Signature {
Signature::build("path split").named(
"columns",
SyntaxShape::Table,
"Optionally operate by column path",
Some('c'),
)
Signature::build("path split")
.input_output_types(vec![(Type::String, Type::List(Box::new(Type::String)))])
.named(
"columns",
SyntaxShape::Table,
"Optionally operate by column path",
Some('c'),
)
}
fn usage(&self) -> &str {

View File

@@ -1,7 +1,9 @@
use std::path::Path;
use nu_engine::CallExt;
use nu_protocol::{engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Value};
use nu_protocol::{
engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::PathSubcommandArguments;
@@ -24,12 +26,14 @@ impl Command for SubCommand {
}
fn signature(&self) -> Signature {
Signature::build("path type").named(
"columns",
SyntaxShape::Table,
"Optionally operate by column path",
Some('c'),
)
Signature::build("path type")
.input_output_types(vec![(Type::String, Type::String)])
.named(
"columns",
SyntaxShape::Table,
"Optionally operate by column path",
Some('c'),
)
}
fn usage(&self) -> &str {