|
| 1 | +type t = { |
| 2 | + projectName: option<string>, |
| 3 | + templateName: option<string>, |
| 4 | +} |
| 5 | + |
| 6 | +let supportedOptionsHint = `Supported options: --template <${Templates.supportedTemplateNames->Array.join( |
| 7 | + "|", |
| 8 | + )}> or -t <${Templates.supportedTemplateNames->Array.join("|")}>.` |
| 9 | + |
| 10 | +let getTemplateName = templateName => |
| 11 | + switch Templates.getTemplateName(templateName) { |
| 12 | + | Some(templateName) => Ok(templateName) |
| 13 | + | None => |
| 14 | + Error( |
| 15 | + `Unknown template "${templateName}". Available templates: ${Templates.supportedTemplateNames->Array.join( |
| 16 | + ", ", |
| 17 | + )}.`, |
| 18 | + ) |
| 19 | + } |
| 20 | + |
| 21 | +let parseError = message => Error(`${message} ${supportedOptionsHint}`) |
| 22 | + |
| 23 | +let rec parseRemainingArguments = (remainingArguments, commandLineArguments) => |
| 24 | + switch remainingArguments { |
| 25 | + | list{} => Ok(commandLineArguments) |
| 26 | + | list{"-t", templateName, ...remainingArguments} |
| 27 | + | list{"--template", templateName, ...remainingArguments} => |
| 28 | + switch getTemplateName(templateName) { |
| 29 | + | Ok(templateName) => |
| 30 | + parseRemainingArguments( |
| 31 | + remainingArguments, |
| 32 | + { |
| 33 | + ...commandLineArguments, |
| 34 | + templateName: Some(templateName), |
| 35 | + }, |
| 36 | + ) |
| 37 | + | Error(message) => Error(message) |
| 38 | + } |
| 39 | + | list{"-t"} | list{"--template"} => parseError("Missing value for --template.") |
| 40 | + | list{argument, ...remainingArguments} if argument->String.startsWith("--template=") => |
| 41 | + switch argument->String.split("=") { |
| 42 | + | [_, templateName] => |
| 43 | + switch getTemplateName(templateName) { |
| 44 | + | Ok(templateName) => |
| 45 | + parseRemainingArguments( |
| 46 | + remainingArguments, |
| 47 | + { |
| 48 | + ...commandLineArguments, |
| 49 | + templateName: Some(templateName), |
| 50 | + }, |
| 51 | + ) |
| 52 | + | Error(message) => Error(message) |
| 53 | + } |
| 54 | + | _ => parseError("Missing value for --template.") |
| 55 | + } |
| 56 | + | list{argument, ..._remainingArguments} if argument->String.startsWith("-") => |
| 57 | + parseError(`Unknown option "${argument}".`) |
| 58 | + | list{argument, ...remainingArguments} => |
| 59 | + switch commandLineArguments.projectName { |
| 60 | + | None => |
| 61 | + parseRemainingArguments( |
| 62 | + remainingArguments, |
| 63 | + {...commandLineArguments, projectName: Some(argument)}, |
| 64 | + ) |
| 65 | + | Some(_) => parseError(`Unexpected argument "${argument}".`) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | +let parse = remainingArguments => |
| 70 | + parseRemainingArguments(remainingArguments, {projectName: None, templateName: None}) |
| 71 | + |
| 72 | +let fromProcessArgv = argv => |
| 73 | + switch List.fromArray(argv) { |
| 74 | + | list{_, _, ...remainingArguments} => parse(remainingArguments) |
| 75 | + | _ => Ok({projectName: None, templateName: None}) |
| 76 | + } |
0 commit comments