Module: EacRubyUtils::Console::DocoptRunner::SubcommandsSupport

Defined in:
lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb

Defined Under Namespace

Modules: SubcommandArgAsArg, SubcommandArgAsList

Constant Summary collapse

EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME =
:extra_available_subcommands

Instance Method Summary collapse

Instance Method Details

#auto_available_subcommandsObject


89
90
91
92
93
94
95
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 89

def auto_available_subcommands
  self.class.constants
      .map { |name| self.class.const_get(name) }
      .select { |c| c.instance_of? Class }
      .select { |c| c < ::EacRubyUtils::Console::DocoptRunner }
      .map { |c| c.name.demodulize.underscore.dasherize }
end

#available_subcommandsObject


81
82
83
84
85
86
87
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 81

def available_subcommands
  r = ::Set.new(setting_value(:subcommands, false) || auto_available_subcommands)
  if respond_to?(EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME, true)
    r += send(EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME)
  end
  r.sort
end

#check_subcommands_argObject


30
31
32
33
34
35
36
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 30

def check_subcommands_arg
  if subcommand_arg_as_list?
    singleton_class.include(SubcommandsSupport::SubcommandArgAsList)
  else
    singleton_class.include(SubcommandsSupport::SubcommandArgAsArg)
  end
end

#docopt_optionsObject


61
62
63
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 61

def docopt_options
  super.merge(options_first: true)
end

#run_with_subcommandObject


38
39
40
41
42
43
44
45
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 38

def run_with_subcommand
  if subcommand_name
    check_valid_subcommand
    subcommand.run
  else
    run_without_subcommand
  end
end

#run_without_subcommandObject


97
98
99
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 97

def run_without_subcommand
  "Method #{__method__} should be overrided in #{self.class.name}"
end

#subcommandObject


47
48
49
50
51
52
53
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 47

def subcommand
  @subcommand ||= subcommand_class_name(subcommand_name).constantize.create(
    argv: subcommand_args,
    program_name: subcommand_program,
    parent: self
  )
end

#subcommand_arg_as_list?Boolean

Returns:


69
70
71
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 69

def subcommand_arg_as_list?
  setting_value(:subcommand_arg_as_list, false) || false
end

#subcommand_argsObject


73
74
75
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 73

def subcommand_args
  options.fetch(SUBCOMMAND_ARGS_ARG)
end

#subcommand_class_name(subcommand) ⇒ Object


65
66
67
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 65

def subcommand_class_name(subcommand)
  "#{self.class.name}::#{subcommand.underscore.camelize}"
end

#subcommand_programObject


77
78
79
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 77

def subcommand_program
  subcommand_name
end

#target_docObject


55
56
57
58
59
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 55

def target_doc
  super.gsub(SUBCOMMANDS_MACRO,
             "#{target_doc_subcommand_arg} [#{SUBCOMMAND_ARGS_ARG}...]") +
    "\n" + subcommands_target_doc
end