class Geny::Command
Constants
- FILENAME
The filename for a generator, relative to the root
- SEPARATOR
Nested commands are joined with colon
- TEMPLATES
The directory where templates are stored, relative to the root
Attributes
The name of the command @return [String]
The registry that was used to find this command @return [Registry]
The root directory for the command @return [String]
Public Class Methods
Create a new command @param registry [Registry] registry used to find this command @param name [String] name of the command @param root [String] name of the command
# File lib/geny/command.rb, line 31 def initialize(name:, root:, registry:) @name = name @root = root @registry = registry end
Public Instance Methods
Defines the behavior of a command. The block is evaluated within the context of a DSL
.
@example
command = Commmand.new(name: "foo", root: "path/to/root") command.define do parse {} invoke {} end
# File lib/geny/command.rb, line 102 def define(&block) @dsl = DSL.new @dsl.instance_eval(&block) self end
The description for a command @return [String]
# File lib/geny/command.rb, line 63 def description parser.description end
The path where the command is located @return [String]
# File lib/geny/command.rb, line 39 def file File.join(root, FILENAME) end
The command's helper modules @return [Array<Module>]
# File lib/geny/command.rb, line 57 def helpers dsl.helpers end
Invoke a command with options @raise [Argy::ValidationError] when required arguments are missing @param options [Hash{Symbol => Object}]
# File lib/geny/command.rb, line 87 def invoke(**options) options = parser.default_values.merge(options) parser.validate!(options) invoke!(options) end
Parse command-line options @param argv [Array<String>] @raise [Argy::ParseError] when the arguments are invalid @raise [Argy::CoersionError] when the arguments cannot be coerced @raise [Argy::ValidationError] when required arguments are missing @return [Argy::Options]
# File lib/geny/command.rb, line 73 def parse(argv) parser.parse(argv) end
The command's option parser @return [Argy::Parser]
# File lib/geny/command.rb, line 51 def parser dsl.parser end
Parse command-line options and run the command @param argv [Array<String>]
# File lib/geny/command.rb, line 79 def run(argv) options = parse(argv) invoke!(options.to_h) end
The path where templates are located @return [String]
# File lib/geny/command.rb, line 45 def templates_path File.join(root, TEMPLATES) end
Private Instance Methods
# File lib/geny/command.rb, line 124 def dsl return @dsl if @dsl @dsl = DSL.new @dsl.instance_eval File.read(file), file @dsl end
# File lib/geny/command.rb, line 119 def extend_with_queries(options) queries = options.map { |k, v| [:"#{k}?", !!v] } options.merge(queries.to_h) end
# File lib/geny/command.rb, line 110 def invoke!(options) context = Context::Invoke.new( command: self, locals: extend_with_queries(options) ) context.instance_eval(&dsl.invoke) end