Sha256: bf10761d263c3d7916aee74fa2465bc6102f445ba912473eb9f380b934ba81b3

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

require 'optparse'

# A Hash specialization that collects the command-line options
class CLIOptions < Hash
  # labelled square notation (LBN).
  # Use online tools (e.g. http://yohasebe.com/rsyntaxtree/) to visualize
  # parse trees from LBN output.
  def initialize(progName, progVersion, args)
    super()
    
    # Default values
    self[:prog_name] = progName
    self[:prog_version] = progVersion
    self[:format] = :ascii_tree
    
    options = build_option_parser
    options.parse!(args)
  end

  private

  def build_option_parser
    OptionParser.new do |opts|
      opts.banner = <<-END_BANNER
#{self[:prog_name]}: a demo utility that parses a JSON file
and renders its parse tree to the standard output 
in the format specified in the command-line.

Usage: JSON_demo.rb [options] FILE

Examples:
JSON_demo --format ascii_tree sample01.jon
END_BANNER

      opts.separator ''

      format_help = <<-END_TEXT
Select the output format (default: ascii_tree). Available formats:
  ascii_tree  Simple text representation of parse trees
  labelled    Labelled square notation (LBN)
              Use online tools (e.g. http://yohasebe.com/rsyntaxtree/) 
              to visualize parse trees from LBN output.
END_TEXT
      formats = %i[ascii_tree labelled]
      opts.on('-f', '--format FORMAT', formats, format_help) do |frm|
        self[:format] = frm
      end

      opts.separator ''
      opts.separator '  **** Utility ****'

      opts.on('-v', '--version', 'Display the program version.') do
        puts self[:prog_version]
        exit
      end

      # No argument, shows at tail.  This will print an options summary.
      opts.on_tail('-h', '--help', 'Display this help message.') do
        puts opts
        exit
      end
    end
  end
end # class

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rley-0.4.07 examples/data_formats/JSON/cli_options.rb