Module: CLIApp::Util

Defined in:
lib/cliapp.rb

Defined Under Namespace

Modules: Color

Class Method Summary collapse

Class Method Details

.argstr_of_proc(proc_) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/cliapp.rb', line 357

def argstr_of_proc(proc_)
  #; [!gbk7b] generates argument string of proc object.
  n = proc_.arity
  n = - (n + 1) if n < 0
  sb = []; cnt = 0
  proc_.parameters.each do |(ptype, pname)|
    aname = param2argname(pname)
    case ptype
    when :req, :opt
      #; [!b6gzp] required param should be '<param>'.
      #; [!q1030] optional param should be '[<param>]'.
      n -= 1
      if n >= 0 ; sb <<  " <#{aname}>"
      else      ; sb << " [<#{aname}>" ; cnt += 1
      end
    when :rest
      #; [!osxwq] variable param should be '[<param>...]'.
      sb << " [<#{aname}>...]"
    end
  end
  sb << ("]" * cnt)
  return sb.join()
end

.arity_of_proc(proc_) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/cliapp.rb', line 341

def arity_of_proc(proc_)
  #; [!get6i] returns min and max arity of proc object.
  #; [!ghrxo] returns nil as max arity if proc has variable param.
  n_max = 0
  n_min = proc_.arity
  n_min = - (n_min + 1) if n_min < 0
  has_rest = false
  proc_.parameters.each do |ptype, _|
    case ptype
    when :req, :opt ; n_max += 1
    when :rest      ; has_rest = true
    end
  end
  return n_min, (has_rest ? nil : n_max)
end

.param2argname(name) ⇒ Object



381
382
383
384
385
386
387
388
389
390
# File 'lib/cliapp.rb', line 381

def param2argname(name)
  #; [!52dzl] converts 'yes_or_no' to 'yes|no'.
  #; [!6qkk6] converts 'file__html' to 'file.html'.
  #; [!2kbhe] converts 'aa_bb_cc' to 'aa-bb-cc'.
  name = name.to_s
  name = name.gsub('_or_', '|')  # ex: 'yes_or_no' -> 'yes|no'
  name = name.gsub('__', '.')    # ex: 'file__html' -> 'file.html'
  name = name.gsub('_', '-')     # ex: 'src_dir' -> 'src-dir'
  return name
end