🔥 명령어 도움말 구성하기

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(트라이) 자료구조 원리와 파이썬 클래스 설계 및 구현 | Trie 자료구조 이야기
Trie 자료구조 완전 정복 - 개념부터 시각화까지 | Trie 자료구조 이야기
Trie 자료구조 파이썬 구현: Search와 Starts With 연산 | Trie 자료구조 이야기
AI는 왜 수백 차원의 벡터를 사용할까? 고차원 공간과 행렬 | 선형대수학
우리가 매일 쓰는 맞춤법 검사기와 라우터 속에 숨겨진 알고리즘은? | Trie 자료구조 이야기
마지막편, 10억 개 데이터 검색이 0.3ms면 끝나는 이유와 LSM-Tree의 등장
내적의 기하학적 의미와 코사인 유사도 원리 | 선형대수학
숫자 하나가 AI 모델의 운명을 바꾼다? | 선형대수학