Add option to select importer.
This commit is contained in:
parent
02d2350b29
commit
dc00d87a32
2 changed files with 41 additions and 1 deletions
|
@ -2,4 +2,12 @@ protocol Importer {
|
||||||
init(source: String) throws
|
init(source: String) throws
|
||||||
|
|
||||||
func read() throws -> [String: ColorDef]
|
func read() throws -> [String: ColorDef]
|
||||||
|
|
||||||
|
static var option: String { get }
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Importer {
|
||||||
|
static var option: String {
|
||||||
|
String(String(describing: self).droppingSuffix("Importer"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,35 @@ private struct GeneratorOption: EnumerableFlag, CustomStringConvertible {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private struct ImporterOption: CaseIterable, ExpressibleByArgument, CustomStringConvertible {
|
||||||
|
static let allCases: [ImporterOption] = [
|
||||||
|
.list,
|
||||||
|
]
|
||||||
|
|
||||||
|
static let list = ImporterOption(type: ListImporter.self)
|
||||||
|
|
||||||
|
let type: Importer.Type
|
||||||
|
|
||||||
|
init(type: Importer.Type) {
|
||||||
|
self.type = type
|
||||||
|
}
|
||||||
|
|
||||||
|
init?(argument: String) {
|
||||||
|
guard let found = Self.allCases.first(where: { $0.description.caseInsensitiveCompare(argument) == .orderedSame }) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
self = found
|
||||||
|
}
|
||||||
|
|
||||||
|
var description: String {
|
||||||
|
type.option
|
||||||
|
}
|
||||||
|
|
||||||
|
static func == (lhs: ImporterOption, rhs: ImporterOption) -> Bool {
|
||||||
|
lhs.type == rhs.type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
enum Errors: Error {
|
enum Errors: Error {
|
||||||
case syntaxError
|
case syntaxError
|
||||||
case duplicateColor(String)
|
case duplicateColor(String)
|
||||||
|
@ -54,6 +83,9 @@ public final class MakeColors: ParsableCommand, Context {
|
||||||
@Flag(help: "The formatter to use.")
|
@Flag(help: "The formatter to use.")
|
||||||
private var formatter = GeneratorOption.allCases[0]
|
private var formatter = GeneratorOption.allCases[0]
|
||||||
|
|
||||||
|
@Option(help: "The importer to use.")
|
||||||
|
private var importer = ImporterOption.list
|
||||||
|
|
||||||
@Option(help: "Prefix for color names.")
|
@Option(help: "Prefix for color names.")
|
||||||
var prefix: String?
|
var prefix: String?
|
||||||
|
|
||||||
|
@ -66,7 +98,7 @@ public final class MakeColors: ParsableCommand, Context {
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
public func run() throws {
|
public func run() throws {
|
||||||
let importer = ListImporter(source: input)
|
let importer = try importer.type.init(source: input)
|
||||||
let data = try importer.read()
|
let data = try importer.read()
|
||||||
|
|
||||||
if dump {
|
if dump {
|
||||||
|
|
Loading…
Add table
Reference in a new issue