Module: Flagger

Defined in:
lib/cli_tools.rb

Instance Method Summary collapse

Instance Method Details

#active_optionsObject



125
126
127
128
129
# File 'lib/cli_tools.rb', line 125

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

#clear_argumentsObject



19
20
21
22
23
24
25
26
27
# File 'lib/cli_tools.rb', line 19

def clear_arguments
  @flags ={}
  @active_keys = {}
  @active_flags = {}
  @potential_flags = {}
  @associated_blocks  = {}
  @remaining_argument = []
  @potential_keywords = []
end

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



84
85
86
87
# File 'lib/cli_tools.rb', line 84

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

#flag_and_key(option, &blk) ⇒ Object



90
91
92
93
# File 'lib/cli_tools.rb', line 90

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

#flag_key_for(which) ⇒ Object



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

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



121
122
123
# File 'lib/cli_tools.rb', line 121

def help_docs
  exit
end

#parse_flags(args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cli_tools.rb', line 60

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(args = nil) ⇒ Object



29
30
31
32
# File 'lib/cli_tools.rb', line 29

def process(args=nil)
  seperate_arguments_from(args) if args
  process_command_line_input 
end

#process_command_line_input(&blk) ⇒ Object



45
46
47
48
49
# File 'lib/cli_tools.rb', line 45

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

#remaining_argumentObject



133
134
135
# File 'lib/cli_tools.rb', line 133

def remaining_argument
  @remaining_argument.join(' ')
end

#remaining_argumentsObject



130
131
132
# File 'lib/cli_tools.rb', line 130

def remaining_arguments
  @remaining_argument||[]
end

#run_flags(options) ⇒ Object



115
116
117
118
119
# File 'lib/cli_tools.rb', line 115

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

#seperate_arguments_from(args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cli_tools.rb', line 33

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

#set_block(option, blk = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cli_tools.rb', line 103

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



95
96
97
98
99
100
101
# File 'lib/cli_tools.rb', line 95

def set_flag(option)
  if option.respond_to? :merge!
    @potential_flags.merge! option 
  else
    @potential_keywords << option
  end
end

#start_flag_checkingObject



56
57
58
# File 'lib/cli_tools.rb', line 56

def start_flag_checking
  @active_flags ||= {}
end

#trip_flag(which, with_value = nil) ⇒ Object



51
52
53
54
# File 'lib/cli_tools.rb', line 51

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