diff --git a/Sources/Commands/Run.swift b/Sources/Commands/Run.swift index 42f309b..fbf9dc3 100644 --- a/Sources/Commands/Run.swift +++ b/Sources/Commands/Run.swift @@ -6,11 +6,11 @@ extension RunOn { static var configuration = CommandConfiguration(abstract: "Start event observer.") - @Flag( + @Option( name: .shortAndLong, - help: "Print more information for debugging." + help: "Logging level." ) - var verbose = false + var log = LogLevel.error @Option( name: [.customLong("config"), .customShort("c")], help: "Configuration file path." @@ -22,7 +22,7 @@ extension RunOn { } mutating func run() throws { - logger.setLevel(verbose ? .debug : .error) + logger.setLevel(log) let config = try Config(fromContentsOf: configUrl) let handler = ConfigHandler(with: config) let activeSources = sources.filter { source in diff --git a/Sources/Logger/LogLevel.swift b/Sources/Logger/LogLevel.swift index 69121a3..bad55ef 100644 --- a/Sources/Logger/LogLevel.swift +++ b/Sources/Logger/LogLevel.swift @@ -1,3 +1,5 @@ +import ArgumentParser + /// Logger log level enum LogLevel { case debug @@ -7,7 +9,7 @@ enum LogLevel { case warning } -extension LogLevel: Comparable, Equatable { +extension LogLevel: Comparable, Equatable, ExpressibleByArgument { /// Return log level as integer var integerValue: Int { switch self { @@ -28,6 +30,28 @@ extension LogLevel: Comparable, Equatable { } } + init?(argument: String) { + switch argument.lowercased() { + case "debug": + self = .debug + + case "disabled": + self = .disabled + + case "error": + self = .error + + case "info": + self = .info + + case "warning": + self = .warning + + default: + return nil + } + } + static func < (lhs: LogLevel, rhs: LogLevel) -> Bool { lhs.integerValue < rhs.integerValue }