Module: Flagger
- Defined in:
- lib/cli_tools.rb
Instance Method Summary collapse
- #active_options ⇒ Object
- #clear_arguments ⇒ Object
- #flag(option, &blk) ⇒ Object (also: #key)
- #flag_and_key(option, &blk) ⇒ Object
- #flag_key_for(which) ⇒ Object
- #help_docs ⇒ Object
- #parse_flags(args) ⇒ Object
- #process(args = nil) ⇒ Object
- #process_command_line_input(&blk) ⇒ Object
- #remaining_argument ⇒ Object
- #remaining_arguments ⇒ Object
- #run_flags(options) ⇒ Object
- #seperate_arguments_from(args) ⇒ Object
- #set_block(option, blk = nil) ⇒ Object
- #set_flag(option) ⇒ Object
- #start_flag_checking ⇒ Object
- #trip_flag(which, with_value = nil) ⇒ Object
Instance Method Details
#active_options ⇒ Object
125 126 127 128 129 |
# File 'lib/cli_tools.rb', line 125 def flags = @active_flags || {} keys = @active_keys || {} flags.merge keys end |
#clear_arguments ⇒ Object
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_docs ⇒ Object
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_argument ⇒ Object
133 134 135 |
# File 'lib/cli_tools.rb', line 133 def remaining_argument @remaining_argument.join(' ') end |
#remaining_arguments ⇒ Object
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() .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_checking ⇒ Object
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 |