Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use crate::syntax_shape::SyntaxShape;
|
2019-12-04 22:14:52 +01:00
|
|
|
use crate::type_shape::Type;
|
2019-05-26 08:54:41 +02:00
|
|
|
use indexmap::IndexMap;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_source::{b, DebugDocBuilder, PrettyDebug, PrettyDebugWithSource};
|
2019-07-02 09:56:20 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-05-26 08:54:41 +02:00
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// The types of named parameter that a command can have
|
2019-07-16 09:08:35 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2019-05-28 08:45:18 +02:00
|
|
|
pub enum NamedType {
|
2020-02-12 03:24:31 +01:00
|
|
|
/// A flag without any associated argument. eg) `foo --bar, foo -b`
|
|
|
|
Switch(Option<char>),
|
|
|
|
/// A mandatory flag, with associated argument. eg) `foo --required xyz, foo -r xyz`
|
|
|
|
Mandatory(Option<char>, SyntaxShape),
|
|
|
|
/// An optional flag, with associated argument. eg) `foo --optional abc, foo -o abc`
|
|
|
|
Optional(Option<char>, SyntaxShape),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NamedType {
|
|
|
|
pub fn get_short(&self) -> Option<char> {
|
|
|
|
match self {
|
|
|
|
NamedType::Switch(s) => *s,
|
|
|
|
NamedType::Mandatory(s, _) => *s,
|
|
|
|
NamedType::Optional(s, _) => *s,
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 08:54:41 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// The type of positional arguments
|
2020-10-28 18:49:38 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
2019-05-28 08:45:18 +02:00
|
|
|
pub enum PositionalType {
|
2020-02-12 03:24:31 +01:00
|
|
|
/// A mandatory positional argument with the expected shape of the value
|
2019-09-14 18:30:24 +02:00
|
|
|
Mandatory(String, SyntaxShape),
|
2020-01-15 19:32:46 +01:00
|
|
|
/// An optional positional argument with the expected shape of the value
|
2019-09-14 18:30:24 +02:00
|
|
|
Optional(String, SyntaxShape),
|
2019-05-28 08:45:18 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl PrettyDebug for PositionalType {
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Prepare the PositionalType for pretty-printing
|
2019-11-21 15:33:14 +01:00
|
|
|
fn pretty(&self) -> DebugDocBuilder {
|
|
|
|
match self {
|
|
|
|
PositionalType::Mandatory(string, shape) => {
|
2019-12-07 10:34:32 +01:00
|
|
|
b::description(string) + b::delimit("(", shape.pretty(), ")").into_kind().group()
|
2019-11-21 15:33:14 +01:00
|
|
|
}
|
|
|
|
PositionalType::Optional(string, shape) => {
|
|
|
|
b::description(string)
|
|
|
|
+ b::operator("?")
|
2019-12-07 10:34:32 +01:00
|
|
|
+ b::delimit("(", shape.pretty(), ")").into_kind().group()
|
2019-11-21 15:33:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 08:45:18 +02:00
|
|
|
impl PositionalType {
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Helper to create a mandatory positional argument type
|
2019-09-14 18:30:24 +02:00
|
|
|
pub fn mandatory(name: &str, ty: SyntaxShape) -> PositionalType {
|
2019-07-15 23:16:27 +02:00
|
|
|
PositionalType::Mandatory(name.to_string(), ty)
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Helper to create a mandatory positional argument with an "any" type
|
2019-07-15 23:16:27 +02:00
|
|
|
pub fn mandatory_any(name: &str) -> PositionalType {
|
2019-09-14 18:30:24 +02:00
|
|
|
PositionalType::Mandatory(name.to_string(), SyntaxShape::Any)
|
2019-07-13 04:07:06 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Helper to create a mandatory positional argument with a block type
|
2019-07-16 21:10:25 +02:00
|
|
|
pub fn mandatory_block(name: &str) -> PositionalType {
|
2019-09-14 18:30:24 +02:00
|
|
|
PositionalType::Mandatory(name.to_string(), SyntaxShape::Block)
|
2019-07-16 21:10:25 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Helper to create a optional positional argument type
|
2019-09-14 18:30:24 +02:00
|
|
|
pub fn optional(name: &str, ty: SyntaxShape) -> PositionalType {
|
2019-07-16 09:25:48 +02:00
|
|
|
PositionalType::Optional(name.to_string(), ty)
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Helper to create a optional positional argument with an "any" type
|
2019-07-16 09:25:48 +02:00
|
|
|
pub fn optional_any(name: &str) -> PositionalType {
|
2019-09-14 18:30:24 +02:00
|
|
|
PositionalType::Optional(name.to_string(), SyntaxShape::Any)
|
2019-07-16 09:25:48 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Gets the name of the positional argument
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
pub fn name(&self) -> &str {
|
2019-06-30 08:14:40 +02:00
|
|
|
match self {
|
2019-07-03 22:31:15 +02:00
|
|
|
PositionalType::Mandatory(s, _) => s,
|
|
|
|
PositionalType::Optional(s, _) => s,
|
2019-06-30 08:14:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-15 23:16:27 +02:00
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Gets the expected type of a positional argument
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
pub fn syntax_type(&self) -> SyntaxShape {
|
2019-07-15 23:16:27 +02:00
|
|
|
match *self {
|
|
|
|
PositionalType::Mandatory(_, t) => t,
|
|
|
|
PositionalType::Optional(_, t) => t,
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 08:45:18 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 06:15:35 +01:00
|
|
|
type Description = String;
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// The full signature of a command. All commands have a signature similar to a function signature.
|
|
|
|
/// Commands will use this information to register themselves with Nu's core engine so that the command
|
|
|
|
/// can be invoked, help can be displayed, and calls to the command can be error-checked.
|
2019-11-18 04:12:37 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2019-08-02 21:15:07 +02:00
|
|
|
pub struct Signature {
|
2020-01-15 19:32:46 +01:00
|
|
|
/// The name of the command. Used when calling the command
|
2019-07-02 09:56:20 +02:00
|
|
|
pub name: String,
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Usage instructions about the command
|
2019-08-30 00:52:32 +02:00
|
|
|
pub usage: String,
|
2020-01-15 19:32:46 +01:00
|
|
|
/// The list of positional arguments, both required and optional, and their corresponding types and help text
|
2019-10-28 06:15:35 +01:00
|
|
|
pub positional: Vec<(PositionalType, Description)>,
|
2020-01-15 19:32:46 +01:00
|
|
|
/// After the positional arguments, a catch-all for the rest of the arguments that might follow, their type, and help text
|
2019-10-28 06:15:35 +01:00
|
|
|
pub rest_positional: Option<(SyntaxShape, Description)>,
|
2020-01-15 19:32:46 +01:00
|
|
|
/// The named flags with corresponding type and help text
|
2019-10-28 06:15:35 +01:00
|
|
|
pub named: IndexMap<String, (NamedType, Description)>,
|
2020-01-15 19:32:46 +01:00
|
|
|
/// The type of values being sent out from the command into the pipeline, if any
|
2019-12-04 22:14:52 +01:00
|
|
|
pub yields: Option<Type>,
|
2020-01-15 19:32:46 +01:00
|
|
|
/// The type of values being read in from the pipeline into the command, if any
|
2019-12-04 22:14:52 +01:00
|
|
|
pub input: Option<Type>,
|
2020-01-15 19:32:46 +01:00
|
|
|
/// If the command is expected to filter data, or to consume it (as a sink)
|
2019-07-02 09:56:20 +02:00
|
|
|
pub is_filter: bool,
|
2019-05-28 08:45:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
impl PartialEq for Signature {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.name == other.name
|
|
|
|
&& self.usage == other.usage
|
|
|
|
&& self.positional == other.positional
|
|
|
|
&& self.rest_positional == other.rest_positional
|
|
|
|
&& self.is_filter == other.is_filter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Signature {}
|
|
|
|
|
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
|
|
|
impl Signature {
|
|
|
|
pub fn shift_positional(&mut self) {
|
|
|
|
self.positional = Vec::from(&self.positional[1..]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_named(&mut self, name: &str) {
|
|
|
|
self.named.remove(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn allowed(&self) -> Vec<String> {
|
|
|
|
let mut allowed = indexmap::IndexSet::new();
|
|
|
|
|
2020-02-12 03:24:31 +01:00
|
|
|
for (name, (t, _)) in &self.named {
|
|
|
|
if let Some(c) = t.get_short() {
|
|
|
|
allowed.insert(format!("-{}", c));
|
|
|
|
}
|
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
|
|
|
allowed.insert(format!("--{}", name));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ty, _) in &self.positional {
|
|
|
|
let shape = ty.syntax_type();
|
|
|
|
allowed.insert(shape.display());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((shape, _)) = &self.rest_positional {
|
|
|
|
allowed.insert(shape.display());
|
|
|
|
}
|
|
|
|
|
|
|
|
allowed.into_iter().collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl PrettyDebugWithSource for Signature {
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Prepare a Signature for pretty-printing
|
2019-11-21 15:33:14 +01:00
|
|
|
fn pretty_debug(&self, source: &str) -> DebugDocBuilder {
|
|
|
|
b::typed(
|
|
|
|
"signature",
|
|
|
|
b::description(&self.name)
|
|
|
|
+ b::preceded(
|
|
|
|
b::space(),
|
|
|
|
b::intersperse(
|
|
|
|
self.positional
|
|
|
|
.iter()
|
|
|
|
.map(|(ty, _)| ty.pretty_debug(source)),
|
|
|
|
b::space(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
impl Signature {
|
2020-02-12 03:24:31 +01:00
|
|
|
/// Create a new command signature with the given name
|
2019-12-04 22:14:52 +01:00
|
|
|
pub fn new(name: impl Into<String>) -> Signature {
|
2019-11-18 04:12:37 +01:00
|
|
|
Signature {
|
2019-12-04 22:14:52 +01:00
|
|
|
name: name.into(),
|
2019-11-18 04:12:37 +01:00
|
|
|
usage: String::new(),
|
|
|
|
positional: vec![],
|
|
|
|
rest_positional: None,
|
2020-02-12 03:24:31 +01:00
|
|
|
named: indexmap::indexmap! {"help".into() => (NamedType::Switch(Some('h')), "Display this help message".into())},
|
2019-11-18 04:12:37 +01:00
|
|
|
is_filter: false,
|
2019-12-04 22:14:52 +01:00
|
|
|
yields: None,
|
|
|
|
input: None,
|
2019-11-18 04:12:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Create a new signature
|
2019-08-02 21:15:07 +02:00
|
|
|
pub fn build(name: impl Into<String>) -> Signature {
|
|
|
|
Signature::new(name.into())
|
2019-07-24 06:10:48 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Add a description to the signature
|
2019-08-30 00:52:32 +02:00
|
|
|
pub fn desc(mut self, usage: impl Into<String>) -> Signature {
|
|
|
|
self.usage = usage.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Add a required positional argument to the signature
|
2019-10-28 06:15:35 +01:00
|
|
|
pub fn required(
|
|
|
|
mut self,
|
|
|
|
name: impl Into<String>,
|
|
|
|
ty: impl Into<SyntaxShape>,
|
|
|
|
desc: impl Into<String>,
|
|
|
|
) -> Signature {
|
|
|
|
self.positional.push((
|
|
|
|
PositionalType::Mandatory(name.into(), ty.into()),
|
|
|
|
desc.into(),
|
|
|
|
));
|
2019-07-24 06:10:48 +02:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Add an optional positional argument to the signature
|
2019-10-28 06:15:35 +01:00
|
|
|
pub fn optional(
|
|
|
|
mut self,
|
|
|
|
name: impl Into<String>,
|
|
|
|
ty: impl Into<SyntaxShape>,
|
|
|
|
desc: impl Into<String>,
|
|
|
|
) -> Signature {
|
|
|
|
self.positional.push((
|
|
|
|
PositionalType::Optional(name.into(), ty.into()),
|
|
|
|
desc.into(),
|
|
|
|
));
|
2019-07-24 06:10:48 +02:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Add an optional named flag argument to the signature
|
2019-10-28 06:15:35 +01:00
|
|
|
pub fn named(
|
|
|
|
mut self,
|
|
|
|
name: impl Into<String>,
|
|
|
|
ty: impl Into<SyntaxShape>,
|
|
|
|
desc: impl Into<String>,
|
2020-02-12 03:24:31 +01:00
|
|
|
short: Option<char>,
|
2019-10-28 06:15:35 +01:00
|
|
|
) -> Signature {
|
2020-07-17 19:57:15 +02:00
|
|
|
let s = short.map(|c| {
|
2020-02-12 03:24:31 +01:00
|
|
|
debug_assert!(!self.get_shorts().contains(&c));
|
2020-07-17 19:57:15 +02:00
|
|
|
c
|
2020-02-12 03:24:31 +01:00
|
|
|
});
|
|
|
|
self.named.insert(
|
|
|
|
name.into(),
|
|
|
|
(NamedType::Optional(s, ty.into()), desc.into()),
|
|
|
|
);
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Add a required named flag argument to the signature
|
2019-08-02 21:15:07 +02:00
|
|
|
pub fn required_named(
|
|
|
|
mut self,
|
|
|
|
name: impl Into<String>,
|
2019-09-14 18:30:24 +02:00
|
|
|
ty: impl Into<SyntaxShape>,
|
2019-10-28 06:15:35 +01:00
|
|
|
desc: impl Into<String>,
|
2020-02-12 03:24:31 +01:00
|
|
|
short: Option<char>,
|
2019-08-02 21:15:07 +02:00
|
|
|
) -> Signature {
|
2020-07-17 19:57:15 +02:00
|
|
|
let s = short.map(|c| {
|
2020-02-12 03:24:31 +01:00
|
|
|
debug_assert!(!self.get_shorts().contains(&c));
|
2020-07-17 19:57:15 +02:00
|
|
|
c
|
2020-02-12 03:24:31 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
self.named.insert(
|
|
|
|
name.into(),
|
|
|
|
(NamedType::Mandatory(s, ty.into()), desc.into()),
|
|
|
|
);
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Add a switch to the signature
|
2020-02-12 03:24:31 +01:00
|
|
|
pub fn switch(
|
|
|
|
mut self,
|
|
|
|
name: impl Into<String>,
|
|
|
|
desc: impl Into<String>,
|
|
|
|
short: Option<char>,
|
|
|
|
) -> Signature {
|
2020-07-17 19:57:15 +02:00
|
|
|
let s = short.map(|c| {
|
2020-07-11 23:49:44 +02:00
|
|
|
debug_assert!(
|
|
|
|
!self.get_shorts().contains(&c),
|
|
|
|
"There may be duplicate short flags, such as -h"
|
|
|
|
);
|
2020-07-17 19:57:15 +02:00
|
|
|
c
|
2020-02-12 03:24:31 +01:00
|
|
|
});
|
|
|
|
|
2019-10-28 06:15:35 +01:00
|
|
|
self.named
|
2020-02-12 03:24:31 +01:00
|
|
|
.insert(name.into(), (NamedType::Switch(s), desc.into()));
|
2020-01-17 23:46:18 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Set the filter flag for the signature
|
2019-08-02 21:15:07 +02:00
|
|
|
pub fn filter(mut self) -> Signature {
|
|
|
|
self.is_filter = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Set the type for the "rest" of the positional arguments
|
2020-08-21 19:51:29 +02:00
|
|
|
/// Note: Not naming the field in your struct holding the rest values "rest", can
|
|
|
|
/// cause errors when deserializing
|
2019-10-28 06:15:35 +01:00
|
|
|
pub fn rest(mut self, ty: SyntaxShape, desc: impl Into<String>) -> Signature {
|
|
|
|
self.rest_positional = Some((ty, desc.into()));
|
2019-08-02 21:15:07 +02:00
|
|
|
self
|
|
|
|
}
|
2019-12-04 22:14:52 +01:00
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Add a type for the output of the command to the signature
|
2019-12-04 22:14:52 +01:00
|
|
|
pub fn yields(mut self, ty: Type) -> Signature {
|
|
|
|
self.yields = Some(ty);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:32:46 +01:00
|
|
|
/// Add a type for the input of the command to the signature
|
2019-12-04 22:14:52 +01:00
|
|
|
pub fn input(mut self, ty: Type) -> Signature {
|
|
|
|
self.input = Some(ty);
|
|
|
|
self
|
|
|
|
}
|
2020-02-12 03:24:31 +01:00
|
|
|
|
|
|
|
/// Get list of the short-hand flags
|
|
|
|
pub fn get_shorts(&self) -> Vec<char> {
|
|
|
|
let mut shorts = Vec::new();
|
|
|
|
for (_, (t, _)) in &self.named {
|
|
|
|
if let Some(c) = t.get_short() {
|
|
|
|
shorts.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shorts
|
|
|
|
}
|
2019-07-24 06:10:48 +02:00
|
|
|
}
|