Class: Commander::Runner
- Inherits:
-
Object
- Object
- Commander::Runner
- Defined in:
- lib/MrMurano/commands/completion.rb
Instance Method Summary collapse
-
#cmdMaxDepth ⇒ Object
Get maximum depth of sub-commands.
-
#cmdTree ⇒ Object
Get a tree of all commands and sub commands.
-
#cmdTreeB ⇒ Object
Alternate tree of sub-commands.
-
#flatswitches(option) ⇒ Object
Change the ‘–[no-]foo’ switch into ‘–no-foo’ and ‘–foo’.
-
#optionDesc(option) ⇒ Object
truncate the description of an option.
-
#takesArg(option, yes = '=', no = '') ⇒ Object
If the switches take an argument, retun =.
Instance Method Details
#cmdMaxDepth ⇒ Object
Get maximum depth of sub-commands.
59 60 61 62 63 64 65 66 |
# File 'lib/MrMurano/commands/completion.rb', line 59 def cmdMaxDepth depth=0 @commands.sort.each do |name,cmd| levels = name.split depth = levels.count if levels.count > depth end depth end |
#cmdTree ⇒ Object
Get a tree of all commands and sub commands
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/MrMurano/commands/completion.rb', line 43 def cmdTree tree={} @commands.sort.each do |name,cmd| levels = name.split pos = tree levels.each do |step| pos[step] = {} unless pos.has_key? step pos = pos[step] end pos["\0cmd"] = cmd end tree end |
#cmdTreeB ⇒ Object
Alternate tree of sub-commands.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/MrMurano/commands/completion.rb', line 70 def cmdTreeB tree={} @commands.sort.each do |name,cmd| levels = name.split tree[levels.join(' ')] = {:cmd=>cmd} # load parent. left = levels[0..-2] right = levels[-1] key = left.join(' ') tree[key] = {} unless tree.has_key? key if tree[key].has_key?(:subs) then tree[key][:subs] << right else tree[key][:subs] = [right] end end tree end |
#flatswitches(option) ⇒ Object
Change the ‘–[no-]foo’ switch into ‘–no-foo’ and ‘–foo’
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/MrMurano/commands/completion.rb', line 13 def flatswitches(option) # if there is a --[no-]foo format, break that into two switches. option[:switches].map{ |switch| switch = switch.sub(/\s.*$/,'') # drop argument spec if exists. if switch =~ /\[no-\]/ then [switch.sub(/\[no-\]/, ''), switch.gsub(/[\[\]]/,'')] else switch end }.flatten end |
#optionDesc(option) ⇒ Object
truncate the description of an option
37 38 39 |
# File 'lib/MrMurano/commands/completion.rb', line 37 def optionDesc(option) option[:description].sub(/\n.*$/,'') end |
#takesArg(option, yes = '=', no = '') ⇒ Object
If the switches take an argument, retun =
27 28 29 30 31 32 33 |
# File 'lib/MrMurano/commands/completion.rb', line 27 def takesArg(option, yes='=', no='') if option[:switches].select { |switch| switch =~ /\s\S+$/ }.empty? then no else yes end end |