Module: Prmd::CLI::Base

Included in:
Combine, Doc, Generate, Render, Verify
Defined in:
lib/prmd/cli/base.rb

Overview

Base module for CLI commands.

Instance Method Summary collapse

Instance Method Details

#execute(options = {}) ⇒ void

This method is abstract.

This method returns an undefined value.

Method called to actually execute the command provided with an options Hash from the commands #parse_options.

Parameters:

  • options (Hash<Symbol, Object>) (defaults to: {})


113
114
115
# File 'lib/prmd/cli/base.rb', line 113

def execute(options = {})
  #
end

#make_parser(options = {}) ⇒ OptionParser

This method is abstract.

Create a parser specific for this command. The parsers produced by this method should yield their options.

Examples:

Overwriting

def make_parser(options = {})
  OptionParser.new do |opts|
    opts.on("-v", "--verbose", "set verbose debugging") do |v|
      yield :verbose, v
    end
  end
end

Parameters:

  • options (Hash<Symbol, Object>) (defaults to: {})

Returns:



25
26
27
# File 'lib/prmd/cli/base.rb', line 25

def make_parser(options = {})
  #
end

#noop_execute(options = {}) ⇒ void

This method returns an undefined value.

Method called when the command is ran with the :noop option enabled. As the option implies, this should do absolutely nothing.

Parameters:

  • options (Hash<Symbol, Object>) (defaults to: {})


122
123
124
# File 'lib/prmd/cli/base.rb', line 122

def noop_execute(options = {})
  $stderr.puts options
end

#parse_options(argv, options = {}) ⇒ Hash<Symbol, Object>

Parse the given argv and produce a options Hash specific to the command. The returned options Hash will include an :argv key which contains the remaining args from the parse operation.

Parameters:

  • argv (Array<String>)
  • options (Hash<Symbol, Object>) (defaults to: {})

Returns:

  • (Hash<Symbol, Object>)

    parsed options



67
68
69
70
71
72
73
74
75
# File 'lib/prmd/cli/base.rb', line 67

def parse_options(argv, options = {})
  opts = {}
  parser = make_parser(options) do |key, value|
    set_option(opts, key, value)
  end
  argv = execute_parser(parser, argv)
  opts[:argv] = argv
  opts
end

#run(argv, options = {}) ⇒ void

This method returns an undefined value.

Run this command given a argv and optional options Hash. If all you have is the options from the #parse_options method, use #execute instead.

Parameters:

  • argv (Array<String>)
  • options (Hash<Symbol, Object>) (defaults to: {})

See Also:



136
137
138
139
140
141
142
143
# File 'lib/prmd/cli/base.rb', line 136

def run(argv, options = {})
  options = options.merge(parse_options(argv, options))
  if options[:noop]
    noop_execute(options)
  else
    execute(options)
  end
end