Class: ParticleCMD::Definition
- Inherits:
-
Object
- Object
- ParticleCMD::Definition
- Defined in:
- lib/particlecmd/definition.rb
Instance Attribute Summary collapse
-
#collecting ⇒ Object
Returns the value of attribute collecting.
-
#flags ⇒ Object
Returns the value of attribute flags.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#positionals ⇒ Object
Returns the value of attribute positionals.
Class Method Summary collapse
Instance Method Summary collapse
- #collect_extra ⇒ Object
- #command_description ⇒ Object
- #command_signature(include_name = true) ⇒ Object
- #description(type, name, desc) ⇒ Object
- #flag(name, **_opts) ⇒ Object
- #help_message ⇒ Object
-
#initialize(name) {|_self| ... } ⇒ Definition
constructor
A new instance of Definition.
- #match(info) ⇒ Object
- #option(name, **_opts) ⇒ Object
- #positional(name, **_opts) ⇒ Object
Constructor Details
#initialize(name) {|_self| ... } ⇒ Definition
Returns a new instance of Definition.
4 5 6 7 8 9 10 11 |
# File 'lib/particlecmd/definition.rb', line 4 def initialize(name) @name = name @positionals = [] @flags = [] @options = [] @collecting = false yield self end |
Instance Attribute Details
#collecting ⇒ Object
Returns the value of attribute collecting.
2 3 4 |
# File 'lib/particlecmd/definition.rb', line 2 def collecting @collecting end |
#flags ⇒ Object
Returns the value of attribute flags.
2 3 4 |
# File 'lib/particlecmd/definition.rb', line 2 def flags @flags end |
#name ⇒ Object
Returns the value of attribute name.
2 3 4 |
# File 'lib/particlecmd/definition.rb', line 2 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
2 3 4 |
# File 'lib/particlecmd/definition.rb', line 2 def @options end |
#positionals ⇒ Object
Returns the value of attribute positionals.
2 3 4 |
# File 'lib/particlecmd/definition.rb', line 2 def positionals @positionals end |
Class Method Details
.from_string(name, string) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/particlecmd/definition.rb', line 13 def self.from_string(name, string) d = new name do end string.split(' ').each do |word| puts word == '...' if word == '...' d.collecting = true elsif word[0] == '-' i = word.match(/-+(.+?)(=(.+))?$/) if i[3] d.option i[1], argname: i[3] else d.flag i[1] end else d.positional word end end d end |
Instance Method Details
#collect_extra ⇒ Object
46 47 48 |
# File 'lib/particlecmd/definition.rb', line 46 def collect_extra @collecting = true end |
#command_description ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/particlecmd/definition.rb', line 92 def command_description s = '' nm = [ (@positionals.map { |p| p[:name].length }.max or 0), (@flags.map { |f| f[:name].length + 2 }.max or 0), (@options.map { |o| o[:name].length + o[:argname].length + 3 }.max or 0) ].max @positionals.each do |p| s << (sprintf " %*s %s\n", nm, p[:name], p[:description]) end @flags.each do |f| s << (sprintf " %*s %s\n", nm, "--#{f[:name]}", f[:description]) end @options.each do |o| s << (sprintf " %*s=%s %s", nm-2, "--#{o[:name]}", o[:argname], o[:description]) end s end |
#command_signature(include_name = true) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/particlecmd/definition.rb', line 76 def command_signature(include_name = true) s = '' s << "#{@name} " if include_name @positionals.each do |p| s << "<#{p[:name]}> " end @flags.each do |f| s << "[--#{f[:name]}] " end @options.each do |o| s << "[--#{o[:name]}=#{o[:argname]}] " end s << "..." if @collecting s end |
#description(type, name, desc) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/particlecmd/definition.rb', line 33 def description(type, name, desc) case type when :positional @positionals when :flag @flags when :option @options else raise RuntimeError.new "Invalid argument type: #{type}" end.find { |i| i[:name] == name }[:description] = desc end |
#flag(name, **_opts) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/particlecmd/definition.rb', line 58 def flag(name, **_opts) opts = { name: name, description: '' }.merge _opts @flags << opts end |
#help_message ⇒ Object
113 114 115 |
# File 'lib/particlecmd/definition.rb', line 113 def 'Usage: ' + command_signature + "\nArguments:\n" + command_description end |
#match(info) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/particlecmd/definition.rb', line 117 def match(info) if @collecting return nil if @positionals.length > info.positionals.length else return nil if @positionals.length != info.positionals.length end return nil if @flags.length < info.flags.length return nil if @options.length < info..length return nil unless (info.flags - @flags.map { |f| f[:name] }).empty? return nil unless (info..keys - @options.map { |o| o[:name] }).empty? res = ParticleCMD::Result.new for i in 0..(@positionals.length - 1) do res.positionals[@positionals[i][:name]] = info.positionals[i] end for i in 0..(@flags.length - 1) do n = @flags[i][:name] res.flags[n] = if info.flags.include? n true else false end end for i in 0..(@options.length - 1) do n = @options[i][:name] res.[n] = if info..include? n info.[n] else nil end end if @collecting && @positionals.length < info.positionals.length res.extra = info.positionals[@positionals.length, info.positionals.length] end res end |
#option(name, **_opts) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/particlecmd/definition.rb', line 66 def option(name, **_opts) opts = { name: name, description: '', argname: 'VALUE', default: nil }.merge _opts @options << opts end |
#positional(name, **_opts) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/particlecmd/definition.rb', line 50 def positional(name, **_opts) opts = { name: name, description: '' }.merge _opts @positionals << opts end |