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

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

Defined Under Namespace

Modules: SubcommandArgAsArg, SubcommandArgAsList

Instance Method Summary collapse

Instance Method Details

#auto_available_subcommandsObject



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

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



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

def available_subcommands
  (setting_value(:subcommands, false) || auto_available_subcommands).sort
end

#check_subcommands_argObject



26
27
28
29
30
31
32
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 26

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

#docopt_optionsObject



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

def docopt_options
  super.merge(options_first: true)
end

#run_with_subcommandObject



34
35
36
37
38
39
40
41
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 34

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

#run_without_subcommandObject



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

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

#subcommandObject



43
44
45
46
47
48
49
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 43

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

#subcommand_arg_as_list?Boolean

Returns:

  • (Boolean)


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

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

#subcommand_argsObject



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

def subcommand_args
  options.fetch(SUBCOMMAND_ARGS_ARG)
end

#subcommand_class_name(subcommand) ⇒ Object



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

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

#subcommand_programObject



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

def subcommand_program
  subcommand_name
end

#target_docObject



51
52
53
54
55
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 51

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