Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/liamnichols/swift-configuration-parser
Simple configuration file parsing for Swift
https://github.com/liamnichols/swift-configuration-parser
Last synced: about 1 month ago
JSON representation
Simple configuration file parsing for Swift
- Host: GitHub
- URL: https://github.com/liamnichols/swift-configuration-parser
- Owner: liamnichols
- License: mit
- Created: 2022-08-07T00:36:00.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-08-09T19:14:24.000Z (over 2 years ago)
- Last Synced: 2024-09-15T15:20:41.310Z (about 2 months ago)
- Language: Swift
- Size: 35.2 KB
- Stars: 12
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Swift Configuration Parser
Simple configuration file parsing for Swift.
## Usage
Start by declaring a type containing your configuration options:
```swift
struct Configuration: ParsableConfiguration {
enum Power: Int, Decodable {
case low, medium, high
}@Option(
summary: "The power level used if not otherwise specified.",
discussion: """
When setting a default power level, it is crucial that you \
are sure that the consumption will not exceed the daily \
allocated allowance. Failure to keep within your usage \
limits will result in termination of the contract.It's recommended that you do not change this value unless \
you are fully aware of the risks involved.
"""
)
var defaultPowerLevel: Power = .medium@Option(summary: "Array of preferred spoken languages")
var preferredLanguages: [String] = ["en", "fr", "ar"]@Option(.deprecated("Replaced by ‘preferredLanguages‘"), hidden: true)
var preferredLanguage: String? = nil@Option(summary: "Details of the author used when creating commits")
var author: Authorstruct Author: ParsableConfiguration {
@Option(summary: "The full name of the author")
var name: String = "John Doe"@Option(summary: "The email address of the author")
var email: String = "[email protected]"
}
}
```Then use the static methods available through the `ParsableConfiguration` protocol to load your configuration from the appropriate source:
```swift
// The default configuration instance
let configuration = Configuration.default
``````swift
// Load directly from a file
let fileURL = URL(fileURLWithPath: "./.options.json")
let configuration = try Configuration.parse(contentsOf: fileURL)
``````swift
// Load from data
let data = try downloadConfigurationFromServer()
let configuration = try Configuration.parse(data)
```## Features
- Preserve default values when deserializing
- Detect unexpected properties (typos/misconfiguration) while loading
- Flexible overrides
- Customizable decoding - Uses `JSONDecoder` by default but you can plug in anythingFor an example, see the simple [example package](./Example/) or the [usage in CreateAPI](https://github.com/CreateAPI/CreateAPI/blob/main/Sources/CreateOptions/ConfigOptions.swift).
---
Heavily inspired by [swift-argument-parser](https://github.com/apple/swift-argument-parser).