lib/dry/cli/command.rb in dry-cli-0.6.0 vs lib/dry/cli/command.rb in dry-cli-0.7.0
- old
+ new
@@ -1,10 +1,9 @@
# frozen_string_literal: true
-require 'forwardable'
-require 'concurrent/array'
-require 'dry/cli/option'
+require "forwardable"
+require "dry/cli/option"
module Dry
class CLI
# Base class for commands
#
@@ -12,31 +11,26 @@
class Command
# @since 0.1.0
# @api private
def self.inherited(base)
super
+ base.class_eval do
+ @_mutex = Mutex.new
+ @description = nil
+ @examples = []
+ @subcommands = []
+ @arguments = base.superclass_arguments || []
+ @options = base.superclass_options || []
+ end
base.extend ClassMethods
end
# @since 0.1.0
# @api private
module ClassMethods
# @since 0.1.0
# @api private
- def self.extended(base)
- super
-
- base.class_eval do
- @description = nil
- @examples = Concurrent::Array.new
- @arguments = Concurrent::Array.new
- @options = Concurrent::Array.new
- end
- end
-
- # @since 0.1.0
- # @api private
attr_reader :description
# @since 0.1.0
# @api private
attr_reader :examples
@@ -46,10 +40,18 @@
attr_reader :arguments
# @since 0.1.0
# @api private
attr_reader :options
+
+ # @since 0.7.0
+ # @api private
+ attr_reader :subcommands
+
+ # @since 0.7.0
+ # @api private
+ attr_writer :subcommands
end
# Set the description of the command
#
# @param description [String] the description
@@ -101,11 +103,11 @@
# # foo server --server=webrick # Force `webrick` server engine
# # foo server --host=0.0.0.0 # Bind to a host
# # foo server --port=2306 # Bind to a port
# # foo server --no-code-reloading # Disable code reloading
def self.example(*examples)
- @examples += examples.flatten
+ @examples += examples.flatten(1)
end
# Specify an argument
#
# @param name [Symbol] the argument name
@@ -240,11 +242,11 @@
#
# # $ foo console --engine=pry
# # starting console (engine: pry)
#
# # $ foo console --engine=foo
- # # Error: Invalid param provided
+ # # ERROR: Invalid param provided
#
# @example Description
# require "dry/cli"
#
# class Console < Dry::CLI::Command
@@ -315,11 +317,13 @@
end
# @since 0.1.0
# @api private
def self.params
- (@arguments + @options).uniq
+ @_mutex.synchronize do
+ (@arguments + @options).uniq
+ end
end
# @since 0.1.0
# @api private
def self.default_params
@@ -338,10 +342,36 @@
# @api private
def self.optional_arguments
arguments.reject(&:required?)
end
+ # @since 0.7.0
+ # @api private
+ def self.subcommands
+ subcommands
+ end
+
+ # @since 0.7.0
+ # @api private
+ def self.superclass_variable_dup(var)
+ if superclass.instance_variable_defined?(var)
+ superclass.instance_variable_get(var).dup
+ end
+ end
+
+ # @since 0.7.0
+ # @api private
+ def self.superclass_arguments
+ superclass_variable_dup(:@arguments)
+ end
+
+ # @since 0.7.0
+ # @api private
+ def self.superclass_options
+ superclass_variable_dup(:@options)
+ end
+
extend Forwardable
delegate %i[
description
examples
@@ -349,9 +379,10 @@
options
params
default_params
required_arguments
optional_arguments
- ] => 'self.class'
+ subcommands
+ ] => "self.class"
end
end
end