Simple short flag parse

This commit is contained in:
JT 2021-07-17 10:39:30 +12:00
parent 4249c5b3e0
commit aa7f23e1e1
2 changed files with 40 additions and 3 deletions

View file

@ -60,9 +60,9 @@ fn main() -> std::io::Result<()> {
.required("block", SyntaxShape::Block, "body of the definition");
working_set.add_decl(sig.into());
let file = std::fs::read(&path)?;
let (output, err) = working_set.parse_file(&path, file);
//let (output, err) = working_set.parse_source(path.as_bytes());
// let file = std::fs::read(&path)?;
// let (output, err) = working_set.parse_file(&path, file);
let (output, err) = working_set.parse_source(path.as_bytes());
println!("{:#?}", output);
println!("error: {:?}", err);

View file

@ -994,6 +994,43 @@ impl ParserWorkingSet {
short: None,
required: false,
}));
} else if contents.starts_with(b"-") {
// Short flag
let short_flag = &contents[1..];
let short_flag =
String::from_utf8_lossy(short_flag).to_string();
let chars: Vec<char> = short_flag.chars().collect();
if chars.len() > 1 {
error = error.or(Some(ParseError::Mismatch(
"short flag".into(),
*span,
)));
args.push(Arg::Flag(Flag {
arg: None,
desc: String::new(),
long: String::new(),
short: None,
required: false,
}));
} else if chars.is_empty() {
// Positional arg
args.push(Arg::Positional(PositionalArg {
desc: String::new(),
name: String::from_utf8_lossy(contents).to_string(),
shape: SyntaxShape::Any,
}))
} else {
args.push(Arg::Flag(Flag {
arg: None,
desc: String::new(),
long: String::new(),
short: Some(chars[0]),
required: false,
}));
}
} else {
// Positional arg
args.push(Arg::Positional(PositionalArg {