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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
FUJIWARA Shunichiro
July 28, 2025
Technology
2.4k
7
Share
alecthomas/kong はいいぞ
GopherのためのCLIツール開発』最新事情 LT 発表資料
https://findy.connpass.com/event/362163/
FUJIWARA Shunichiro
July 28, 2025
More Decks by FUJIWARA Shunichiro
See All by FUJIWARA Shunichiro
作るべきものと向き合う - ecspresso 8年間の開発史から学ぶ技術選定 / 技術選定con findy 2026
fujiwara3
8
3.5k
さくらのクラウドでのシークレット管理を考える/tamachi.sre#2
fujiwara3
2
310
Amazon ECS デプロイツール ecspresso の開発を支える「正しい抽象化」の探求 / YAPC::Fukuoka 2025
fujiwara3
13
10k
パフォーマンスチューニングのために普段からできること/Performance Tuning: Daily Practices
fujiwara3
8
6.6k
ecspressoの設計思想に至る道 / sekkeinight2025
fujiwara3
12
3.6k
さくらのIaaS基盤のモニタリングとOpenTelemetry/OSC Hokkaido 2025
fujiwara3
3
3.3k
監視のこれまでとこれから/sakura monitoring seminar 2025
fujiwara3
11
5.7k
k6による負荷試験 入門から日常的な実践まで/Re:TechTalk #01
fujiwara3
2
550
困難を「一般解」で解く
fujiwara3
10
4.2k
Other Decks in Technology
See All in Technology
AWS認定資格は本当に意味があるのか?
nrinetcom
PRO
2
270
Choose your own adventure in agentic design patterns
glaforge
0
140
Data Hubグループ 紹介資料
sansan33
PRO
0
2.9k
Claude Code を安全に使おう勉強会 / Claude Code Security Basics
masahirokawahara
11
32k
マルチプロダクトの信頼性を効率良く保っていくために
kworkdev
PRO
0
160
AWS Agent Registry の基礎・概要を理解する/aws-agent-registry-intro
ren8k
3
370
Chasing Real-Time Observability for CRuby
whitegreen
0
110
ARIA Notifyについて
ryokatsuse
1
120
2026年、知っておくべき最新 サーバレスTips10選/serverless-10-tips
slsops
13
5.2k
最近の技術系の話題で気になったもの色々(IoT系以外も) / IoTLT 花見予定会(たぶんBBQ) @都立潮風公園バーベキュー広場
you
PRO
1
240
Good Enough Types: Heuristic Type Inference for Ruby
riseshia
1
210
コミュニティ・勉強会を作るのは目的じゃない
ohmori_yusuke
0
190
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
100
What's in a price? How to price your products and services
michaelherold
247
13k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.3k
Testing 201, or: Great Expectations
jmmastey
46
8.1k
The Cult of Friendly URLs
andyhume
79
6.8k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.5k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
770
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
710
Paper Plane (Part 1)
katiecoart
PRO
0
6.6k
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 から移行)