Class: GitCommander::Command

Inherits:
Object
  • Object
show all
Includes:
CommandLoaderOptions
Defined in:
lib/git_commander/command.rb,
lib/git_commander/command/option.rb,
lib/git_commander/command/runner.rb,
lib/git_commander/command/loaders/raw.rb,
lib/git_commander/command/configurator.rb,
lib/git_commander/command/loaders/file_loader.rb

Overview

Wraps domain logic for executing git-cmd Commands

Defined Under Namespace

Modules: Loaders Classes: Configurator, Option, Runner

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandLoaderOptions

#argument, #description, #flag, #on_run, #summary, #switch

Constructor Details

#initialize(name, registry: nil, **options) {|run_options| ... } ⇒ Command

Returns a new instance of Command.

Parameters:

  • name (String, Symbol)

    the name of the command

  • registry (GitCommander::Registry) (defaults to: nil)

    (GitCommander::Registry.new) the command registry to use lookups

  • options (Hash)

    the options to create the command with

Options Hash (**options):

  • :description (String) — default: nil

    a short description to use in the single line version of the command’s help output

  • :summary (String) — default: nil

    the long-form description of the command to use in the command’s help output

  • :output (IO) — default: STDOUT

    the IO object you want to use to send outut from the command to

  • :arguments (Array)

    an array of hashes describing the argument names and default values that can be supplied to the command

  • :flags (Array)

    an array of hashes describing the flags and default values that can be supplied to the command

  • :switches (Array)

    an array of hashes describing the switches and default values that can be supplied to the command

Yield Parameters:

  • run_options (Array<Option>)

    an Array of Option instances defined from the above options



38
39
40
41
42
43
44
45
46
47
# File 'lib/git_commander/command.rb', line 38

def initialize(name, registry: nil, **options, &block)
  @name = name
  @description = options[:description]
  @summary = options[:summary]
  @block = block_given? ? block : proc {}
  @registry = registry || GitCommander::Registry.new
  @output = options[:output] || STDOUT

  define_command_options(options)
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



14
15
16
# File 'lib/git_commander/command.rb', line 14

def arguments
  @arguments
end

#blockObject (readonly)

Returns the value of attribute block.



14
15
16
# File 'lib/git_commander/command.rb', line 14

def block
  @block
end

#flagsObject (readonly)

Returns the value of attribute flags.



14
15
16
# File 'lib/git_commander/command.rb', line 14

def flags
  @flags
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/git_commander/command.rb', line 14

def name
  @name
end

#outputObject

Returns the value of attribute output.



15
16
17
# File 'lib/git_commander/command.rb', line 15

def output
  @output
end

#registryObject (readonly)

Returns the value of attribute registry.



14
15
16
# File 'lib/git_commander/command.rb', line 14

def registry
  @registry
end

#switchesObject (readonly)

Returns the value of attribute switches.



14
15
16
# File 'lib/git_commander/command.rb', line 14

def switches
  @switches
end

Instance Method Details

#add_option(option_type, options = {}) ⇒ Object

Add to this Command’s #arguments, #flags, or #switches

Parameters:

  • option_type (String, Symbol)

    the type of option to add

  • options (Hash) (defaults to: {})

    the options to create the [Option] with

Options Hash (options):

  • :name (String, Symbol)

    the name of the option to add

  • :default (Object) — default: nil

    the default value of the Option

  • :description (String) — default: nil

    a description of the Option to use in help text output

  • :value (Object) — default: nil

    the value on of the Option



97
98
99
100
101
102
103
104
105
106
# File 'lib/git_commander/command.rb', line 97

def add_option(option_type, options = {})
  case option_type.to_sym
  when :argument
    @arguments << Option.new(**options)
  when :flag
    @flags << Option.new(**options)
  when :switch
    @switches << Option.new(**options)
  end
end

#helpObject

Adds command-line help text to the #output of this Command



68
69
70
71
72
73
74
75
76
# File 'lib/git_commander/command.rb', line 68

def help
  say "NAME"
  say "    git-cmd #{name} – #{summary}"
  say "USAGE"
  say "    git-cmd #{name} [command options] #{arguments.map { |arg| "[#{arg.name}]" }.join(" ")}"
  description_help
  argument_help
  options_help
end

#optionsSet

Access to a unique Set of this Command’s #arguments, #flags, and #switches

Returns:

  • (Set)

    a unique list of all options this command can accept



82
83
84
# File 'lib/git_commander/command.rb', line 82

def options
  Set.new(@arguments + @flags + @switches)
end

#run(run_options = []) ⇒ Object

Executes the block for the command with the provided run_options.

Parameters:

  • run_options (Array<Option>) (defaults to: [])

    an array of Option(s) to pass to the #block of this Command



53
54
55
56
# File 'lib/git_commander/command.rb', line 53

def run(run_options = [])
  assign_option_values(run_options)
  Runner.new(self).run options.map(&:to_h).reduce(:merge)
end

#say(message) ⇒ Object

Appends the message to the Command’s #output

Parameters:

  • message (String)

    the string to append to the #output



62
63
64
# File 'lib/git_commander/command.rb', line 62

def say(message)
  output.puts message
end