Sha256: 57db849bfa8ff8923bbd4a33beff5cc38e32991ea6f92b7f5a7594b30a1e8bfe

Contents?: true

Size: 1.44 KB

Versions: 19

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require 'optparse' # Use standard OptionParser class for command-line parsing

module Loxxy
  # A command-line option parser for the Loxxy interpreter.
  # It is a specialisation of the OptionParser class.
  class CLIParser < OptionParser
    # @return [Hash{Symbol=>String, Array}]
    attr_reader(:parsed_options)

    # Constructor.
    def initialize(prog_name, ver)
      super()
      reset(prog_name, ver)

      heading
      separator 'Options:'
      separator ''
      add_tail_options
    end

    def parse!(args)
      super
      parsed_options
    end

    private

    def reset(prog_name, ver)
      @program_name = prog_name
      @version = ver
      @banner = "Usage: #{prog_name} LOX_FILE [options]"
      @parsed_options = {}
    end

    def description
      <<-DESCR
  Description:
    loxxy is a Lox interpreter, it executes the Lox file(s) given in command-line.
    More on Lox Language: https://craftinginterpreters.com/the-lox-language.html

  Example:
    #{program_name} hello.lox
      DESCR
    end

    def heading
      banner
      separator ''
      separator description
      separator ''
    end

    def add_tail_options
      on_tail('--version', 'Display the program version then quit.') do
        puts version
        exit(0)
      end

      on_tail('-?', '-h', '--help', 'Display this help then quit.') do
        puts help
        exit(0)
      end
    end
  end # class
end # module
# End of file

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
loxxy-0.4.08 lib/loxxy/cli_parser.rb
loxxy-0.4.07 lib/loxxy/cli_parser.rb
loxxy-0.4.06 lib/loxxy/cli_parser.rb
loxxy-0.4.05 lib/loxxy/cli_parser.rb
loxxy-0.4.04 lib/loxxy/cli_parser.rb
loxxy-0.4.03 lib/loxxy/cli_parser.rb
loxxy-0.4.02 lib/loxxy/cli_parser.rb
loxxy-0.4.01 lib/loxxy/cli_parser.rb
loxxy-0.4.00 lib/loxxy/cli_parser.rb
loxxy-0.3.03 lib/loxxy/cli_parser.rb
loxxy-0.3.02 lib/loxxy/cli_parser.rb
loxxy-0.3.01 lib/loxxy/cli_parser.rb
loxxy-0.3.00 lib/loxxy/cli_parser.rb
loxxy-0.2.06 lib/loxxy/cli_parser.rb
loxxy-0.2.05 lib/loxxy/cli_parser.rb
loxxy-0.2.04 lib/loxxy/cli_parser.rb
loxxy-0.2.03 lib/loxxy/cli_parser.rb
loxxy-0.2.02 lib/loxxy/cli_parser.rb
loxxy-0.2.01 lib/loxxy/cli_parser.rb