Module: Flagger

Defined in:
lib/cli_tools.rb

Instance Method Summary collapse

Instance Method Details

#active_optionsObject



108
109
110
111
112
# File 'lib/cli_tools.rb', line 108

def active_options
  flags = @active_flags || {}
  keys = @active_keys || {}
  flags.merge keys
end

#flag(option, &blk) ⇒ Object Also known as: key



65
66
67
68
# File 'lib/cli_tools.rb', line 65

def flag option, &blk
  set_flag  option
  set_block option, blk
end

#flag_and_key(option, &blk) ⇒ Object



71
72
73
74
# File 'lib/cli_tools.rb', line 71

def flag_and_key option, &blk
  flag option, &blk
  option.each { |k, v| flag k, &blk }
end

#flag_key_for(which) ⇒ Object



58
59
60
61
62
63
# File 'lib/cli_tools.rb', line 58

def flag_key_for(which)
  return which.to_sym if @potential_flags.member? which.to_sym
  try = @potential_flags.invert
  return try[which] if try.member? which
  return nil
end

#help_docsObject



104
105
106
# File 'lib/cli_tools.rb', line 104

def help_docs
  exit
end

#parse_flags(args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cli_tools.rb', line 41

def parse_flags(args)
  start_flag_checking
  while not args.empty?
    flag_set  = args.shift
    flag_set.shift
    unless flag_set.flag?
      while not flag_set.empty?
        @active_flags.merge!({flag_key_for(flag_set.shift) => (flag_set.empty? && args.first) ? (args.first.flag? or args.shift ) : true})
      end
    else
      flag_set.shift
      @active_flags.merge!({flag_key_for(flag_set) => (args.first ? (args.first.flag? or args.shift) : true) })
    end
  end
  @active_flags
end

#process_command_line_inputObject



26
27
28
29
30
# File 'lib/cli_tools.rb', line 26

def process_command_line_input
  flags = parse_flags(@flags)
  run_flags @active_keys
  run_flags flags
end

#remaining_argumentObject



113
114
115
# File 'lib/cli_tools.rb', line 113

def remaining_argument
  @remaining_argument||[]
end

#run_flags(options) ⇒ Object



98
99
100
101
102
# File 'lib/cli_tools.rb', line 98

def run_flags(options)
  options.each { |key, value|
    @associated_blocks[key.to_sym][value] rescue help_docs
  }
end

#seperate_arguments_from(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cli_tools.rb', line 12

def seperate_arguments_from(args)
  @active_keys = {}
  @remaining_argument = []
  args = args.duplicate
  while !args.empty? and !args.first.flag?
    if @potential_keywords && @potential_keywords.include?(args.first.to_sym) then
      @active_keys.merge! args.shift.to_sym => nil
    else
      @remaining_argument << args.shift
    end
  end
  @flags = args
end

#set_block(option, blk = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cli_tools.rb', line 86

def set_block(option, blk=nil)
  @associated_blocks||= {}
  blk ||= lambda{}
  tmp = {}
  if option.kind_of? Symbol
    tmp[option] = blk
  else
    option.each {|key, value| tmp[key] = blk}
  end
  @associated_blocks.merge! tmp
end

#set_flag(option) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/cli_tools.rb', line 76

def set_flag(option)
  if option.respond_to? :merge!
    @potential_flags ||= {}
    @potential_flags.merge! option 
  else
    @potential_keywords ||= []
    @potential_keywords << option
  end
end

#start_flag_checkingObject



37
38
39
# File 'lib/cli_tools.rb', line 37

def start_flag_checking
  @active_flags ||= {}
end

#trip_flag(which, with_value = nil) ⇒ Object



32
33
34
35
# File 'lib/cli_tools.rb', line 32

def trip_flag which, with_value=nil
  start_flag_checking
  @active_flags.merge!({which => (with_value || true)})
end