Module: Flagger

Defined in:
lib/cli_tools.rb

Instance Method Summary collapse

Instance Method Details

#active_flagsObject



170
171
172
# File 'lib/cli_tools.rb', line 170

def active_flags
  @active_flags || {}
end

#active_keysObject



166
167
168
# File 'lib/cli_tools.rb', line 166

def active_keys
  @active_keys || {}
end

#active_optionsObject



156
157
158
# File 'lib/cli_tools.rb', line 156

def active_options
  active_flags.merge active_keys
end

#clear_argumentsObject



41
42
43
44
45
46
47
48
49
# File 'lib/cli_tools.rb', line 41

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



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

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

#flag_and_key(option, &blk) ⇒ Object



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

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

#flag_key_for(which) ⇒ Object



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

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



152
153
154
# File 'lib/cli_tools.rb', line 152

def help_docs
  exit
end

#parse_flags(args) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cli_tools.rb', line 91

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 ) : nil})
      end
    else
      flag_set.shift
        @active_flags.merge!({flag_key_for(flag_set) => (args.first ? (args.first.flag? or args.shift) : nil) })
    end
  end
  @active_flags
end

#process(args = nil) ⇒ Object



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

def process(args=nil)
  seperate(args) if args
  process_input 
end

#process_flags(options) ⇒ Object



146
147
148
149
150
# File 'lib/cli_tools.rb', line 146

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

#process_input(&blk) ⇒ Object



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

def process_input &blk
  run_flags
  run_keys
end

#remaining_argumentObject



162
163
164
# File 'lib/cli_tools.rb', line 162

def remaining_argument
  @remaining_argument.join(' ')
end

#remaining_argumentsObject



159
160
161
# File 'lib/cli_tools.rb', line 159

def remaining_arguments
  @remaining_argument||[]
end

#run_flagsObject



69
70
71
# File 'lib/cli_tools.rb', line 69

def run_flags
  process_flags parse_flags(@flags)
end

#run_keysObject



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

def run_keys
  process_flags @active_keys
end

#seperate(args = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cli_tools.rb', line 56

def seperate(args=nil)
  return nil unless 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



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cli_tools.rb', line 134

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



126
127
128
129
130
131
132
# File 'lib/cli_tools.rb', line 126

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

#start_flag_checkingObject



87
88
89
# File 'lib/cli_tools.rb', line 87

def start_flag_checking
  @active_flags ||= {}
end

#trip_flag(which, with_value = nil) ⇒ Object



82
83
84
85
# File 'lib/cli_tools.rb', line 82

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