Generate a command-line interface using swift-argument-parser with subcommands, typed options, shell completions, and help text. Invoke with the project name and list of commands/subcommands.
Generate a structured command-line interface using Swift ArgumentParser with subcommands, typed options, validation, and shell completions.
$ARGUMENTS should include:
Example: fsgraph init(path:String) scan(path:String,recursive:Bool) watch(paths:[String],daemon:Bool) query(sql:String) decide(file:String) review(batch:Int?)
Package.swift to verify swift-argument-parser is a dependency.Sources/{PROJECT_NAME}CLI/main.swift exists beyond the scaffold.Sources/{PROJECT_NAME}/Database/DatabaseManager.swift for database
commands that need a connection.Sources/{PROJECT_NAME}Daemon/Daemon.swift for daemon-related commands.Create or update Sources/{PROJECT_NAME}CLI/main.swift:
@main struct {PROJECT_NAME}CLI: AsyncParsableCommand with:
static let configuration = CommandConfiguration(commandName: "{project-name}", abstract: "{description}", subcommands: [...])--config global option (path to config file).--verbose / --quiet global flags.--version flag printing the current version.For each declared subcommand, create Sources/{PROJECT_NAME}CLI/Commands/{Command}Command.swift:
struct {Command}Command: AsyncParsableCommand with:
static let configuration = CommandConfiguration(commandName: "{command}", abstract: "{description}")@Argument and @Option properties matching declared fields.@Flag for boolean options.mutating func run() async throws with implementation skeleton.Standard subcommands for a typical project:
Create Sources/{PROJECT_NAME}CLI/Helpers/:
CLIOutput.swift — Formatted console output (table, JSON, progress).ConfigLoader.swift — Load config from --config option or default path.ErrorHandler.swift — Structured error display with exit codes.Add instructions for generating shell completions:
// In the root command:
static let configuration = CommandConfiguration(
// ...
subcommands: [/* ... */, GenerateCompletions.self]
)
struct GenerateCompletions: ParsableCommand {
static let configuration = CommandConfiguration(
commandName: "completions",
abstract: "Generate shell completions"
)
@Argument var shell: String = "zsh"
func run() throws {
// swift-argument-parser handles this automatically
}
}
Define standard exit codes in Sources/{PROJECT_NAME}CLI/Helpers/ExitCodes.swift:
enum ExitCode: Int32 {
case success = 0
case generalError = 1
case configError = 2
case databaseError = 3
case watcherError = 4
case inputError = 64 // EX_USAGE
case dataError = 65 // EX_DATAERR
case noInput = 66 // EX_NOINPUT
}
main.swift already exists with content, read it and only add missing subcommands./data-layer first./daemon-service
is needed for background operation./scaffold-package (for Package.swift and module structure)/data-layer (for database commands), /daemon-service (for watch commands)/contributor-guide (for CLI usage documentation)