Add optional params

This commit is contained in:
JT 2021-07-17 10:53:45 +12:00
parent aa7f23e1e1
commit d08f2e73d0

View File

@ -927,7 +927,7 @@ impl ParserWorkingSet {
} }
enum Arg { enum Arg {
Positional(PositionalArg), Positional(PositionalArg, bool), // bool - required
Flag(Flag), Flag(Flag),
} }
@ -958,7 +958,7 @@ impl ParserWorkingSet {
let span = Span { start, end }; let span = Span { start, end };
let source = &self.file_contents[..span.end]; let source = &self.file_contents[..span.end];
let (output, err) = lex(&source, span.start, &[b'\n', b','], &[b':', b'?']); let (output, err) = lex(&source, span.start, &[b'\n', b','], &[b':']);
error = error.or(err); error = error.or(err);
let mut args: Vec<Arg> = vec![]; let mut args: Vec<Arg> = vec![];
@ -1017,11 +1017,14 @@ impl ParserWorkingSet {
})); }));
} else if chars.is_empty() { } else if chars.is_empty() {
// Positional arg // Positional arg
args.push(Arg::Positional(PositionalArg { args.push(Arg::Positional(
desc: String::new(), PositionalArg {
name: String::from_utf8_lossy(contents).to_string(), desc: String::new(),
shape: SyntaxShape::Any, name: String::from_utf8_lossy(contents).to_string(),
})) shape: SyntaxShape::Any,
},
true,
))
} else { } else {
args.push(Arg::Flag(Flag { args.push(Arg::Flag(Flag {
arg: None, arg: None,
@ -1032,12 +1035,29 @@ impl ParserWorkingSet {
})); }));
} }
} else { } else {
// Positional arg if contents.ends_with(b"?") {
args.push(Arg::Positional(PositionalArg { let contents = &contents[..(contents.len() - 1)];
desc: String::new(),
name: String::from_utf8_lossy(contents).to_string(), // Positional arg, optional
shape: SyntaxShape::Any, args.push(Arg::Positional(
})) PositionalArg {
desc: String::new(),
name: String::from_utf8_lossy(contents).to_string(),
shape: SyntaxShape::Any,
},
false,
))
} else {
// Positional arg, required
args.push(Arg::Positional(
PositionalArg {
desc: String::new(),
name: String::from_utf8_lossy(contents).to_string(),
shape: SyntaxShape::Any,
},
true,
))
}
} }
} }
ParseMode::TypeMode => { ParseMode::TypeMode => {
@ -1045,7 +1065,7 @@ impl ParserWorkingSet {
let syntax_shape = self.parse_shape_name(contents); let syntax_shape = self.parse_shape_name(contents);
//TODO check if we're replacing one already //TODO check if we're replacing one already
match last { match last {
Arg::Positional(PositionalArg { shape, .. }) => { Arg::Positional(PositionalArg { shape, .. }, ..) => {
*shape = syntax_shape; *shape = syntax_shape;
} }
Arg::Flag(Flag { arg, .. }) => *arg = Some(syntax_shape), Arg::Flag(Flag { arg, .. }) => *arg = Some(syntax_shape),
@ -1073,7 +1093,7 @@ impl ParserWorkingSet {
} }
flag.desc.push_str(&contents); flag.desc.push_str(&contents);
} }
Arg::Positional(positional) => { Arg::Positional(positional, ..) => {
if !positional.desc.is_empty() { if !positional.desc.is_empty() {
positional.desc.push_str("\n"); positional.desc.push_str("\n");
} }
@ -1090,7 +1110,13 @@ impl ParserWorkingSet {
for arg in args { for arg in args {
match arg { match arg {
Arg::Positional(positional) => sig.required_positional.push(positional), Arg::Positional(positional, required) => {
if required {
sig.required_positional.push(positional)
} else {
sig.optional_positional.push(positional)
}
}
Arg::Flag(flag) => sig.named.push(flag), Arg::Flag(flag) => sig.named.push(flag),
} }
} }