🔥 명령어 도움말 구성하기

186자
2분

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

swift
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
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)
        }
    }
}
 

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

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

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

text
% 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
% 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!
...

YouTube 영상

채널 보기
Trie 자료구조 파이썬 구현: Search와 Starts With 연산 | Trie 자료구조 이야기
벡터의 정의와 덧셈 연산 | 선형대수학
투영과 예측, 그리고 선형 결합 | 선형대수학
우리가 매일 쓰는 맞춤법 검사기와 라우터 속에 숨겨진 알고리즘은? | Trie 자료구조 이야기
마지막편, 트라이 노드를 50% 이상 줄이는 방법? 압축 트라이 성능 분석 | Trie 자료구조 이야기
직교성과 벡터 투영 | 선형대수학
AI는 왜 수백 차원의 벡터를 사용할까? 고차원 공간과 행렬 | 선형대수학
트라이(Trie)를 이용한 자동 완성 알고리즘 | Trie 자료구조 이야기