lib/cmdparse.rb in cmdparse-3.0.0 vs lib/cmdparse.rb in cmdparse-3.0.1
- old
+ new
@@ -16,11 +16,11 @@
#
# See CmdParse::CommandParser and CmdParse::Command for the two important classes.
module CmdParse
# The version of this cmdparse implemention
- VERSION = '3.0.0'
+ VERSION = '3.0.1'
# Base class for all cmdparse errors.
class ParseError < StandardError
@@ -484,11 +484,11 @@
#
# A typical return value would look like the following:
#
# {command | other_command | another_command }
def usage_commands
- (commands.size > 0 ? "{#{commands.keys.join(" | ")}}" : '')
+ (commands.size > 0 ? "{#{commands.keys.sort.join(" | ")}}" : '')
end
# Returns the formatted short description.
#
# For the output format see #cond_format_help_section
@@ -517,11 +517,11 @@
indent: command_parser.help_desc_indent)
str << "\n" << (cmd.takes_commands? ? describe_commands.call(cmd, level + 1) : "")
end.join('')
end
cond_format_help_section("Available commands", describe_commands.call(self),
- condition: takes_commands?)
+ condition: takes_commands?, preformatted: true)
end
# Returns the formatted arguments of this command.
#
# For the output format see #cond_format_help_section
@@ -540,11 +540,11 @@
summary_width = command_parser.main_options.summary_width
options.summarize([], summary_width, summary_width - 1, '') do |line|
summary << format(line, width: command_parser.help_line_width - command_parser.help_indent,
indent: summary_width + 1, indent_first_line: false) << "\n"
end
- cond_format_help_section(title, summary, condition: !summary.empty?)
+ cond_format_help_section(title, summary, condition: !summary.empty?, preformatted: true)
end
# This hook method is called when the command (or one of its super-commands) is added to another
# Command instance that has an associated command parser (#see command_parser).
#
@@ -569,15 +569,30 @@
#
# A typical help section would look like the following:
#
# Summary:
# help - Provide help for individual commands
- def cond_format_help_section(title, *lines, condition: true, indent: true)
+ #
+ # Options:
+ #
+ # condition:: The formatted help section is only returned if the condition is +true+.
+ #
+ # indent:: Whether the lines should be indented with CommandParser#help_indent spaces.
+ #
+ # preformatted:: Assume that the given lines are already correctly formatted and don't try to
+ # reformat them.
+ def cond_format_help_section(title, *lines, condition: true, indent: true, preformatted: false)
if condition
- "#{title}:\n" << format(lines.flatten.join("\n"),
- indent: (indent ? command_parser.help_indent : 0),
- indent_first_line: true) << "\n\n"
+ out = "#{title}:\n"
+ lines = lines.flatten.join("\n").split(/\n/)
+ if preformatted
+ lines.map! {|l| ' '*command_parser.help_indent << l} if indent
+ out << lines.join("\n")
+ else
+ out << format(lines.join("\n"), indent: (indent ? command_parser.help_indent : 0), indent_first_line: true)
+ end
+ out << "\n\n"
else
''
end
end
@@ -594,12 +609,12 @@
# indent_first_line:: If this option is +true+, then the first line is also indented.
def format(content, width: command_parser.help_line_width,
indent: command_parser.help_indent, indent_first_line: false)
content = (content || '').dup
line_length = width - indent
- first_line_pattern = other_lines_pattern = /\A.{1,#{line_length}}\z|\A.{1,#{line_length}}[ \n]/
- (first_line_pattern = /\A.{1,#{width}}\z|\A.{1,#{width}}[ \n]/) unless indent_first_line
+ first_line_pattern = other_lines_pattern = /\A.{1,#{line_length}}\z|\A.{1,#{line_length}}[ \n]/m
+ (first_line_pattern = /\A.{1,#{width}}\z|\A.{1,#{width}}[ \n]/m) unless indent_first_line
pattern = first_line_pattern
content.split(/\n\n/).map do |paragraph|
lines = []
while paragraph.length > 0
@@ -671,27 +686,35 @@
end
# The default version command.
#
- # It adds the options "-v" and "--version" to the CommandParser#global_options.
+ # It adds the options "-v" and "--version" to the CommandParser#main_options but this can be
+ # changed in ::new.
#
# When the command is specified on the command line (or one of the above mentioned options), it
# shows the version of the program configured by the settings
#
# * command_parser.main_options.program_name
# * command_parser.main_options.version
class VersionCommand < Command
- def initialize #:nodoc:
+ # Create a new version command.
+ #
+ # Options:
+ #
+ # add_switches:: Specifies whether the '-v' and '--version' switches should be added to the
+ # CommandParser#main_options
+ def initialize(add_switches: true)
super('version', takes_commands: false)
short_desc("Show the version of the program")
+ @add_switches = add_switches
end
def on_after_add #:nodoc:
command_parser.main_options.on_tail("--version", "-v", "Show the version of the program") do
execute
- end
+ end if @add_switches
end
def execute #:nodoc:
version = command_parser.main_options.version
version = version.join('.') if version.kind_of?(Array)