Class: EacRubyUtils::Console::DocoptRunner

Inherits:
Object
  • Object
show all
Includes:
EacRubyUtils::Contextualizable, SettingsProvider
Defined in:
lib/eac_ruby_utils/console/docopt_runner.rb,
lib/eac_ruby_utils/console/docopt_runner/_doc.rb,
lib/eac_ruby_utils/console/docopt_runner/_settings.rb,
lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb,
lib/eac_ruby_utils/console/docopt_runner/_class_methods.rb

Defined Under Namespace

Modules: SubcommandsSupport

Constant Summary collapse

PROGRAM_MACRO =
'__PROGRAM__'
SUBCOMMAND_ARG =
'<subcommand>'
SUBCOMMAND_ARGS_ARG =
'<subcommand-args>'
SUBCOMMANDS_MACRO =
'__SUBCOMMANDS__'
DOCOPT_ERROR_EXIT_CODE =
0xC0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SettingsProvider

#setting_constant_name, #setting_search_order, #setting_value_by_constant, #setting_value_by_method, #setting_value_by_settings_object, #settings_object, #settings_object_name

Methods included from EacRubyUtils::Contextualizable

#context

Constructor Details

#initialize(settings = {}) ⇒ DocoptRunner

Returns a new instance of DocoptRunner.


25
26
27
28
# File 'lib/eac_ruby_utils/console/docopt_runner.rb', line 25

def initialize(settings = {})
  @settings = settings.with_indifferent_access.freeze
  check_subcommands
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings


23
24
25
# File 'lib/eac_ruby_utils/console/docopt_runner.rb', line 23

def settings
  @settings
end

Class Method Details

.create(settings = {}) ⇒ Object


18
19
20
# File 'lib/eac_ruby_utils/console/docopt_runner.rb', line 18

def create(settings = {})
  new(settings)
end

.run(options = {}) ⇒ Object


9
10
11
12
13
14
# File 'lib/eac_ruby_utils/console/docopt_runner/_class_methods.rb', line 9

def run(options = {})
  create(options).send(:run)
rescue Docopt::Exit => e
  STDERR.write(e.message + "\n")
  ::Kernel.exit(DOCOPT_ERROR_EXIT_CODE) # rubocop:disable Rails/Exit
end

Instance Method Details

#check_subcommandsObject


17
18
19
20
21
22
23
24
25
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 17

def check_subcommands
  return unless subcommands?

  singleton_class.include(SubcommandsSupport)
  check_subcommands_arg
  return if singleton_class.method_defined?(:run)

  singleton_class.send(:alias_method, :run, :run_with_subcommand)
end

#optionsObject


30
31
32
# File 'lib/eac_ruby_utils/console/docopt_runner.rb', line 30

def options
  @options ||= Docopt.docopt(target_doc, docopt_options)
end

#parentObject


34
35
36
# File 'lib/eac_ruby_utils/console/docopt_runner.rb', line 34

def parent
  settings[:parent]
end

#source_docObject


8
9
10
# File 'lib/eac_ruby_utils/console/docopt_runner/_doc.rb', line 8

def source_doc
  setting_value(:doc)
end

#source_program_nameObject


16
17
18
# File 'lib/eac_ruby_utils/console/docopt_runner/_doc.rb', line 16

def source_program_name
  setting_value(:program_name, false)
end

#subcommands?Boolean

Returns:


13
14
15
# File 'lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb', line 13

def subcommands?
  source_doc.include?(SUBCOMMANDS_MACRO)
end

#target_docObject


12
13
14
# File 'lib/eac_ruby_utils/console/docopt_runner/_doc.rb', line 12

def target_doc
  source_doc.gsub(PROGRAM_MACRO, target_program_name).strip + "\n"
end

#target_program_nameObject


20
21
22
# File 'lib/eac_ruby_utils/console/docopt_runner/_doc.rb', line 20

def target_program_name
  [source_program_name, ENV['PROGRAM_NAME'], $PROGRAM_NAME].find(&:present?)
end