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
alecthomas/kong はいいぞ
Search
FUJIWARA Shunichiro
July 28, 2025
Technology
6
1.9k
alecthomas/kong はいいぞ
GopherのためのCLIツール開発』最新事情 LT 発表資料
https://findy.connpass.com/event/362163/
FUJIWARA Shunichiro
July 28, 2025
Tweet
Share
More Decks by FUJIWARA Shunichiro
See All by FUJIWARA Shunichiro
パフォーマンスチューニングのために普段からできること/Performance Tuning: Daily Practices
fujiwara3
2
200
ecspressoの設計思想に至る道 / sekkeinight2025
fujiwara3
12
3k
さくらのIaaS基盤のモニタリングとOpenTelemetry/OSC Hokkaido 2025
fujiwara3
3
1.3k
監視のこれまでとこれから/sakura monitoring seminar 2025
fujiwara3
11
5.4k
k6による負荷試験 入門から日常的な実践まで/Re:TechTalk #01
fujiwara3
2
180
困難を「一般解」で解く
fujiwara3
10
3.9k
「隙間家具OSS」に至る道/Fujiwara Tech Conference 2025
fujiwara3
7
14k
alecthomas/kong はいいぞ / kamakura.go#7
fujiwara3
1
1.2k
ISUCONに強くなるかもしれない日々の過ごしかた/Findy ISUCON 2024-11-14
fujiwara3
11
1.4k
Other Decks in Technology
See All in Technology
決済システムの信頼性を支える技術と運用の実践
ykagano
0
250
The Twin Mandate of Observability
charity
1
770
Sansan BIが実践する AI on BI とセマンティックレイヤー / data_summit_findy
sansan_randd
0
110
從裝潢設計圖到 Home Assistant:打造智慧家庭的實戰與踩坑筆記
kewang
0
140
Oracle Cloud Infrastructure:2025年10月度サービス・アップデート
oracle4engineer
PRO
0
120
Mackerelにおけるインシデント対応とポストモーテム - 現場での工夫と学び
taxin
0
110
Boxを“使われる場”にする統制と自動化の仕組み
demaecan
0
210
3年ぶりの re:Invent 今年の意気込みと前回の振り返り
kazzpapa3
0
150
Design and implementation of "Markdown to Google Slides" / phpconfuk 2025
k1low
1
320
今から間に合う re:Invent 準備グッズと現地の地図、その他ラスベガスを周る際の Tips/reinvent-preparation-guide
emiki
1
320
新米エンジニアをTech Leadに任命する ー 成長を支える挑戦的な人と組織のマネジメント
naopr
1
370
今のコンピュータ、AI にも Web にも 向いていないので 作り直そう!!
piacerex
0
700
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
658
61k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
How GitHub (no longer) Works
holman
315
140k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
23
1.5k
Making Projects Easy
brettharned
120
6.4k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
How to Ace a Technical Interview
jacobian
280
24k
How to train your dragon (web standard)
notwaldorf
97
6.3k
Why Our Code Smells
bkeepers
PRO
340
57k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
Unsuck your backbone
ammeep
671
58k
Transcript
alecthomas/kong はいいぞ 2025.07.28 『GopherのためのCLIツール開発』最新事情 LT @fujiwara
自己紹介 @fujiwara (X, GitHub, Bluesky) 2025-02〜 さくらインターネット ISUCON 優勝4回 /
運営5回 github.com/kayac/ecspresso github.com/fujiwara/lambroll
CLI flag parser、何を使ってますか? Claude にお勧めを聞いてみたらこんな感じ flag (標準) spf13/pflag spf13/cobra urfave/cli
alecthomas/kong 今日は alecthomas/kong をお勧めする話をします
https://github.com/alecthomas/kong
package main import "github.com/alecthomas/kong" var CLI struct { Rm struct
{ Force bool `help:"Force removal."` Recursive bool `help:"Recursively remove files."` Paths []string `arg:"" name:"path" help:"Paths to remove." type:"path"` } `cmd:"" help:"Remove files."` Ls struct { Paths []string `arg:"" optional:"" name:"path" help:"Paths to list." type:"path"` } `cmd:"" help:"List paths."` } func main() { ctx := kong.Parse(&CLI) switch ctx.Command() { case "rm <path>": case "ls": default: panic(ctx.Command()) } }
サブコマンドと引数を構造体に定義 var CLI struct { Rm struct { Force bool
`help:"Force removal."` Recursive bool `help:"Recursively remove files."` Paths []string `arg:"" name:"path" help:"Paths to remove." type:"path"` } `cmd:"" help:"Remove files."` Ls struct { Paths []string `arg:"" optional:"" name:"path" help:"Paths to list." type:"path"` } `cmd:"" help:"List paths."` } rm コマンドには --force , --recursive 、 paths (複数可)が引数 command rm --force --recursive foo bar ls コマンドには paths (複数可)が引数 command ls foo bar baz
定義した構造体を kong.Parse に渡して解析 func main() { ctx := kong.Parse(&CLI) //
このctxはcontext.Context**ではない**ので注意 switch ctx.Command() { case "rm <path>": case "ls": default: panic(ctx.Command()) } } 基本はこれだけ コマンドラインから渡された引数( os.Args )を解析して構造体に入れてくれる その後のコードは好きに書いていい
kong のいいところ 宣言的。サブコマンドまで含めて一目でオプションが把握できる map ( Foo map[string]int → --foo="x=1;y=2;z=3" )
slice ( Foo []string → --foo=x --foo=y ) struct tag でいろいろ設定できる required:"" (必須) default:"value" (デフォルト値の設定) short:"x" (短縮形 -x を生成) negatable:"" (bool型の否定形を生成 --wait → --no-wait ) enum:"a,b,c" (a,b,cいずれかの値を要求) env:"FOO" (環境変数FOOを読む) などなど 型に合わせて適切な引数のパースをしてくれる int , bool , time.Duration , time.Time (format指定可)など
kong の一番好きなところ コードの書き方を強要されない CLIフレームワークではなく単なるパーサーとして使える
応用例 例えば Lambda 関数を Go で書く場合、設定値は環境変数から読む func main() { lambda.Start(handler)
} func handler(ctx context.Context) error { // ダメな例 s3Bucket := os.Getenv("BUCKET") if s3Bucket == "" { return errors.New("BUCKET is required") } // ... } 環境変数がないときにエラーになるのは「関数の実行時」 デプロイ直後には発覚しない、めったに通らないところにこれが埋まっていると…?
環境変数を読むのは kong に任せる var CLI struct { Bucket string `env:"BUCKET"
required:"" help:"S3 BUCKET"` LogLevel string `env:"LOG_LEVEL" default:"info" help:"Log level"` } func main() { ctx := kong.Parse(&CLI) // ここで CLI に値が入っている // 必須がなければエラー、デフォルト値も埋まっている lambda.Start(handler) } func handler(ctx context.Context) error { // ここでは CLI の値を使うだけ } こうしておけば、関数の初期化時に確実にエラーになる デフォルト値も設定できる
まとめ alecthomas/kong はパーサー特化、便利なのでお勧めです 最近の fujiwara-ware はだいたい kong を使っています (ecspresso, lambroll
は alecthomas/kingpin から移行)