Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
kotlinx.cliで始めるCLIアプリ開発
Search
Yuta Tomiyama
August 08, 2020
Programming
0
1k
kotlinx.cliで始めるCLIアプリ開発
CAMPHOR- LT 2020 Summer にて発表
Yuta Tomiyama
August 08, 2020
Tweet
Share
More Decks by Yuta Tomiyama
See All by Yuta Tomiyama
Android Autoが思ったよりしんどい話
yt8492
0
93
apollo-kotlinにcontributeした話
yt8492
0
38
DMM TVのSDカードダウンロード機能を実装した話
yt8492
1
580
今だからこそ知りたいKotlin Multiplatform
yt8492
0
180
State management and API calls in Jetpack Compose: Learning Apollo + Jetpack Compose through React Hooks
yt8492
0
1.1k
サーバーフレームワークの仕組みが気になったので車輪の再発明をしてみた
yt8492
0
150
Compose for Webを始めよう
yt8492
0
310
Compose Multiplatform 1.0.0
yt8492
0
130
Kotlin/NativeからCの標準ライブラリを呼び出そう
yt8492
0
240
Other Decks in Programming
See All in Programming
Prepare for Jakarta EE 11 - Performance and Developer Productivity
ivargrimstad
0
730
Blue/Greenデプロイの導入による 運用フローの改善
kudoas
1
370
見た目から始める生産性向上
ikumatadokoro
7
830
Git Lint
bkuhlmann
4
750
dbtのドメイン分割による データ基盤の改善とDigdagとの連携
sakama
0
140
大規模Reactアプリのリアーキテクチャ~8万行のTanStack Query移行の軌跡~
kj455
4
950
Compose-View Interop in Practice (mDevCamp 2024)
stewemetal
0
120
Build Apps for iOS, Android & Desktop in 100% Kotlin With Compose Multiplatform (mDevCamp 2024)
zsmb
0
290
Git Rebase
bkuhlmann
11
1.6k
スクラムガイドのスプリントレトロスペクティブを改めて読みかえしてみた / Re-reading the Sprint Retrospective Section in the Scrum Guide
mackey0225
3
410
AWS Application Composerで始める、 サーバーレスなデータ基盤構築 / 20240406-jawsug-hokuriku-shinkansen
kasacchiful
1
260
Ruby Function Composition
bkuhlmann
1
330
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
227
16k
The Art of Programming - Codeland 2020
erikaheidi
42
12k
Atom: Resistance is Futile
akmur
259
25k
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
GitHub's CSS Performance
jonrohan
1025
450k
A better future with KSS
kneath
231
16k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
104
6.6k
Why You Should Never Use an ORM
jnunemaker
PRO
51
8.6k
A designer walks into a library…
pauljervisheath
200
23k
[RailsConf 2023] Rails as a piece of cake
palkan
23
3.9k
The Pragmatic Product Professional
lauravandoore
25
5.8k
Typedesign – Prime Four
hannesfritz
36
2.1k
Transcript
kotlinx.cliで始める CLIアプリ開発 CAMPHOR- LT 2020 Summer
自己紹介 HN: マヤミト 会津大学26期 (学部3年) Zli 現代表 Twitter: @yt8492 GitHub:
https://github.com/yt8492 好きな技術: Kotlin https://yt8492.com Facebookのすがた Twitterのすがた
もくじ 1. そもそもKotlinでCLIアプリの開発ができるのか? 2. kotlinx.cli の紹介 a. プロジェクトへの導入 b. ArgParser
c. Argument d. Option e. Subcommand 3. デモ
1. そもそもKotlinでCLIアプリ開発ができるのか?
1. そもそもKotlinでCLIアプリ開発ができるのか? A. できます。(できなかったらそもそも今日登壇してない)
前提: KotlinでCLIアプリを開発するには - 実行可能なjarファイルにする - 実行可能なバイナリにする - nodeで実行可能なjsにする
2. kotlinx.cliとは - Kotlinのコマンドラインパーサーライブラリ - Kotlin公式が出している - コマンドやパラメータを宣言的に書ける - マルチプラットフォーム対応
- Kotlin/JVM, Kotlin/JS, Kotlin/Native (公式リポジトリのReadmeより適当に和訳) https://github.com/Kotlin/kotlinx-cli
使用例1
使用例2
kotlinx.cliの導入 方法その1 build.gradleに依存を追加
kotlinx.cliの導入 方法その2(Kotlin/Nativeの場合のみ) build.gradleのターゲットの設定でenableEndorsedLibsをtrueにする
ArgParser コマンドライン引数をパースしてくれるやつ コンストラクタにプログラム名を渡す parseメソッドにコマンドライン引数のString型の配列を渡してパース
Argument そのCLIアプリの主要な引数 例: catで渡すファイル名 type: 引数のタイプ(String, Int, Double, Boolean, Choice)
description: 引数の説明 変数名がそのままargumentの名前になる
Option コマンドのオプション 例: git commit --amend type, description: Argumentと同じ shortName:
-が1つの省略形 Argumentと同じく、変数名がそのままオプション名(fullName)になる デフォルト値や必須のオプションなどを設定可能
Subcommand CLIアプリのサブコマンド 例: git add, git commit Subcommandクラスを継承して実装する Subcommandクラスのコンストラクタに名前と説明を渡す ArgumentとOptionの使い方はほぼ同じ
executeメソッドに実行時の処理を実装する
デモ
使ってみた感想 - めちゃくちゃ楽 - 宣言的に書けるので読みやすい - helpを自動で設定してくれるのが良い - MPP対応なのでいろいろな使い方ができそう -
サーバーの起動時のオプションをkotlinx.cliを使って渡すなど - CLIアプリの場合バイナリにできるKotlin/Nativeと相性が良い
リンク集 kotlinx.cli https://github.com/Kotlin/kotlinx-cli 今回のサンプルアプリ https://github.com/yt8492/CliSample catをKotlin/Nativeで実装しようとしてるやつ(WIP) https://github.com/yt8492/CatKt