Class: CouchShell::Commandline
- Inherits:
-
Object
- Object
- CouchShell::Commandline
- Defined in:
- lib/couch-shell/commandline.rb
Defined Under Namespace
Classes: ArgNotAllowedError, Error, Opt, OptArgMissingError, UnknownOptError
Instance Method Summary collapse
- #arg(&block) ⇒ Object
-
#initialize {|_self| ... } ⇒ Commandline
constructor
A new instance of Commandline.
- #opt(optspec1, optspec2 = nil, doc = nil, &block) ⇒ Object
- #optlisting(indent = " ") ⇒ Object
-
#process(args) ⇒ Object
May raise Commandline::Error or subclass thereof.
Constructor Details
#initialize {|_self| ... } ⇒ Commandline
Returns a new instance of Commandline.
88 89 90 91 92 93 94 |
# File 'lib/couch-shell/commandline.rb', line 88 def initialize @arg_action = nil @opts = [] @leading_help = nil @trailing_help = nil yield self end |
Instance Method Details
#arg(&block) ⇒ Object
96 97 98 |
# File 'lib/couch-shell/commandline.rb', line 96 def arg(&block) @arg_action = block end |
#opt(optspec1, optspec2 = nil, doc = nil, &block) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/couch-shell/commandline.rb', line 100 def opt(optspec1, optspec2 = nil, doc = nil, &block) raise "optspec and action block required" unless optspec1 && block option = Opt.new option.doc = doc option.parse_spec! optspec1 if optspec2 && doc option.parse_spec! optspec2 if optspec2 elsif optspec2 option.doc = doc end option.action = block @opts << option end |
#optlisting(indent = " ") ⇒ Object
114 115 116 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 |
# File 'lib/couch-shell/commandline.rb', line 114 def optlisting(indent = " ") max_value_len = @opts.map(&:value).compact.map(&:length).max || 0 String.new.tap do |t| @opts.each { |opt| t << indent if opt.short t << "-#{opt.short} " if opt.value t << opt.value t << (" " * (max_value_len - opt.value.length)) end else t << " " << (" " * max_value_len) end t << " " if opt.long t << "--#{opt.long}" t << "=#{opt.value}" if opt.value end t << "\n" if opt.doc opt.doc.each_line { |l| t << (indent * 3) << l } end t << "\n" } end end |
#process(args) ⇒ Object
May raise Commandline::Error or subclass thereof.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/couch-shell/commandline.rb', line 145 def process(args) optstop = false opt_to_eat_arg = nil opt_to_eat_arg_arg = nil args.each { |arg| if optstop if opt_to_eat_arg raise OptArgMissingError.new(opt_to_eat_arg_arg) end @arg_action.call(arg) next end if opt_to_eat_arg opt_to_eat_arg.action.call(arg) opt_to_eat_arg = nil opt_to_eat_arg_arg = nil next end case arg when "--" optstop = true when /\A--([^=]*)(=.*)?\z/ long = $1 if long.nil? || long.empty? raise Error, "expected option name in `#{arg}'" end opt = @opts.find { |opt| opt.long == long } raise UnknownOptError.new(arg) unless opt if opt.value if $2 opt.action.call($2[1..-1]) else raise OptArgMissingError.new(arg) end else if $2 raise ArgNotAllowedError.new(arg) else opt.action.call end end when /\A-([^-])\z/ short = $1 opt = @opts.find { |opt| opt.short == short } raise UnknownOptError.new(arg) unless opt if opt.value opt_to_eat_arg = opt opt_to_eat_arg_arg = arg else opt.action.call end when /\A-/ raise UnknownOptError.new(arg) else if @arg_action @arg_action.call(arg) else ArgNotAllowedError.new(arg) end end } if opt_to_eat_arg raise OptArgMissingError.new(opt_to_eat_arg_arg) end end |