mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 11:45:50 +02:00
Add "fall-through" signatures (#7527)
Fixes https://github.com/nushell/nushell/issues/4659 Fixes https://github.com/nushell/nushell/issues/5294 Fixes https://github.com/nushell/nushell/issues/6124 fix https://github.com/nushell/nushell/issues/5103
This commit is contained in:
@ -7,6 +7,7 @@ use crate::{DeclId, Span, Spanned};
|
||||
pub enum Argument {
|
||||
Positional(Expression),
|
||||
Named((Spanned<String>, Option<Spanned<String>>, Option<Expression>)),
|
||||
Unknown(Expression), // unknown argument used in "fall-through" signatures
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
@ -36,6 +37,7 @@ impl Call {
|
||||
self.arguments.iter().filter_map(|arg| match arg {
|
||||
Argument::Named(named) => Some(named),
|
||||
Argument::Positional(_) => None,
|
||||
Argument::Unknown(_) => None,
|
||||
})
|
||||
}
|
||||
|
||||
@ -46,6 +48,7 @@ impl Call {
|
||||
self.arguments.iter_mut().filter_map(|arg| match arg {
|
||||
Argument::Named(named) => Some(named),
|
||||
Argument::Positional(_) => None,
|
||||
Argument::Unknown(_) => None,
|
||||
})
|
||||
}
|
||||
|
||||
@ -64,10 +67,15 @@ impl Call {
|
||||
self.arguments.push(Argument::Positional(positional));
|
||||
}
|
||||
|
||||
pub fn add_unknown(&mut self, unknown: Expression) {
|
||||
self.arguments.push(Argument::Unknown(unknown));
|
||||
}
|
||||
|
||||
pub fn positional_iter(&self) -> impl Iterator<Item = &Expression> {
|
||||
self.arguments.iter().filter_map(|arg| match arg {
|
||||
Argument::Named(_) => None,
|
||||
Argument::Positional(positional) => Some(positional),
|
||||
Argument::Unknown(unknown) => Some(unknown),
|
||||
})
|
||||
}
|
||||
|
||||
@ -75,6 +83,7 @@ impl Call {
|
||||
self.arguments.iter_mut().filter_map(|arg| match arg {
|
||||
Argument::Named(_) => None,
|
||||
Argument::Positional(positional) => Some(positional),
|
||||
Argument::Unknown(unknown) => Some(unknown),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -118,6 +118,7 @@ pub struct Signature {
|
||||
pub allow_variants_without_examples: bool,
|
||||
pub is_filter: bool,
|
||||
pub creates_scope: bool,
|
||||
pub allows_unknown_args: bool,
|
||||
// Signature category used to classify commands stored in the list of declarations
|
||||
pub category: Category,
|
||||
}
|
||||
@ -220,6 +221,7 @@ impl Signature {
|
||||
is_filter: false,
|
||||
creates_scope: false,
|
||||
category: Category::Default,
|
||||
allows_unknown_args: false,
|
||||
}
|
||||
}
|
||||
|
||||
@ -274,6 +276,12 @@ impl Signature {
|
||||
self
|
||||
}
|
||||
|
||||
/// Allow unknown signature parameters
|
||||
pub fn allows_unknown_args(mut self) -> Signature {
|
||||
self.allows_unknown_args = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// Add a required positional argument to the signature
|
||||
pub fn required(
|
||||
mut self,
|
||||
|
Reference in New Issue
Block a user