Class: CmdParse::VersionCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/cmdparse.rb

Overview

The default version command.

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

Instance Attribute Summary

Attributes inherited from Command

#commands, #data, #default_command, #name, #super_command

Instance Method Summary collapse

Methods inherited from Command

#<=>, #action, #add_command, #argument_desc, #arity, #command_chain, #command_parser, #help, #help_arguments, #help_banner, #help_commands, #help_long_desc, #help_options, #help_short_desc, #long_desc, #options, #short_desc, #takes_arguments?, #takes_commands, #takes_commands?, #usage, #usage_arguments, #usage_commands, #usage_options

Constructor Details

#initialize(add_switches: true) ⇒ VersionCommand

Create a new version command.

Options:

add_switches

Specifies whether the ‘-v’ and ‘–version’ switches should be added to the CommandParser#main_options



718
719
720
721
722
# File 'lib/cmdparse.rb', line 718

def initialize(add_switches: true)
  super('version', takes_commands: false)
  short_desc("Show the version of the program")
  @add_switches = add_switches
end

Instance Method Details

#executeObject

:nodoc:



730
731
732
733
734
735
736
# File 'lib/cmdparse.rb', line 730

def execute #:nodoc:
  version = command_parser.main_options.version
  version = version.join('.') if version.kind_of?(Array)
  puts command_parser.main_options.banner + "\n" if command_parser.main_options.banner?
  puts "#{command_parser.main_options.program_name} #{version}"
  exit
end

#on_after_addObject

:nodoc:



724
725
726
727
728
# File 'lib/cmdparse.rb', line 724

def on_after_add #:nodoc:
  command_parser.main_options.on_tail("--version", "-v", "Show the version of the program") do
    execute
  end if @add_switches
end