🔥 명령어 도움말 구성하기

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

채널 보기
NestJS 빌트인 파이프 ParseIntPipe, ParseUUIDPipe 사용하기 | NestJS 가이드
Pro펑터, 입력과 출력을 동시에 다루는 펑터 | 프로그래머를 위한 카테고리 이론
바이펑터란? | 프로그래머를 위한 카테고리 이론
Git Worktree로 여러 피처 동시에 개발하기 | AI 코딩 시대의 필수 스킬
class-validator 와 DTO | NestJS 가이드
NestJS 가드, 바이딩과 스코프 | NestJS 가이드
NestJS 역할 기반 접근 권한 부여 - Guard, Reflector | NestJS 가이드
NestJS 파이프가 뭔가요? 컨트롤러를 보호하는 방법 | NestJS 가이드