Module: Clin::CommandMixin::Core::ClassMethods
- Defined in:
- lib/clin/command_mixin/core.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#_abstract ⇒ Object
Returns the value of attribute _abstract.
-
#_arguments ⇒ Object
Returns the value of attribute _arguments.
-
#_default_priority ⇒ Object
Returns the value of attribute _default_priority.
-
#_description ⇒ Object
Returns the value of attribute _description.
-
#_exe_name ⇒ Object
Returns the value of attribute _exe_name.
-
#_priority ⇒ Object
Returns the value of attribute _priority.
-
#_skip_options ⇒ Object
Returns the value of attribute _skip_options.
Instance Method Summary collapse
-
#abstract(value) ⇒ Object
Mark the class as abstract.
-
#abstract? ⇒ Boolean
Return if the current command class is abstract.
-
#arguments(args = nil) ⇒ Object
(also: #args)
Set or get the arguments for the command.
- #banner ⇒ Object
- #default_commands ⇒ Object
-
#description(desc = nil) ⇒ Object
Set or get the description.
-
#exe_name(value = nil) ⇒ Object
Set or get the exe name.
- #help ⇒ Object
-
#inherited(subclass) ⇒ Object
Trigger when a class inherit this class Rest class_attributes that should not be shared with subclass.
-
#prioritize(value = 1) ⇒ Object
Priorities this command.
- #priority ⇒ Object
-
#skip_options(value) ⇒ Object
Allow the current option to skip unknown options.
-
#skip_options? ⇒ Boolean
Is the current command skipping options.
-
#subcommands ⇒ Object
List the subcommands The subcommands are all the Classes inheriting this one that are not set to abstract.
- #usage ⇒ Object
Instance Attribute Details
#_abstract ⇒ Object
Returns the value of attribute _abstract.
15 16 17 |
# File 'lib/clin/command_mixin/core.rb', line 15 def _abstract @_abstract end |
#_arguments ⇒ Object
Returns the value of attribute _arguments.
13 14 15 |
# File 'lib/clin/command_mixin/core.rb', line 13 def _arguments @_arguments end |
#_default_priority ⇒ Object
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 |
#_description ⇒ Object
Returns the value of attribute _description.
14 15 16 |
# File 'lib/clin/command_mixin/core.rb', line 14 def _description @_description end |
#_exe_name ⇒ Object
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 |
#_priority ⇒ Object
Returns the value of attribute _priority.
19 20 21 |
# File 'lib/clin/command_mixin/core.rb', line 19 def _priority @_priority end |
#_skip_options ⇒ Object
Returns the value of attribute _skip_options.
17 18 19 |
# File 'lib/clin/command_mixin/core.rb', line 17 def @_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
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
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 |
#banner ⇒ Object
97 98 99 |
# File 'lib/clin/command_mixin/core.rb', line 97 def "Usage: #{usage}" end |
#default_commands ⇒ Object
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
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>… “‘
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 |
#help ⇒ Object
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 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
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. = 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 |
#priority ⇒ Object
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.
64 65 66 |
# File 'lib/clin/command_mixin/core.rb', line 64 def (value) @_skip_options = value end |
#skip_options? ⇒ Boolean
Is the current command skipping options
69 70 71 |
# File 'lib/clin/command_mixin/core.rb', line 69 def @_skip_options end |
#subcommands ⇒ Object
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 |
#usage ⇒ Object
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 |