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



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

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



79
80
81
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 79

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

#check_subcommands_argObject



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

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

#docopt_optionsObject



59
60
61
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 59

def docopt_options
  super.merge(options_first: true)
end

#run_with_subcommandObject



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

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

#run_without_subcommandObject



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

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

#subcommandObject



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

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)


67
68
69
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 67

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

#subcommand_argsObject



71
72
73
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 71

def subcommand_args
  options.fetch(SUBCOMMAND_ARGS_ARG)
end

#subcommand_class_name(subcommand) ⇒ Object



63
64
65
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 63

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

#subcommand_programObject



75
76
77
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 75

def subcommand_program
  subcommand_name
end

#target_docObject



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

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