Skip to content

Instantly share code, notes, and snippets.

@morimorihoge
Created June 12, 2020 09:46
Show Gist options
  • Save morimorihoge/7ae85e62ebd848003d74fcbd2a98fa58 to your computer and use it in GitHub Desktop.
Save morimorihoge/7ae85e62ebd848003d74fcbd2a98fa58 to your computer and use it in GitHub Desktop.
OptionParserを使ったスクリプトテンプレ
#!/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