DocsIntegratingIntegrationsSwift argument parser

Fig Autocomplete & swift-argument-parser

This tutorial will walk you through setting up Fig's integration with Apple CLI framework, swift-argument-parser.

Add the --generate-fig-spec option to your command

Add the fig-swift-argument-parser module as a dependency in your Package.swift file:

let package = Package(
  name: "my-cli",
  dependencies: [
    .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
    .package(url: "https://github.com/withfig/fig-swift-argument-parser", .upToNextMinor(from: "0.1.0")),
    // other dependencies
  ],
  targets: [
    .executableTarget(name: "my-cli", dependencies: [
      .product(name: "ArgumentParser", package: "swift-argument-parser"),
      .product(name: "FigSwiftArgumentParser", package: "fig-swift-argument-parser"),
      // other dependencies
    ]),
    // other targets
  ]
)

Add an @OptionGroup property to the root of your command:

import ArgumentParser
import FigSwiftArgumentParser

@main
public struct MyCli: ParsableCommand {
  @OptionGroup var generateFigSpec: GenerateFigSpec<Self>
  // other variables and methods...
}

NOTE: ensure that generateFigSpec is the first (instance) variable declaration.

Now when you compile and run my-cli --generate-fig-spec you will see the output spec.

Next Steps: now that you have successfully configured the integration you can either push the spec to the autocomplete repo or push it to Fig's cloud.