module Eco module API class UseCases class Cli module DSL # Links the usecase to the Cli via `arg_case` def apply!(arg_case = cli_name) #puts "DEFINING CLI for '#{arg_case}' via #{self}" if applied?(arg_case) puts "Warning: (#{self}) Tried to call again cli.apply! on '#{arg_case}'" return self end cli_config_case(arg_case) apply_options(arg_case) applied!(arg_case) self end def applied @applied ||= {} end def applied?(arg_case) applied[arg_case] || false end def applied!(arg_case) applied[arg_case] = true end attr_writer :usecase # Unless specified, assume Cli class hangs from its case namespace def usecase raise "#{self} is to use to extend a class" unless self.is_a?(Class) @usecase ||= Kernel.const_get(self.to_s.split('::')[0..-2].join('::')) end def description(value = nil) @description = (value.nil? ? @description : value) end alias_method :desc, :description def type usecase.type end def name usecase.name end # It defaults to the use case preceded by dash def cli_name(arg_name = nil) @cli_name = (arg_name.nil? ? @cli_name : arg_name).yield_self do |value| value = "-#{name}" if value.nil? value end end def callback(&block) return @callback unless block_given? @callback = block end def options @options ||= {} end def add_option(arg, desc = nil, &block) self.tap do "Overriding option '#{arg}' on case '#{name}'" if options.key?(arg) @options[arg] = Eco::API::UseCases::Cli::Option.new(arg, desc, &block) end end private def apply_options(arg_case) options.each do |_key, option| option.link_case(cli_config_case(arg_case)) end end def cli_config_case(arg_case = nil) @cli_config_case ||= usecases.add(arg_case, type, desc, case_name: name, &callback) end def usecases(&block) ::ASSETS.cli.config.usecases(&block) end end end end end end