Created
June 12, 2020 09:46
-
-
Save morimorihoge/7ae85e62ebd848003d74fcbd2a98fa58 to your computer and use it in GitHub Desktop.
OptionParserを使ったスクリプトテンプレ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# requireや初期化を書く | |
require 'optparse' | |
opt = OptionParser.new | |
opt.version = '1.0' | |
# opt.banner = 'Usage: command [options]' | |
option_a = nil | |
# option別の#onを実装 | |
opt.on('-a REQUIRED', 'description') do |v| | |
option_a = v | |
end | |
# #parse!を実行 | |
opt.parse!(ARGV) | |
# 通常の引数があるならここから展開(default nilの場合) | |
arg1 = ARGV[0] || nil | |
arg2 = ARGV[1] || nil | |
# メインの処理をここから書く | |
unless option_a.nil? | |
# -aオプションの時の処理 | |
end | |
# 共通処理 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment