🔥 도움말 플래그 이름 수정하기

241자
2분

사용자는 -h--help 플래그를 전달하여 명령어 도움말 화면을 볼 수 있습니다. 만약 -h--help 플래그를 다른 용도로 사용해야 한다면, 루트 명령어를 정의할 때 help 플래그에 다른 이름을 지정할 수 있습니다.

swift
struct Example: ParsableCommand {
    static let configuration = CommandConfiguration(
        helpNames: [.long, .customShort("?")])
 
    @Option(name: .shortAndLong, help: "The number of history entries to show.")
    var historyDepth: Int
 
    mutating func run() throws {
        printHistory(depth: historyDepth)
    }
}
 
swift
struct Example: ParsableCommand {
    static let configuration = CommandConfiguration(
        helpNames: [.long, .customShort("?")])
 
    @Option(name: .shortAndLong, help: "The number of history entries to show.")
    var historyDepth: Int
 
    mutating func run() throws {
        printHistory(depth: historyDepth)
    }
}
 

명령어를 실행할 때 -hhistoryDepth 속성을 나타내는 짧은 이름으로 사용하고, -?는 도움말 화면을 표시하는 옵션으로 사용합니다:

text
% example -h 3
nmap -v -sS -O 10.2.2.2
sshnuke 10.2.2.2 -rootpw="Z1ON0101"
ssh 10.2.2.2 -l root
% example -?
USAGE: example --history-depth <history-depth>

ARGUMENTS:
  <phrase>                The phrase to repeat.

OPTIONS:
  -h, --history-depth     The number of history entries to show.
  -?, --help              Show help information.
text
% example -h 3
nmap -v -sS -O 10.2.2.2
sshnuke 10.2.2.2 -rootpw="Z1ON0101"
ssh 10.2.2.2 -l root
% example -?
USAGE: example --history-depth <history-depth>

ARGUMENTS:
  <phrase>                The phrase to repeat.

OPTIONS:
  -h, --history-depth     The number of history entries to show.
  -?, --help              Show help information.

이 예제에서 부모 명령어는 --help-?를 도움말 옵션으로 정의합니다. 따로 지정하지 않으면, 하위 명령어들은 이 도움말 옵션을 그대로 상속합니다.

swift
struct Parent: ParsableCommand {
    static let configuration = CommandConfiguration(
        subcommands: [Child.self],
        helpNames: [.long, .customShort("?")])
 
    struct Child: ParsableCommand {
        @Option(name: .shortAndLong, help: "The host the server will run on.")
        var host: String
    }
}
 
swift
struct Parent: ParsableCommand {
    static let configuration = CommandConfiguration(
        subcommands: [Child.self],
        helpNames: [.long, .customShort("?")])
 
    struct Child: ParsableCommand {
        @Option(name: .shortAndLong, help: "The host the server will run on.")
        var host: String
    }
}
 

위 코드에서 child 하위 명령어는 부모 도움말 이름을 상속하기 때문에 사용자는 -h 를 사용하여 호스트 정보를, -? 를 사용하여 자세한 도움말을 확인할 수 있습니다.

text
% parent child -h 192.0.0.0
...
% parent child -?
USAGE: parent child --host <host>

OPTIONS:
  -h, --host <host>       The host the server will run on.
  -?, --help              Show help information.
text
% parent child -h 192.0.0.0
...
% parent child -?
USAGE: parent child --host <host>

OPTIONS:
  -h, --host <host>       The host the server will run on.
  -?, --help              Show help information.

YouTube 영상

채널 보기
함수 타입과 Hom-Set 이해하기 | 프로그래머를 위한 카테고리 이론
C++ 속의 펑터 | 프로그래머를 위한 카테고리 이론
Git Worktree로 여러 피처 동시에 개발하기 | AI 코딩 시대의 필수 스킬
class-validator 와 DTO | NestJS 가이드
NestJS 역할 기반 접근 권한 부여 - Guard, Reflector | NestJS 가이드
NestJS 가드, 바이딩과 스코프 | NestJS 가이드
미들웨어 vs 가드, 왜 NestJS에서는 가드가 더 똑똑할까? | NestJS 가이드
Product와 Coproduct가 Bifunctor인 이유 | 프로그래머를 위한 카테고리 이론