🔥 최상위 명령어 정의하기

200자
2분

명령어와 하위 명령어로 구성된 프로그램을 만들기 위해서는 명령어 타입을 여러 개 정의한 후, 각 명령어 설정에서 하위 명령어를 지정해야 합니다. 예를 들어, 다음은 커맨드 라인에서 주어진 일련의 값에 대해 연산을 수행하는 math 유틸리티 인터페이스입니다.

shell
% math add 10 15 7
32
% math multiply 10 15 7
1050
% math stats average 3 4 13 15 15
10.0
% math stats average --kind median 3 4 13 15 15
13.0
% math stats
OVERVIEW: Calculate descriptive statistics.
 
 
USAGE: math stats <subcommand>
 
 
OPTIONS:
  -h, --help              Show help information.
 
 
SUBCOMMANDS:
  average                 Print the average of the values.
  stdev                   Print the standard deviation of the values.
  quantiles               Print the quantiles of the values (TBD).
 
 
  See 'math help stats <subcommand>' for detailed help.
 
shell
% math add 10 15 7
32
% math multiply 10 15 7
1050
% math stats average 3 4 13 15 15
10.0
% math stats average --kind median 3 4 13 15 15
13.0
% math stats
OVERVIEW: Calculate descriptive statistics.
 
 
USAGE: math stats <subcommand>
 
 
OPTIONS:
  -h, --help              Show help information.
 
 
SUBCOMMANDS:
  average                 Print the average of the values.
  stdev                   Print the standard deviation of the values.
  quantiles               Print the quantiles of the values (TBD).
 
 
  See 'math help stats <subcommand>' for detailed help.
 

먼저 최상위 Math 명령어를 정의해 볼게요. 명령어에는 하위 명령어와 기본 하위 명령어를 지정하는 정적 configuration 속성을 제공할 수 있어요.

swift
struct Math: ParsableCommand {
    static var configuration = CommandConfiguration(
        abstract: "A utility for performing maths.",
        subcommands: [Add.self, Multiply.self, Statistics.self],
        defaultSubcommand: Add.self)
}
 
swift
struct Math: ParsableCommand {
    static var configuration = CommandConfiguration(
        abstract: "A utility for performing maths.",
        subcommands: [Add.self, Multiply.self, Statistics.self],
        defaultSubcommand: Add.self)
}
 

MathAdd, Multiply, Statistics 타입으로 세 개 하위 명령어를 나열했습니다. 그리고 Add를 기본 하위 명령어로 지정했어요. 이렇게 하면 사용자가 하위 명령어 이름을 생략했을 때 Add를 선택하죠.

shell
% math 10 15 7
32
shell
% math 10 15 7
32

YouTube 영상

채널 보기
AI는 왜 수백 차원의 벡터를 사용할까? 고차원 공간과 행렬 | 선형대수학
트라이(Trie) 자료구조: 파이썬으로 삽입(Insert) 연산 구현하기 | Trie 자료구조 이야기
우리가 매일 쓰는 맞춤법 검사기와 라우터 속에 숨겨진 알고리즘은? | Trie 자료구조 이야기
숫자 하나가 AI 모델의 운명을 바꾼다? | 선형대수학
트라이(Trie)에서 단어를 삭제하는 방법 | Trie 자료구조 이야기
내적의 기하학적 의미와 코사인 유사도 원리 | 선형대수학
7편, 파이썬으로 구현하는 B-Tree
마지막편, 10억 개 데이터 검색이 0.3ms면 끝나는 이유와 LSM-Tree의 등장