Files
Lyrebird/Sources/LyrebirdCLI/CLICommand.swift
T
2026-06-01 18:27:40 -07:00

110 lines
4.7 KiB
Swift

// CLICommand.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// Top-level commands parsed from `CommandLine.arguments`.
public enum CLICommand: Equatable {
/// Build the current package, then launch `swift repl` with Lyrebird available.
case repl
/// Enter the LineNoise-powered `lyrebird>` interactive shell (no subcommand given).
case interactive
/// Print usage help.
case help
/// Print the lyrebird version string.
case version
/// Scaffold a new Lyrebird project in a directory named `name` using `template`.
/// When `lyrebirdPath` is set, the generated `Package.swift` and (where
/// applicable) `.xcodeproj` reference Lyrebird as a local SPM package at
/// that absolute path instead of pulling from the published Git URL.
case new(name: String, template: ProjectTemplate, lyrebirdPath: String?)
/// Run the current project with `swift run`.
case run(release: Bool)
/// Export a named Synthesizer template to a Swift source file.
/// When `overwrite` is `true`, an existing file at the destination is replaced.
case export(name: String, to: String?, overwrite: Bool)
/// Install Lyrebird Xcode file templates to the user template directory.
/// When `force` is `true`, replaces any existing installation.
case installTemplates(force: Bool)
/// Remove Lyrebird Xcode file templates from the user template directory.
case uninstallTemplates
/// Parse the command from a raw arguments array (typically `CommandLine.arguments`).
///
/// - Parameter arguments: The full argument vector. `arguments[0]` is the binary name;
/// `arguments[1]` (if present) is the subcommand.
/// - Returns: The matching command, or `.help` for unrecognised or malformed subcommands.
public static func parse(_ arguments: [String]) -> CLICommand {
guard arguments.count > 1 else { return .interactive }
switch arguments[1].lowercased() {
case "repl":
return .repl
case "version", "--version":
return .version
case "help", "--help", "-h":
return .help
case "new":
guard arguments.count > 2, !arguments[2].isEmpty else { return .help }
// If the positional argument looks like a flag, the user omitted the name.
guard !arguments[2].hasPrefix("-") else { return .help }
let template: ProjectTemplate
if let tIdx: Array<String>.Index = arguments.firstIndex(of: "--template"),
tIdx + 1 < arguments.count,
let t: ProjectTemplate = ProjectTemplate(string: arguments[tIdx + 1]) {
template = t
} else {
template = .basic
}
// Optional `--lyrebird-path <abs-path>` for local-checkout use.
// Value must not be a flag token.
let lyrebirdPath: String?
if let pIdx: Array<String>.Index = arguments.firstIndex(of: "--lyrebird-path"),
pIdx + 1 < arguments.count,
!arguments[pIdx + 1].hasPrefix("-") {
lyrebirdPath = arguments[pIdx + 1]
} else {
lyrebirdPath = nil
}
return .new(name: arguments[2],
template: template,
lyrebirdPath: lyrebirdPath)
case "run":
let release: Bool = arguments.contains("--release")
return .run(release: release)
case "install-templates":
let force: Bool = arguments.contains("--force")
return .installTemplates(force: force)
case "uninstall-templates":
return .uninstallTemplates
case "export":
guard arguments.count > 2, !arguments[2].isEmpty else { return .help }
let synthName: String = arguments[2]
// If the positional argument looks like a flag, the user omitted the name.
guard !synthName.hasPrefix("-") else { return .help }
// Look for `--to <path>` anywhere after the subcommand.
// Guard that the value following `--to` is not itself a flag token.
let toPath: String?
if let toIdx: Array<String>.Index = arguments.firstIndex(of: "--to"),
toIdx + 1 < arguments.count,
!arguments[toIdx + 1].hasPrefix("-") {
toPath = arguments[toIdx + 1]
} else {
toPath = nil
}
let overwrite: Bool = arguments.contains("--overwrite")
return .export(name: synthName, to: toPath, overwrite: overwrite)
default:
return .help
}
}
}