forked from extern/nushell
Color named type help especial case. (#1263)
Refactored out help named type as switch.
This commit is contained in:
parent
07191754bf
commit
fe4ad5f77e
@ -45,17 +45,6 @@ pub fn parse_command_tail(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NamedType::Help => {
|
|
||||||
let switch = extract_switch(name, tail);
|
|
||||||
|
|
||||||
match switch {
|
|
||||||
None => named.insert_switch(name, None),
|
|
||||||
Some((_, flag)) => {
|
|
||||||
named.insert_switch(name, Some(*flag));
|
|
||||||
return Ok(Some((None, Some(named))));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
NamedType::Mandatory(syntax_type) => {
|
NamedType::Mandatory(syntax_type) => {
|
||||||
match extract_mandatory(config, name, tail, command_span) {
|
match extract_mandatory(config, name, tail, command_span) {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@ -115,35 +104,13 @@ pub fn parse_command_tail(
|
|||||||
|
|
||||||
let mut positional = vec![];
|
let mut positional = vec![];
|
||||||
|
|
||||||
for arg in &config.positional {
|
match continue_parsing_positionals(&config, tail, &mut rest_signature, command_span) {
|
||||||
trace!(target: "nu::parse::trace_remaining", "Processing positional {:?}", arg);
|
Ok(positionals) => {
|
||||||
|
positional = positionals;
|
||||||
tail.move_to(0);
|
}
|
||||||
|
Err(reason) => {
|
||||||
let result = expand_spaced_expr(arg.0.syntax_type(), tail);
|
if found_error.is_none() && !tail.source().contains("help") {
|
||||||
|
found_error = Some(reason);
|
||||||
match result {
|
|
||||||
Err(_) => match &arg.0 {
|
|
||||||
PositionalType::Mandatory(..) => {
|
|
||||||
if found_error.is_none() {
|
|
||||||
found_error = Some(ParseError::argument_error(
|
|
||||||
config.name.clone().spanned(command_span),
|
|
||||||
ArgumentError::MissingMandatoryPositional(arg.0.name().to_string()),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
PositionalType::Optional(..) => {
|
|
||||||
if tail.expand_syntax(MaybeWhitespaceEof).is_ok() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Ok(result) => {
|
|
||||||
rest_signature.shift_positional();
|
|
||||||
positional.push(result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -213,6 +180,45 @@ pub fn parse_command_tail(
|
|||||||
Ok(Some((positional, named)))
|
Ok(Some((positional, named)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn continue_parsing_positionals(
|
||||||
|
config: &Signature,
|
||||||
|
tail: &mut TokensIterator,
|
||||||
|
rest_signature: &mut Signature,
|
||||||
|
command_span: Span,
|
||||||
|
) -> Result<Vec<SpannedExpression>, ParseError> {
|
||||||
|
let mut positional = vec![];
|
||||||
|
|
||||||
|
for arg in &config.positional {
|
||||||
|
trace!(target: "nu::parse::trace_remaining", "Processing positional {:?}", arg);
|
||||||
|
|
||||||
|
tail.move_to(0);
|
||||||
|
|
||||||
|
let result = expand_spaced_expr(arg.0.syntax_type(), tail);
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Err(_) => match &arg.0 {
|
||||||
|
PositionalType::Mandatory(..) => {
|
||||||
|
return Err(ParseError::argument_error(
|
||||||
|
config.name.clone().spanned(command_span),
|
||||||
|
ArgumentError::MissingMandatoryPositional(arg.0.name().to_string()),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
PositionalType::Optional(..) => {
|
||||||
|
if tail.expand_syntax(MaybeWhitespaceEof).is_ok() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Ok(result) => {
|
||||||
|
rest_signature.shift_positional();
|
||||||
|
positional.push(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(positional)
|
||||||
|
}
|
||||||
|
|
||||||
fn eat_any_whitespace(tail: &mut TokensIterator) {
|
fn eat_any_whitespace(tail: &mut TokensIterator) {
|
||||||
loop {
|
loop {
|
||||||
match tail.expand_infallible(MaybeSpaceShape) {
|
match tail.expand_infallible(MaybeSpaceShape) {
|
||||||
|
@ -13,7 +13,6 @@ pub enum NamedType {
|
|||||||
Mandatory(SyntaxShape),
|
Mandatory(SyntaxShape),
|
||||||
/// An optional flag, with associated argument. eg) `foo --optional abc`
|
/// An optional flag, with associated argument. eg) `foo --optional abc`
|
||||||
Optional(SyntaxShape),
|
Optional(SyntaxShape),
|
||||||
Help,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The type of positional arguments
|
/// The type of positional arguments
|
||||||
@ -165,7 +164,7 @@ impl Signature {
|
|||||||
usage: String::new(),
|
usage: String::new(),
|
||||||
positional: vec![],
|
positional: vec![],
|
||||||
rest_positional: None,
|
rest_positional: None,
|
||||||
named: indexmap::indexmap! {"help".into() => (NamedType::Help, "Display this help message".into())},
|
named: indexmap::indexmap! {"help".into() => (NamedType::Switch, "Display this help message".into())},
|
||||||
is_filter: false,
|
is_filter: false,
|
||||||
yields: None,
|
yields: None,
|
||||||
input: None,
|
input: None,
|
||||||
@ -243,14 +242,6 @@ impl Signature {
|
|||||||
pub fn switch(mut self, name: impl Into<String>, desc: impl Into<String>) -> Signature {
|
pub fn switch(mut self, name: impl Into<String>, desc: impl Into<String>) -> Signature {
|
||||||
self.named
|
self.named
|
||||||
.insert(name.into(), (NamedType::Switch, desc.into()));
|
.insert(name.into(), (NamedType::Switch, desc.into()));
|
||||||
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove the default help switch
|
|
||||||
pub fn remove_help(mut self) -> Signature {
|
|
||||||
self.named.remove("help");
|
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,7 +159,7 @@ pub(crate) fn get_help(
|
|||||||
long_desc.push_str("\nflags:\n");
|
long_desc.push_str("\nflags:\n");
|
||||||
for (flag, ty) in signature.named {
|
for (flag, ty) in signature.named {
|
||||||
match ty.0 {
|
match ty.0 {
|
||||||
NamedType::Switch | NamedType::Help => {
|
NamedType::Switch => {
|
||||||
long_desc.push_str(&format!(
|
long_desc.push_str(&format!(
|
||||||
" --{}{} {}\n",
|
" --{}{} {}\n",
|
||||||
flag,
|
flag,
|
||||||
|
@ -63,7 +63,6 @@ fn signature_dict(signature: Signature, tag: impl Into<Tag>) -> Value {
|
|||||||
NamedType::Mandatory(_) => sig.push_value(for_spec(name, "flag", true, &tag)),
|
NamedType::Mandatory(_) => sig.push_value(for_spec(name, "flag", true, &tag)),
|
||||||
NamedType::Optional(_) => sig.push_value(for_spec(name, "flag", false, &tag)),
|
NamedType::Optional(_) => sig.push_value(for_spec(name, "flag", false, &tag)),
|
||||||
NamedType::Switch => sig.push_value(for_spec(name, "switch", false, &tag)),
|
NamedType::Switch => sig.push_value(for_spec(name, "switch", false, &tag)),
|
||||||
NamedType::Help => sig.push_value(for_spec("help", "switch", false, &tag)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user