Sha256: 66c0d5a4d1866686d5a441a36f57ba488ce0316ac5bc53ce0175c8cff7537f70

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

module Hypercuke
  class CLI

    # I extract relevant information from a 'hcu' command line
    class Parser
      attr_reader :options
      def initialize(hcu_command)
        @tokens = tokenize(hcu_command)
        parse_options
      end

      def layer_name
        options[:layer_name]
      end

      private
      attr_reader :tokens

      def parse_options
        @options = Hash.new

        ignore_hcu
        set_layer_name
        set_mode_if_present
        set_profile_if_present
        set_other_args

        options
      end

      def tokenize(input)
        args =
          case input
          when Array  ; input
          when String ; input.split(/\s+/) # That's 4 years of CS education right there, baby
          else fail "Don't know how to parse #{input.inspect}"
          end
        args.compact
      end

      # We might get the 'hcu' command name itself; just drop it on the floor
      def ignore_hcu
        tokens.shift if tokens.first =~ /\bhcu$/i
      end

      # This is the only required argument.
      # TODO: Validate this against the list of known layers?
      #       ^ Would require loading local app's hypercuke config.
      #       ^ Would require allowing local app to *have* hypercuke config.
      def set_layer_name
        fail "Layer name is required" if tokens.empty?
        options[:layer_name] = tokens.shift
      end

      def set_mode_if_present
        unless tokens.first =~ /^-/
          options[:mode] = tokens.shift
        end
      end

      def set_profile_if_present
        if profile_index = ( tokens.index('--profile') || tokens.index('-p') )
          tokens.delete_at(profile_index) # don't care
          options[:profile] = tokens.delete_at(profile_index)
        end
      end

      def set_other_args
        options[:other_args] = Array.new.tap do |rest|
          rest << tokens.shift until tokens.empty?
        end
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hypercuke-0.5.2 lib/hypercuke/cli/parser.rb
hypercuke-0.5.1 lib/hypercuke/cli/parser.rb
hypercuke-0.5.0 lib/hypercuke/cli/parser.rb
hypercuke-0.4.1 lib/hypercuke/cli/parser.rb