lib/cl/help/cmd.rb in cl-1.0.0 vs lib/cl/help/cmd.rb in cl-1.0.1
- old
+ new
@@ -1,12 +1,13 @@
+require 'cl/help/format'
require 'cl/help/table'
require 'cl/help/usage'
class Cl
class Help
class Cmd < Struct.new(:ctx, :cmd)
- include Regex
+ include Format
def format
[usage, summary, description, arguments, options, common, examples].compact.join("\n\n")
end
@@ -84,70 +85,17 @@
def width
[args.width, opts.width, cmmn.width].max
end
- def format_obj(obj)
- opts = []
- opts << "type: #{format_type(obj)}" unless obj.type == :flag
- opts << 'required: true' if obj.required?
- opts += format_opt(obj) if obj.is_a?(Opt)
- opts = opts.join(', ')
- opts = "(#{opts})" if obj.description && !opts.empty?
- opts = [obj.description, opts]
- opts.compact.map(&:strip).join(' ')
- end
-
- def format_opt(opt)
- opts = []
- opts << "alias: #{format_aliases(opt)}" if opt.aliases?
- opts << "requires: #{opt.requires.join(', ')}" if opt.requires?
- opts << "default: #{format_default(opt)}" if opt.default?
- opts << "known values: #{format_enum(opt)}" if opt.enum?
- opts << "format: #{opt.format}" if opt.format?
- opts << "downcase: true" if opt.downcase?
- opts << "min: #{opt.min}" if opt.min?
- opts << "max: #{opt.max}" if opt.max?
- opts << "e.g.: #{opt.example}" if opt.example?
- opts << "note: #{opt.note}" if opt.note?
- opts << "see: #{opt.see}" if opt.see?
- opts << format_deprecated(opt) if opt.deprecated?
- opts.compact
- end
-
- def format_aliases(opt)
- opt.aliases.map do |name|
- strs = [name]
- strs << "(deprecated, please use #{opt.name})" if opt.deprecated[0] == name
- strs.join(' ')
- end.join(', ')
- end
-
- def format_enum(opt)
- opt.enum.map { |value| format_regex(value) }.join(', ')
- end
-
- def format_type(obj)
- return obj.type unless obj.is_a?(Opt) && obj.type == :array
- "array (string, can be given multiple times)"
- end
-
- def format_default(opt)
- opt.default.is_a?(Symbol) ? opt.default.to_s.sub('_', ' ') : opt.default
- end
-
- def format_deprecated(opt)
- return "deprecated (#{opt.deprecated[1]})" if opt.deprecated[0] == opt.name
- end
-
def rjust(objs)
return objs unless objs.any?
width = objs.max_by(&:size).size
objs.map { |objs| [*Array.new(width - objs.size) { '' }, *objs] }
end
def indent(str)
- str.lines.map { |line| " #{line}" }.join
+ str.lines.map { |line| " #{line}".rstrip }.join("\n")
end
end
end
end