Class: Clin::Command

Inherits:
CommandOptionsMixin show all
Defined in:
lib/clin/command.rb

Overview

Clin Command

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandOptionsMixin

add_option, execute_general_options, flag_option, general_option, list_flag_option, list_option, opt_option, option, register_options, remove_general_option

Constructor Details

#initialize(params = {}) ⇒ Command


144
145
146
147
# File 'lib/clin/command.rb', line 144

def initialize(params = {})
  @params = params
  self.class.execute_general_options(params)
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params


142
143
144
# File 'lib/clin/command.rb', line 142

def params
  @params
end

Class Method Details

.abstract(value) ⇒ Object

Mark the class as abstract


34
35
36
# File 'lib/clin/command.rb', line 34

def self.abstract(value)
  self._abstract = value
end

.arguments(args) ⇒ Object


66
67
68
69
70
71
# File 'lib/clin/command.rb', line 66

def self.arguments(args)
  self.args = []
  [*args].map(&:split).flatten.each do |arg|
    self.args += [Clin::Argument.new(arg)]
  end
end

78
79
80
# File 'lib/clin/command.rb', line 78

def self.banner
  "Usage: #{usage}"
end

.default_commandsObject


128
129
130
131
132
# File 'lib/clin/command.rb', line 128

def self.default_commands
  # self.constants.map { |c| self.const_get(c) }
  # .select { |c| c.is_a?(Class) && (c < Clin::Command) }
  subcommands
end

.dispatch(args, prefix: nil, commands: nil) ⇒ Object

Redispatch the command to a sub command with the given arguments If no commands are given it will look for Clin::Command in the class namespace e.g. If those 2 classes are defined. `MyDispatcher < Clin::Command` and `MyDispatcher::ChildCommand < Clin::Command` Will test against ChildCommand


115
116
117
# File 'lib/clin/command.rb', line 115

def self.dispatch(args, prefix: nil, commands: nil)
  self._redispatch_args = [[*args], prefix, commands]
end

.dispatch_doc(opts) ⇒ Object


119
120
121
122
123
124
125
126
# File 'lib/clin/command.rb', line 119

def self.dispatch_doc(opts)
  return if _redispatch_args.nil?
  opts.separator 'Examples: '
  commands = (_redispatch_args[2] || default_commands)
  commands.each do |cmd_cls|
    opts.separator "\t#{cmd_cls.usage}"
  end
end

.exe_name(value = nil) ⇒ Object

Set or get the exe name. Executable name that will be display in the usage. If exe_name is not set in a class or it's parent it will use the global setting Clin.exe_name “` class Git < Clin::Command

exe_name 'git'
arguments '<command> <args>...'

end Git.usage # => git <command> <args>… “`


49
50
51
52
# File 'lib/clin/command.rb', line 49

def self.exe_name(value = nil)
  self._exe_name = value unless value.nil?
  self._exe_name ||= Clin.exe_name
end

.inherited(subclass) ⇒ Object

Trigger when a class inherit this class Rest class_attributes that should not be shared with subclass


26
27
28
29
30
31
# File 'lib/clin/command.rb', line 26

def self.inherited(subclass)
  subclass._redispatch_args = nil
  subclass._abstract = false
  subclass._skip_options = false
  super
end

.option_parser(out = {}) ⇒ Object

Build the Option Parser object Used to parse the option Useful for regenerating the help as well.


92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/clin/command.rb', line 92

def self.option_parser(out = {})
  OptionParser.new do |opts|
    opts.banner = banner
    opts.separator ''
    opts.separator 'Options:'
    register_options(opts, out)
    dispatch_doc(opts)
    unless description.blank?
      opts.separator "\nDescription:"
      opts.separator description
    end
    opts.separator ''
  end
end

.parse(argv = ARGV, fallback_help: true) ⇒ Object

Parse the command and initialize the command object with the parsed options


84
85
86
87
# File 'lib/clin/command.rb', line 84

def self.parse(argv = ARGV, fallback_help: true)
  parser = Clin::CommandParser.new(self, argv, fallback_help: fallback_help)
  parser.parse
end

.redispatch?Boolean


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

def self.redispatch?
  !_redispatch_args.nil?
end

.skip_options(value) ⇒ Object


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

def self.skip_options(value)
  self._skip_options = value
end

.skip_options?Boolean


58
59
60
# File 'lib/clin/command.rb', line 58

def self.skip_options?
  _skip_options
end

.subcommandsObject

List the subcommands The subcommands are all the Classes inheriting this one that are not set to abstract


136
137
138
# File 'lib/clin/command.rb', line 136

def self.subcommands
  subclasses.reject(&:_abstract)
end

.usageObject


73
74
75
76
# File 'lib/clin/command.rb', line 73

def self.usage
  a = [exe_name, args.map(&:original).join(' '), '[Options]']
  a.reject(&:blank?).join(' ')
end