///|
/// What the command line asks for.
priv enum Command {
Help
Version
Generate(inputs~ : Array[String], output~ : String, check~ : Bool)
}
///|
let usage : String =
#|Write the XAML codecs of MoonBit types from their #xaml attributes.
#|
#|Usage: xaml-codegen [--check] ... -o
#|
#| -o The file for the code of all inputs
#| --check Write nothing, and fail when the output file has other code
#| --help Show this text
#| --version Show the version
///|
/// The command in the arguments after the name of the program, or what is wrong with them.
fn parse_command(arguments : ArrayView[String]) -> Result[Command, String] {
let inputs = []
let mut output = None
let mut check = false
for rest = arguments {
match rest {
[] => break
["--help" | "-h", ..] => return Ok(Help)
["--version", ..] => return Ok(Version)
["--check", .. rest] => {
check = true
continue rest
}
["-o", path, .. rest] => {
guard output is None else { return Err("there are two -o") }
output = Some(path)
continue rest
}
["-o"] => return Err("-o needs a file")
[path, .. rest] => {
guard !path.has_prefix("-") else {
return Err("there is no option \{path}")
}
inputs.push(path)
continue rest
}
}
}
guard !inputs.is_empty() else { return Err("there are no inputs") }
guard output is Some(output) else {
return Err("the output is missing, as in -o models.g.mbt")
}
guard !inputs.contains(output) else {
return Err("\{output} is an input and the output")
}
Ok(Generate(inputs~, output~, check~))
}