Karma CLI Parsing SDK
ktools-swift/kcli/README.mdkcli is the Swift implementation of the ktools CLI parsing layer.
It is designed around the same two CLI shapes as the C++ implementation:
- top-level options such as
--verboseand--output - inline roots such as
--trace-*,--config-*, and--build-*
The library exposes two main parse entry points:
parseOrExit(_:)for normal executable startupparseOrThrow(_:)when callers want to interceptCliError
Documentation
Quick Start
import Kcli
func handleVerbose(_ context: HandlerContext) throws {
}
func handleProfile(_ context: HandlerContext, _ value: String) throws {
}
let parser = Parser()
var build = try InlineParser("--build")
try build.setHandler("-profile",
handler: handleProfile,
description: "Set build profile.")
try parser.addInlineParser(build)
try parser.addAlias("-v", target: "--verbose")
try parser.setHandler("--verbose",
handler: handleVerbose,
description: "Enable verbose logging.")
parser.parseOrExit()
Behavior Highlights
- The full command line is validated before any registered handler runs.
parseOrExit()reports invalid CLI input tostderrand exits with code2.parseOrThrow()preserves the input arguments and throwsCliError.- Bare inline roots such as
--buildprint inline help unless a root value is provided. setHandler(..., handler: ValueHandler, ...)registers a required-value option.setOptionalValueHandler(...)registers an optional-value option.- Required values may consume a first token that begins with
-. - Literal
--is rejected as an unknown option; it is not treated as an option terminator.
For the full parsing rules, see docs/behavior.md.
Build SDK
Workspace-style build:
kbuild --build-latest
Direct SwiftPM flow:
cd src
swift test
swift run kcli-demo-core --alpha-message hello
Demos
Demo directories mirror the C++ shape even though the executable products live in the SwiftPM package:
- Bootstrap compile/import check: demo/bootstrap/README.md
- SDK demos: demo/sdk/alpha/README.md, demo/sdk/beta/README.md, demo/sdk/gamma/README.md
- Executable demos: demo/exe/core/README.md, demo/exe/omega/README.md
Useful demo commands:
cd src
swift run kcli-demo-bootstrap
swift run kcli-demo-core --alpha
swift run kcli-demo-core --alpha-message hello
swift run kcli-demo-core --output stdout
swift run kcli-demo-omega --beta-workers 8
swift run kcli-demo-omega --newgamma-tag prod
swift run kcli-demo-omega --build
Repository Layout
- Public API:
src/Sources/Kcli/Kcli.swift - Demo assembly:
src/Sources/KcliDemoSupport/ - Parser and demo coverage:
src/Tests/ - Additional docs:
docs/ - Demo directory map:
demo/
Coding Agents
If you are using a coding agent, paste the following prompt:
Read AGENTS.md and README.md, then inspect src/Package.swift, src/Sources/, and src/Tests/ before editing code.