Module: Clin::CommandMixin::Core::ClassMethods

Defined in:
lib/clin/command_mixin/core.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_abstractObject

Returns the value of attribute _abstract.



15
16
17
# File 'lib/clin/command_mixin/core.rb', line 15

def _abstract
  @_abstract
end

#_argumentsObject

Returns the value of attribute _arguments.



13
14
15
# File 'lib/clin/command_mixin/core.rb', line 13

def _arguments
  @_arguments
end

#_default_priorityObject

Returns the value of attribute _default_priority.



18
19
20
# File 'lib/clin/command_mixin/core.rb', line 18

def _default_priority
  @_default_priority
end

#_descriptionObject

Returns the value of attribute _description.



14
15
16
# File 'lib/clin/command_mixin/core.rb', line 14

def _description
  @_description
end

#_exe_nameObject

Returns the value of attribute _exe_name.



16
17
18
# File 'lib/clin/command_mixin/core.rb', line 16

def _exe_name
  @_exe_name
end

#_priorityObject

Returns the value of attribute _priority.



19
20
21
# File 'lib/clin/command_mixin/core.rb', line 19

def _priority
  @_priority
end

#_skip_optionsObject

Returns the value of attribute _skip_options.



17
18
19
# File 'lib/clin/command_mixin/core.rb', line 17

def _skip_options
  @_skip_options
end

Instance Method Details

#abstract(value) ⇒ Object

Mark the class as abstract



36
37
38
# File 'lib/clin/command_mixin/core.rb', line 36

def abstract(value)
  @_abstract = value
end

#abstract?Boolean

Return if the current command class is abstract

Returns:

  • (Boolean)


42
43
44
# File 'lib/clin/command_mixin/core.rb', line 42

def abstract?
  @_abstract
end

#arguments(args = nil) ⇒ Object Also known as: args

Set or get the arguments for the command

Parameters:

  • args (Array<String>) (defaults to: nil)

    List of arguments to set. If nil it just return the current args.



75
76
77
78
79
80
81
# File 'lib/clin/command_mixin/core.rb', line 75

def arguments(args = nil)
  return @_arguments if args.nil?
  @_arguments = []
  [*args].map(&:split).flatten.each do |arg|
    @_arguments << Clin::Argument.new(arg)
  end
end


97
98
99
# File 'lib/clin/command_mixin/core.rb', line 97

def banner
  "Usage: #{usage}"
end

#default_commandsObject



124
125
126
# File 'lib/clin/command_mixin/core.rb', line 124

def default_commands
  subcommands.sort_by(&:priority).reverse
end

#description(desc = nil) ⇒ Object

Set or get the description

Parameters:

  • desc (String) (defaults to: nil)

    Description to set. If nil it just return the current value.



87
88
89
90
# File 'lib/clin/command_mixin/core.rb', line 87

def description(desc = nil)
  @_description = desc unless desc.nil?
  @_description
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>… “‘

Parameters:

  • value (String) (defaults to: nil)

    name of the exe.



57
58
59
60
# File 'lib/clin/command_mixin/core.rb', line 57

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

#helpObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/clin/command_mixin/core.rb', line 134

def help
  Clin::Text.new do |t|
    t.line banner
    t.blank
    t.line 'Options:'
    t.text option_help, indent: 2
    unless description.blank?
      t.line 'Description:'
      t.line description, indent: 2
    end
  end
end

#inherited(subclass) ⇒ Object

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

Parameters:



24
25
26
27
28
29
30
31
32
33
# File 'lib/clin/command_mixin/core.rb', line 24

def inherited(subclass)
  subclass._arguments = []
  subclass._description = ''
  subclass._abstract = false
  subclass._skip_options = false
  subclass._exe_name = @_exe_name
  subclass._default_priority = @_default_priority.to_f / 2
  subclass._priority = 0
  super
end

#prioritize(value = 1) ⇒ Object

Priorities this command. This does not set the priority. It add value to the default priority The default priority is computed using half of the parent default priority. e.g. “‘ Parent = Class.new(Clin::Command) Child1 = Class.new(Parent) Child2 = Class.new(Parent) Parent.priority # => 500 Child1.priority # => 250 Child2.priority # => 250 Child2.prioritize Child2.priority # => 251 “` When dispatching commands they are sorted by priority



116
117
118
# File 'lib/clin/command_mixin/core.rb', line 116

def prioritize(value = 1)
  @_priority = value
end

#priorityObject



120
121
122
# File 'lib/clin/command_mixin/core.rb', line 120

def priority
  @_default_priority + @_priority
end

#skip_options(value) ⇒ Object

Allow the current option to skip unknown options.

Parameters:

  • value (Boolean)

    skip or not



64
65
66
# File 'lib/clin/command_mixin/core.rb', line 64

def skip_options(value)
  @_skip_options = value
end

#skip_options?Boolean

Is the current command skipping options

Returns:

  • (Boolean)


69
70
71
# File 'lib/clin/command_mixin/core.rb', line 69

def skip_options?
  @_skip_options
end

#subcommandsObject

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



130
131
132
# File 'lib/clin/command_mixin/core.rb', line 130

def subcommands
  subclasses.reject(&:abstract?)
end

#usageObject



92
93
94
95
# File 'lib/clin/command_mixin/core.rb', line 92

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