Module: Benry::CmdApp::Util

Defined in:
lib/benry/cmdapp.rb

Class Method Summary collapse

Class Method Details

.color_mode?Boolean

Returns:

  • (Boolean)


155
156
157
158
159
# File 'lib/benry/cmdapp.rb', line 155

def color_mode?()
  #; [!xyta1] returns value of $COLOR_MODE if it is not nil.
  #; [!8xufh] returns value of $stdout.tty? if $COLOR_MODE is nil.
  return $COLOR_MODE != nil ? $COLOR_MODE : $stdout.tty?
end

.delete_escape_chars(str) ⇒ Object



150
151
152
153
# File 'lib/benry/cmdapp.rb', line 150

def delete_escape_chars(str)
  #; [!snl3e] removes escape chars from string.
  return str.gsub(/\e\[.*?m/, '')
end

.method2action(meth) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/benry/cmdapp.rb', line 58

def method2action(meth)
  #; [!bt77a] converts method name (Symbol) to action name (String).
  #; [!o5822] converts `:foo_` into `'foo'`.
  #; [!msgjc] converts `:aa__bb____cc` into `'aa:bb:cc'`.
  #; [!qmkfv] converts `:aa_bb_cc` into `'aa-bb-cc'`.
  #; [!tvczb] converts `:_aa_bb:_cc_dd:_ee` into `'_aa-bb:_cc-dd:_ee'`.
  s = meth.to_s                # ex: :foo            => "foo"
  s = s.sub(/_+\z/, '')        # ex: "foo_"          => "foo"
  s = s.gsub(/(__)+/, ':')     # ex: "aa__bb__cc"    => "aa:bb:cc"
  s = s.gsub(/(?<=\w)_/, '-')  # ex: '_aa_bb:_cc_dd' => '_aa-bb:_cc-dd'
  return s
end

.method2help(obj, meth) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/benry/cmdapp.rb', line 71

def method2help(obj, meth)
  #; [!q3y3a] returns command argument string which represents method parameters.
  #; [!r6u58] converts `.foo(x)` into `' <x>'`.
  #; [!8be14] converts `.foo(x=0)` into `' [<x>]'`.
  #; [!skofc] converts `.foo(*x)` into `' [<x>...]'`.
  #; [!61xy6] converts `.foo(x, y=0, *z)` into `' <x> [<y> [<z>...]]'`.
  #; [!0342t] ignores keyword parameters.
  sb = []; n = 0
  obj.method(meth).parameters.each do |kind, param|
    case kind
    when :req  ; sb << " <#{param2arg(param)}>"
    when :opt  ; sb << " [<#{param2arg(param)}>"     ; n += 1
    when :rest ; sb << " [<#{param2arg(param)}>...]"
    when :key
    when :keyreq
    when :keyrest
    else
      raise "** assertion failed: kind=#{kind.inspect}"
    end
  end
  sb << ("]" * n) if n > 0
  #; [!mbxy5] converts `.foo(x, *x_)` into `' <x>...'`.
  #; [!mh9ni] converts `.foo(x, *x2)` into `' <x>...'`.
  return sb.join().sub(/<([^>]+)> \[<\1[-_2]?>\.\.\.\]/, "<\\1>...")
end

.method_override?(klass, meth) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


161
162
163
164
165
166
167
168
169
170
171
# File 'lib/benry/cmdapp.rb', line 161

def method_override?(klass, meth)  # :nodoc:
  #; [!ldd1x] returns true if method defined in parent or ancestor classes.
  klass.ancestors[1..-1].each do |cls|
    if cls.method_defined?(meth) || cls.private_method_defined?(meth)
      return true
    end
    break if cls.is_a?(Class)
  end
  #; [!bc65v] returns false if meethod not defined in parent nor ancestor classes.
  return false
end

.name_should_be_a_string(name, kind, errcls) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/benry/cmdapp.rb', line 173

def name_should_be_a_string(name, kind, errcls)
  #; [!9j4d0] do nothing if name is a string.
  #; [!a2n8y] raises error if name is not a string.
  name.is_a?(String)  or
    raise errcls.new("`#{name.inspect}`: #{kind} name should be a string, but got #{name.class.name} object.")
  nil
end

.param2arg(param) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/benry/cmdapp.rb', line 97

def param2arg(param)
  #; [!ahvsn] converts parameter name (Symbol) into argument name (String).
  #; [!27dpw] converts `:aa_or_bb_or_cc` into `'aa|bb|cc'`.
  #; [!to41h] converts `:aa__bb__cc` into `'aa.bb.cc'`.
  #; [!cldax] converts `:aa_` into `'aa'`.
  #; [!2ma08] converts `:aa_bb_cc` into `'aa-bb-cc'`.
  #; [!5d0qf] lefts `:_aa` as `'_aa'`.
  s = param.to_s
  s = s.gsub('_or_', '|')    # ex: 'file_or_dir' => 'file|dir'
  s = s.gsub('__'  , '.')    # ex: 'file__html'  => 'file.html'
  s = s.gsub(/_+\z/, '')     # ex: 'end_'        => 'end'
  s = s.gsub('_'   , '-')    # ex: 'foo_bar_baz' => 'foo-bar-baz'
  s = s.gsub(/\A-/ , '_')    # ex: '-aa'         => '_aa'
  return s
end

.validate_args_and_kwargs(obj, meth, args, kwargs) ⇒ Object



113
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
143
144
145
146
147
148
# File 'lib/benry/cmdapp.rb', line 113

def validate_args_and_kwargs(obj, meth, args, kwargs)
  n_req = 0; n_opt = 0; rest_p = false; keyrest_p = false
  kws = kwargs.dup
  obj.method(meth).parameters.each do |kind, param|
    case kind
    when :req     ; n_req += 1           # ex: f(x)
    when :opt     ; n_opt += 1           # ex: f(x=0)
    when :rest    ; rest_p = true        # ex: f(*x)
    when :key     ; kws.delete(param)    # ex: f(x: 0)
    when :keyreq  ; kws.delete(param)    # ex: f(x:)
    when :keyrest ; keyrest_p = true     # ex: f(**x)
    else
      raise "** assertion failed: kind=#{kind.inspect}"
    end
  end
  #; [!jalnr] returns error message if argument required but no args specified.
  #; [!gv6ow] returns error message if too less arguments.
  if args.length < n_req
    return (args.length == 0) \
           ? "Argument required (but nothing specified)." \
           : "Too less arguments (at least #{n_req} args)."
  end
  #; [!q5rp3] returns error message if argument specified but no args expected.
  #; [!dewkt] returns error message if too much arguments specified.
  if args.length > n_req + n_opt && ! rest_p
    return (n_req + n_opt == 0) \
           ? "#{args[0].inspect}: Unexpected argument (expected no args)." \
           : "Too much arguments (at most #{n_req + n_opt} args)."
  end
  #; [!u7wgm] returns error message if unknown keyword argument specified.
  if ! kws.empty? && ! keyrest_p
    return "#{kws.keys.first}: Unknown keyword argument."
  end
  #; [!2ep76] returns nil if no error found.
  return nil
end