Created
December 23, 2019 12:26
-
-
Save dcorking/caa775802cdf41c6720f15dcfdc9d3eb to your computer and use it in GitHub Desktop.
lightweight-ish option parsing with Ruby standard library
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
# run with | |
# ruby tools/demo-option-parser.rb --skip-download | |
# or | |
# bundle exec rails runner tools/demo-option-parser.rb --skip-download | |
require 'optparse' # not needed for rails runner | |
skip = nil | |
p ARGV # ["--skip-download"] | |
OptionParser.new do |opts| | |
opts.on("--skip-download") do |s| | |
puts "hello #{s}" # "hello true" | |
skip = s | |
end | |
end.parse! | |
p skip # true | |
# ARGV has been emptied out | |
p ARGV # [] | |
## alternative approach is to populate an options hash that the code refers to later | |
config = { | |
file: 'project_images_from_faraday.json', | |
} | |
OptionParser.new do |parser| | |
parser.on('--skip-download', 'Use a previously saved raw download instead of a fresh download.') | |
parser.on('--file PATH', String, 'Temporary file to store raw download') | |
end.parse!(into: config) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment