프로젝트
GitHub에서 보기
Carthage/Commandant
소개
타입 안전성을 갖춘 명령줄 인수 처리
개발 도구측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- 0
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalCommandant
Commandant is a Swift framework for parsing command-line arguments, inspired by Argo (which is, in turn, inspired by the Haskell library Aeson).
Example
With Commandant, a command and its associated options could be defined as follows:
struct LogCommand: CommandProtocol {
typealias Options = LogOptions
let verb = "log"
let function = "Reads the log"
func run(_ options: Options) -> Result<(), YourErrorType> {
// Use the parsed options to do something interesting here.
return ()
}
}
struct LogOptions: OptionsProtocol {
let lines: Int
let verbose: Bool
let logName: String
static func create(_ lines: Int) -> (Bool) -> (String) -> LogOptions {
return { verbose in { logName in LogOptions(lines: lines, verbose: verbose, logName: logName) } }
}
static func evaluate(_ m: CommandMode) -> Result<LogOptions, CommandantError<YourErrorType>> {
return create
<*> m <| Option(key: "lines", defaultValue: 0, usage: "the number of lines to read from the logs")
<*> m <| Option(key: "verbose", defaultValue: false, usage: "show verbose output")
<*> m <| Argument(usage: "the log to read")
}
}
Then, each available command should be added to a registry:
let commands = CommandRegistry<YourErrorType>()
commands.register(LogCommand())
commands.register(VersionCommand())
After which, arguments can be parsed by simply invoking the registry:
var arguments = CommandLine.arguments
// Remove the executable name.
assert(!arguments.isEmpty)
arguments.remove(at: 0)
if let verb = arguments.first {
// Remove the command name.
arguments.remove(at: 0)
if let result = commands.run(command: verb, arguments: arguments) {
// Handle success or failure.
} else {
// Unrecognized command.
}
} else {
// No command given.
}
For real-world examples, see the implementation of the Carthage command-line tool.
License
Commandant is released under the MIT license.
관련 프로젝트 살펴보기
불러오는 중…