class Spud::CLI::Parser

Attributes

results[R]

Public Class Methods

new(args) click to toggle source
# File lib/spud/cli/parser.rb, line 25
def initialize(args)
  @args = args.dup
  @results = Results.new
end
parse(args) click to toggle source
# File lib/spud/cli/parser.rb, line 20
def self.parse(args)
  new(args).parse!
end
parse!() click to toggle source
# File lib/spud/cli/parser.rb, line 15
def self.parse!
  parse(ARGV)
end

Public Instance Methods

parse!() click to toggle source
# File lib/spud/cli/parser.rb, line 31
def parse!
  parse_arg! until done?
  results
end

Private Instance Methods

arg() click to toggle source
# File lib/spud/cli/parser.rb, line 89
def arg
  @args.first
end
before_task_name?() click to toggle source
# File lib/spud/cli/parser.rb, line 73
def before_task_name?
  !results.task
end
done?() click to toggle source
# File lib/spud/cli/parser.rb, line 99
def done?
  @args.empty?
end
flag?() click to toggle source
# File lib/spud/cli/parser.rb, line 94
def flag?
  arg.start_with?('-')
end
handle_option!() click to toggle source
# File lib/spud/cli/parser.rb, line 52
def handle_option!
  flag = take!
  case flag
  when '-h', '--help' then options.help = true
  when '-w', '--watch' then options.watches << take!
  when '-i', '--inspect' then options.inspect = true
  else raise Error, "invalid option: '#{flag}'"
  end
end
lstrip_hyphens(flag) click to toggle source
# File lib/spud/cli/parser.rb, line 68
def lstrip_hyphens(flag)
  flag.gsub(/^-+/, '')
end
options() click to toggle source
# File lib/spud/cli/parser.rb, line 63
def options
  results.options
end
parse_arg!() click to toggle source
# File lib/spud/cli/parser.rb, line 39
def parse_arg!
  if before_task_name?
    flag? ? handle_option! : set_task_name!
  else
    if flag?
      results.named[lstrip_hyphens(take!)] = take!
    else
      results.ordered << take!
    end
  end
end
set_task_name!() click to toggle source
# File lib/spud/cli/parser.rb, line 78
def set_task_name!
  results.task = take!
end
take!() click to toggle source
# File lib/spud/cli/parser.rb, line 83
def take!
  @args.shift
end