Module: CLI::Kit::CommandHelp::ClassMethods
Constant Summary collapse
- DEFAULT_HELP_SECTIONS =
[ :desc, :long_desc, :usage, :examples, :options, ]
Instance Method Summary collapse
-
#_command_name ⇒ Object
: -> String.
-
#_desc ⇒ Object
: -> String.
-
#build_desc ⇒ Object
: -> String.
-
#build_examples ⇒ Object
: -> String?.
-
#build_help ⇒ Object
: -> String.
-
#build_options ⇒ Object
: -> String?.
-
#build_usage ⇒ Object
: -> String.
-
#command_name(command_name) ⇒ Object
: (String command_name) -> void.
-
#desc(desc) ⇒ Object
: (String desc) -> void.
-
#example(command, explanation) ⇒ Object
: (String command, String? explanation) -> void.
-
#help_sections(sections) ⇒ Object
: (Array sections) -> void.
-
#long_desc(long_desc) ⇒ Object
: (String long_desc) -> void.
-
#opts_class ⇒ Object
: -> untyped.
-
#usage(usage) ⇒ Object
: (String usage) -> void.
Instance Method Details
#_command_name ⇒ Object
: -> String
103 104 105 106 107 108 |
# File 'lib/cli/kit/command_help.rb', line 103 def _command_name return @command_name if @command_name last_camel = send(:name).split('::').last last_camel.gsub(/([a-z])([A-Z])/, '\1-\2').downcase end |
#_desc ⇒ Object
: -> String
111 112 113 |
# File 'lib/cli/kit/command_help.rb', line 111 def _desc @desc end |
#build_desc ⇒ Object
: -> String
116 117 118 119 120 121 122 |
# File 'lib/cli/kit/command_help.rb', line 116 def build_desc out = +"{{command:#{CommandHelp._tool_name} #{_command_name}}}" if @desc out << ": #{@desc}" end "{{bold:#{out}}}" end |
#build_examples ⇒ Object
: -> String?
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/cli/kit/command_help.rb', line 237 def build_examples return unless @examples cmd_prefix = " {{command:#{CommandHelp._tool_name} #{_command_name}}}" "{{bold:Examples:}}\n" + @examples.map do |command, explanation| cmd = "#{cmd_prefix} #{command}" exp = "{{italic:{{gray:# #{explanation}}}}}" width = CLI::UI::ANSI.printing_width(CLI::UI.fmt("#{cmd} #{exp}")) if width > CLI::UI::Terminal.width " #{exp}\n#{cmd}" else "#{cmd} #{exp}" end end.join("\n\n") end |
#build_help ⇒ Object
: -> String
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/cli/kit/command_help.rb', line 82 def build_help h = (@help_sections || DEFAULT_HELP_SECTIONS).map do |section| case section when :desc build_desc when :long_desc @long_desc when :usage @usage_section ||= build_usage when :examples @examples_section ||= build_examples when :options @options_section ||= else raise "Unknown help section: #{section}" end end.compact.map(&:chomp).join("\n\n") + "\n" CLI::UI.fmt(h) end |
#build_options ⇒ Object
: -> String?
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/cli/kit/command_help.rb', line 133 def opts = opts_class return unless opts methods = [] loop do methods.concat(opts.public_instance_methods(false)) break if opts.superclass == CLI::Kit::Opts opts = opts.superclass end @defn = Args::Definition.new o = opts.new o.define!(@defn) return if @defn..empty? && @defn.flags.empty? merged = @defn. #: Array[(Args::Definition::Option | Args::Definition::Flag)] merged += @defn.flags merged.sort_by!(&:name) "{{bold:Options:}}\n" + merged.map do |o| if o.is_a?(Args::Definition::Option) z = ' ' + [o.short&.prepend('-'), o.long&.prepend('--')].compact.join(', ') + ' VALUE' default = if o.dynamic_default? '(generated default)' elsif o.default.nil? '(no default)' else "(default: #{o.default.inspect})" end z << if o.desc " {{italic:{{gray:# #{o.desc} #{default}}}}}" else " {{italic:{{gray:# #{default}}}}}" end else z = ' ' + [o.short&.prepend('-'), o.long&.prepend('--')].compact.join(', ') if o.desc z << " {{italic:{{gray:# #{o.desc}}}}}" end end z end.join("\n") end |
#build_usage ⇒ Object
: -> String
223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/cli/kit/command_help.rb', line 223 def build_usage '{{bold:Usage:}}' + case (@usage || []).size when 0 " {{command:#{CommandHelp._tool_name} #{_command_name}}} [options]\n" when 1 " {{command:#{CommandHelp._tool_name} #{_command_name}}} #{@usage.first}\n" else "\n" + @usage.map do |usage| " {{command:#{CommandHelp._tool_name} #{_command_name}}} #{usage}\n" end.join end end |
#command_name(command_name) ⇒ Object
: (String command_name) -> void
185 186 187 188 189 190 191 |
# File 'lib/cli/kit/command_help.rb', line 185 def command_name(command_name) if @command_name raise(ArgumentError, "Command name already set to #{@command_name}") end @command_name = command_name end |
#desc(desc) ⇒ Object
: (String desc) -> void
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/cli/kit/command_help.rb', line 194 def desc(desc) # A limit of 80 characters has been chosen to fit on standard terminal configurations. `long_desc` is # available when descriptions don't fit nicely in that space. If you're using CLI::Kit for an application # where you control the runtime environments and know that terminals will have more than 80 columns # available, you can use # # CLI::Kit::CommandHelp.max_desc_length = # # to increase this limit. if desc.size > CommandHelp._max_desc_length raise(ArgumentError, "description must be #{CommandHelp._max_desc_length} characters or less") end if @desc raise(ArgumentError, 'description already set') end @desc = desc end |
#example(command, explanation) ⇒ Object
: (String command, String? explanation) -> void
261 262 263 264 |
# File 'lib/cli/kit/command_help.rb', line 261 def example(command, explanation) @examples ||= [] @examples << [command, explanation] end |
#help_sections(sections) ⇒ Object
: (Array sections) -> void
180 181 182 |
# File 'lib/cli/kit/command_help.rb', line 180 def help_sections(sections) @help_sections = sections end |
#long_desc(long_desc) ⇒ Object
: (String long_desc) -> void
214 215 216 217 218 219 220 |
# File 'lib/cli/kit/command_help.rb', line 214 def long_desc(long_desc) if @long_desc raise(ArgumentError, 'long description already set') end @long_desc = long_desc end |
#opts_class ⇒ Object
: -> untyped
125 126 127 128 129 130 |
# File 'lib/cli/kit/command_help.rb', line 125 def opts_class uself = self #: as untyped uself.const_get(:Opts) # rubocop:disable Sorbet/ConstantsFromStrings rescue NameError Class.new(CLI::Kit::Opts) end |
#usage(usage) ⇒ Object
: (String usage) -> void
255 256 257 258 |
# File 'lib/cli/kit/command_help.rb', line 255 def usage(usage) @usage ||= [] @usage << usage end |