Sha256: 0c368ea2844a62be64e3ceaeb3cbb963c5afbba2dcdc1adcb13870f4650e9e1a

Contents?: true

Size: 1.65 KB

Versions: 7

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require_relative '../options'

module CommandKit
  module Options
    #
    # Defines a version option.
    #
    # ## Examples
    #
    #     include CommandKit::Options::Version
    #     
    #     version '0.1.0'
    #     
    #     def run(*argv)
    #       # ...
    #     end
    #
    module Version
      #
      # Includes {Options}, extends {Version::ClassMethods}, and defines a
      # `-V, --version` option.
      #
      def self.included(command)
        command.include Options
        command.extend ClassMethods

        command.option :version, short: '-V',
                                 desc: 'Prints the version and exits' do
          print_version
          exit(0)
        end
      end

      #
      # Defines class-level methods.
      #
      module ClassMethods
        #
        # Gets or sets the version string.
        #
        # @param [String, nil] new_version
        #   If given a new version String, it will set the class'es version
        #   string.
        #
        # @return [String, nil]
        #   The classes version string.
        #
        # @api public
        #
        def version(new_version=nil)
          if new_version
            @version = new_version
          else
            @version || (superclass.version if superclass.kind_of?(ClassMethods))
          end
        end
      end

      #
      # @see ClassMethods#version
      #
      # @api public
      #
      def version
        self.class.version
      end

      #
      # Prints the version.
      #
      # @api public
      #
      def print_version
        puts "#{command_name} #{version}"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
command_kit-0.6.0 lib/command_kit/options/version.rb
command_kit-0.5.6 lib/command_kit/options/version.rb
command_kit-0.5.5 lib/command_kit/options/version.rb
command_kit-0.5.4 lib/command_kit/options/version.rb
command_kit-0.5.3 lib/command_kit/options/version.rb
command_kit-0.5.2 lib/command_kit/options/version.rb
command_kit-0.5.1 lib/command_kit/options/version.rb