Skip to content

Instantly share code, notes, and snippets.

@pandazx
Last active February 19, 2018 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pandazx/0ecad2cfdfe77c75ada7fcd021f8b555 to your computer and use it in GitHub Desktop.
Save pandazx/0ecad2cfdfe77c75ada7fcd021f8b555 to your computer and use it in GitHub Desktop.
Rubyスクリプトのテンプレートファイル
#
# Rubyスクリプトのテンプレートファイル
#
require 'optparse'
require 'fileutils'
# コマンドライン引数の読み込み
def parse_args(argv)
conf = {}
opt = OptionParser.new
opt.on('-i input_path', '入力ファイル') do |v|
conf[:input_path] = v
end
opt.on('-o output_dir', '出力ディレクトリ') do |v|
conf[:output_dir] = v
FileUtils.mkdir_p(conf[:output_dir]) unless FileTest.exist?(conf[:output_dir])
end
opt.parse(argv)
raise 'must use -i option' if conf[:input_path].nil?
raise 'ust use -o option' if conf[:output_dir].nil?
return conf
end
def main(conf)
puts conf
end
if $PROGRAM_NAME == __FILE__
# このスクリプトが直接、実行された場合のみ、main()を実行.
# これは他スクリプトからメソッドだけrequireしたい場合の備え
conf = parse_args(ARGV)
main(conf)
puts 'done'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment