Sha256: e70b633f8034e957f2dc7bd6d305eaa8ac7d13b8a302af2fe40d022ffbb7460b

Contents?: true

Size: 685 Bytes

Versions: 2

Compression:

Stored size: 685 Bytes

Contents

module Sshster
  class ArgvParser
    attr_reader :config, :command, :help

    def initialize(argv)
      @argv = argv
    end

    def parse
      options = parse_argv(@argv)
      @config = options['-c']
      @help = options['-h']
      @command = detect_command(options)
      self
    end

    def valid?
      !@command.nil? && valid_commands.include?(@command)
    end

    private

    def valid_commands
      %i[init compose help]
    end

    def detect_command(options)
      command = options.find { |_k, v| v.nil? }
      command[0].to_sym if command
    end

    def parse_argv(argv)
      Hash[argv.join(' ').scan(/(-?[^=\s]+)(?: ([^-]{1}[\S]+))?/)]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sshster-0.1.1 lib/sshster/argv_parser.rb
sshster-0.1.0 lib/sshster/argv_parser.rb