Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
alecthomas/kong はいいぞ / kamakura.go#7
Search
FUJIWARA Shunichiro
December 13, 2024
Technology
1.9k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
alecthomas/kong はいいぞ / kamakura.go#7
https://kamakurago.connpass.com/event/336353/
FUJIWARA Shunichiro
December 13, 2024
More Decks by FUJIWARA Shunichiro
See All by FUJIWARA Shunichiro
多摩川(.dev)ランニング入門 / Tamagawa.dev#3
fujiwara3
3
570
クラウドを使う側から、作る側へ / 大吉祥寺.pm 2026前夜祭
fujiwara3
8
2.2k
作るべきものと向き合う - ecspresso 8年間の開発史から学ぶ技術選定 / 技術選定con findy 2026
fujiwara3
9
6.3k
さくらのクラウドでのシークレット管理を考える/tamachi.sre#2
fujiwara3
2
420
Amazon ECS デプロイツール ecspresso の開発を支える「正しい抽象化」の探求 / YAPC::Fukuoka 2025
fujiwara3
13
14k
パフォーマンスチューニングのために普段からできること/Performance Tuning: Daily Practices
fujiwara3
9
7k
alecthomas/kong はいいぞ
fujiwara3
7
2.6k
ecspressoの設計思想に至る道 / sekkeinight2025
fujiwara3
12
3.8k
さくらのIaaS基盤のモニタリングとOpenTelemetry/OSC Hokkaido 2025
fujiwara3
4
5.7k
Other Decks in Technology
See All in Technology
分析の民主化を支えるメタデータ整備の実践
kakehashi
PRO
4
1.4k
AIは「聞けば答える」から「任せると自分で進める」へ - JACTIMペナン地域部会・ペナン三水会講演
knishioka
0
110
AI × アーキテクト 第1・2章|生成AIの課題とアーキテクトの素養
daidaidai
0
160
AI導入は済んだ。次は統制を作り替える番だ_AI×開発組織 Summit 2026
techtekt
PRO
0
210
遠藤な_めぐろLT #38 「社内のAI活用推進」@麻布台_20261009
endonanana
0
130
USB PDを使ってスマホを労わりたい
raspython3
0
180
しろおびから始める!ログ活用の第一歩
shumei_ito
0
940
V8のArray.prototype.flatを最大約20倍速くするまでと、巨大OSSへの大規模コミットの道のり
riyaamemiya
0
290
hackers_champloo_2026_tidb
makocchi
0
160
SAENM ~Jupyterで残すPoCの試行錯誤、未来の自分と仲間を助ける記録!~
terapyon
0
110
DGX Spark でどこまでハッキングができるのか?
kash1064
3
1.3k
Persisting Massive Data with Rails ~Designing for 60 billion rows, and fighting the bottlenecks~ Railsで挑む大量データ永続化 〜600億件を永続化するための設計とボトルネックとの戦い〜
occhi
0
720
Featured
See All Featured
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
460
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
570
GraphQLとの向き合い方2022年版
quramy
50
15k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
2.9k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
The Cost Of JavaScript in 2023
addyosmani
55
10k
Done Done
chrislema
187
17k
Site-Speed That Sticks
csswizardry
13
1.6k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
730
Transcript
alecthomas/kong はいいぞ 2024.12.13 kamakura.go #7 @fujiwara
自己紹介 @fujiwara (X, GitHub, Bluesky) 面白法人カヤック SREチーム ISUCON 優勝4回 /
運営5回 github.com/kayac/ecspresso github.com/fujiwara/lambroll
CLI flag parser、何を使ってますか? ChatGPT-4o にお勧めを聞いてみたらこんな感じ 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型の否定形を生成 --limit → --no-limit ) enum:"a,b,c" (a,b,cいずれかの値を要求) env:"FOO" (環境変数FOOを読む) などなど コードの書き方を強要されない 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 の値を使うだけ } こうしておけば、関数の初期化時に確実にエラーになる デフォルト値も設定できる
更に応用 fujiwara/lamblocal と組み合わせる 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) lamblocal.Run(handler) } func handler(ctx context.Context, in Payload) (Response, error) { // ... } lamblocal: CLIでもLambdaでも動くコマンドを作れる(入出力はSTDIN/STDOUT) $ echo '{"key":"foobar"}' | go run main.go --s3-bucket foo {"result":"ok"}
まとめ alctehomas/kong はパーサー特化、便利なのでお勧めです 最近の fujiwara-ware はだいたい kong を使っています (ecspresso, lambroll
は alecthomas/kingpin から移行)