🔥 명령어 도움말 구성하기

186자
2분

명령어와 하위 명령어 정의하기에서 설명한 명령어 이름과 하위 명령어 구성 외에도 요약, 설명 또는 사용자 지정 사용법 문자열을 제공해 명령어 도움말을 구성할 수 있습니다.

struct Repeat: ParsableCommand {
    static var configuration = CommandConfiguration(
        abstract: "Repeats your input phrase.",
        usage: """
            repeat <phrase>
            repeat --count <count> <phrase>
            """,
        discussion: """
            Prints to stdout forever, or until you halt the program.
            """)
 
    @Argument(help: "The phrase to repeat.")
    var phrase: String
 
    @Option(help: "How many times to repeat.")
    var count: Int? = nil
 
    mutating func run() throws {
        for _ in 0..<(count ?? 2) {
            print(phrase)
        }
    }
}
 
swift

위 코드에서는 CommandConfiguration을 사용해 명령어 도움말을 맞춤 설정합니다.

  • abstract: 명령어에 대한 간단한 요약을 제공합니다.
  • usage: 명령어 사용법을 설명하는 문자열을 제공합니다.
  • discussion: 명령어에 대한 자세한 설명을 제공합니다.

이제 맞춤 설정한 구성 요소가 생성한 도움말 화면에 나타납니다.

% repeat --help
OVERVIEW: Repeats your input phrase.

Prints to stdout forever, or until you halt the program.

USAGE: repeat <phrase>
       repeat --count <count> <phrase>

ARGUMENTS:
  <phrase>                The phrase to repeat.

OPTIONS:
  -h, --help              Show help information.

% repeat hello!
hello!
hello!
hello!
hello!
hello!
hello!
...
text