Class: Json2::Option
- Inherits:
-
Object
- Object
- Json2::Option
- Defined in:
- lib/json2/option.rb
Overview
Process command line switches.
The keys you are going to use:
:without_header - Boolean, if true the user want to parse the Json
file as if it has no header data.
:with_path - Boolean, if true the user wants to extract only
a particular path in the Json file.
:path - This is the String path to extract if :with_path
is true.
Examples
opt = Option.new
if opt[:with_path]
puts "Extracting #{opt[:path]}…"
# Do the job.
end
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get an option.
-
#initialize ⇒ Option
constructor
Creates a new Option instance.
- #parse(opts) ⇒ Object
Constructor Details
#initialize ⇒ Option
Creates a new Option instance.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/json2/option.rb', line 24 def initialize = { without_header: false, with_path: false } optparse = OptionParser.new {|opts| parse(opts) } begin optparse.parse! rescue OptionParser::InvalidOption => exception puts exception.to_s exit 1 end print_version if [:version] end |
Instance Method Details
#[](key) ⇒ Object
Get an option.
key - The Symbol name of the option to get.
Returns Any value corresponding of the key, or nil if the key
doesn't exists.
45 46 47 |
# File 'lib/json2/option.rb', line 45 def [](key) [key] end |
#parse(opts) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/json2/option.rb', line 49 def parse(opts) opts.on('-w', '--without-header', 'Output csv without a header') do [:without_header] = true end opts.on('-p', '--path PATH', 'Extract only this path') do |arg| [:with_path] = true [:path] = arg end opts.on('-v', '--version', 'Print version number and exit') do [:version] = true end opts.on('-h', '--help', 'Display this screen') do puts opts exit end end |