scriptOptions function
Implementation
List<FigOption> scriptOptions(ScriptFields script) {
final options = <FigOption>[
FigOption(
name: ["-h", "--help"],
description: "Show help for the script",
),
];
final parameters = script.fields['parameters'] as List<dynamic>? ?? [];
for (final param in parameters) {
final name = param['name'];
final description = param['description'] ?? param['type'];
final type = param['type'];
List<FigArg> args = [];
switch (type) {
case "Text":
args.add(FigArg(name: name));
break;
case "Selector":
List<FigGenerator> generators = [];
final selectorGenerators =
param['selector']?['generators'] as List<dynamic>?;
if (selectorGenerators != null) {
generators = selectorGenerators
.where((g) => g['type'] == "ShellScript")
.map<FigGenerator>((g) => FigGenerator(
script: ["bash", "-c", g['shellScript']['script']],
splitOn: "\n",
))
.toList();
}
final suggestions =
(param['selector']?['suggestions'] as List<dynamic>?)
?.map((s) => s.toString())
.toList();
args.add(FigArg(
name: name,
suggestions: suggestions,
generators: generators,
));
break;
case "Path":
args.add(FigArg(
name: name,
template: "filepaths",
));
break;
case "Checkbox":
options.add(FigOption(
name: "--no-$name",
description: description,
isRequired: true,
exclusiveOn: ["--$name"],
));
// Dart option doesn't support modifying existing option instance easily (final fields)
// But we need to set exclusiveOn on the main option too.
// We can recreate it.
// Or just return as is, since we are building the list.
// Actually, we can't modify 'option' after creation.
// So we should construct it with exclusiveOn if needed.
// Refactoring to handle Checkbox logic before creating FigOption.
break;
}
// Re-create option with args and exclusiveOn if needed
List<String>? exclusiveOn;
if (type == "Checkbox") {
exclusiveOn = ["--no-$name"];
}
options.add(FigOption(
name: "--$name",
description: description,
isRequired: true,
args: args.isNotEmpty ? args : null,
exclusiveOn: exclusiveOn,
));
}
return options;
}