Color named type help especial case. (#1263)

Refactored out help named type as switch.
This commit is contained in:
Andrés N. Robalino 2020-01-22 19:36:48 -05:00 committed by GitHub
parent 07191754bf
commit fe4ad5f77e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 52 deletions

View File

@ -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) => {
match extract_mandatory(config, name, tail, command_span) {
Err(err) => {
@ -115,35 +104,13 @@ pub fn parse_command_tail(
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(..) => {
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);
match continue_parsing_positionals(&config, tail, &mut rest_signature, command_span) {
Ok(positionals) => {
positional = positionals;
}
Err(reason) => {
if found_error.is_none() && !tail.source().contains("help") {
found_error = Some(reason);
}
}
}
@ -213,6 +180,45 @@ pub fn parse_command_tail(
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) {
loop {
match tail.expand_infallible(MaybeSpaceShape) {

View File

@ -13,7 +13,6 @@ pub enum NamedType {
Mandatory(SyntaxShape),
/// An optional flag, with associated argument. eg) `foo --optional abc`
Optional(SyntaxShape),
Help,
}
/// The type of positional arguments
@ -165,7 +164,7 @@ impl Signature {
usage: String::new(),
positional: vec![],
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,
yields: None,
input: None,
@ -243,14 +242,6 @@ impl Signature {
pub fn switch(mut self, name: impl Into<String>, desc: impl Into<String>) -> Signature {
self.named
.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
}

View File

@ -159,7 +159,7 @@ pub(crate) fn get_help(
long_desc.push_str("\nflags:\n");
for (flag, ty) in signature.named {
match ty.0 {
NamedType::Switch | NamedType::Help => {
NamedType::Switch => {
long_desc.push_str(&format!(
" --{}{} {}\n",
flag,

View File

@ -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::Optional(_) => sig.push_value(for_spec(name, "flag", false, &tag)),
NamedType::Switch => sig.push_value(for_spec(name, "switch", false, &tag)),
NamedType::Help => sig.push_value(for_spec("help", "switch", false, &tag)),
}
}