🔥 명령어 도움말 구성하기

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 영상

채널 보기
입력을 전처리하는 Functor - Contravariant와 contramap 이해하기 | 프로그래머를 위한 카테고리 이론
존 매카시가 들려주는 인공지능의 탄생 이야기
API 응답 지연과 복잡한 에러, NestJS 인터셉터로 관리하는 방법 | NestJS 가이드
class-validator 와 DTO | NestJS 가이드
인터셉터와 RxJS로 캐시 시스템 구축하기 | NestJS 가이드
함수 객체의 보편적 구성 | 프로그래머를 위한 카테고리 이론
변환 파이프로 컨트롤러 코드 깔끔하게 만들기 | NestJS 가이드
함수 타입과 Hom-Set 이해하기 | 프로그래머를 위한 카테고리 이론