Class: Clin::Command
- Inherits:
-
CommandOptionsMixin
- Object
- CommandOptionsMixin
- Clin::Command
- Defined in:
- lib/clin/command.rb
Overview
Clin Command
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
Class Method Summary collapse
-
.abstract(value) ⇒ Object
Mark the class as abstract.
- .arguments(args) ⇒ Object
- .banner ⇒ Object
- .default_commands ⇒ Object
-
.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.
- .dispatch_doc(opts) ⇒ Object
-
.exe_name(value = nil) ⇒ Object
Set or get the exe name.
-
.inherited(subclass) ⇒ Object
Trigger when a class inherit this class Rest class_attributes that should not be shared with subclass.
-
.option_parser(out = {}) ⇒ Object
Build the Option Parser object Used to parse the option Useful for regenerating the help as well.
-
.parse(argv = ARGV, fallback_help: true) ⇒ Object
Parse the command and initialize the command object with the parsed options.
- .redispatch? ⇒ Boolean
- .skip_options(value) ⇒ Object
- .skip_options? ⇒ Boolean
-
.subcommands ⇒ Object
List the subcommands The subcommands are all the Classes inheriting this one that are not set to abstract.
- .usage ⇒ Object
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Command
constructor
A new instance of Command.
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
Returns a new instance of Command.
144 145 146 147 |
# File 'lib/clin/command.rb', line 144 def initialize(params = {}) @params = params self.class.(params) end |
Instance Attribute Details
#params ⇒ Object
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 |
.banner ⇒ Object
78 79 80 |
# File 'lib/clin/command.rb', line 78 def self. "Usage: #{usage}" end |
.default_commands ⇒ Object
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. = 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. = opts.separator '' opts.separator '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.(value) self. = value end |
.skip_options? ⇒ Boolean
58 59 60 |
# File 'lib/clin/command.rb', line 58 def self. end |
.subcommands ⇒ Object
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 |
.usage ⇒ Object
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 |