Module: Prmd::CLI

Defined in:
lib/prmd/cli.rb,
lib/prmd/cli/doc.rb,
lib/prmd/cli/base.rb,
lib/prmd/cli/stub.rb,
lib/prmd/cli/render.rb,
lib/prmd/cli/verify.rb,
lib/prmd/cli/combine.rb,
lib/prmd/cli/generate.rb

Overview

Main CLI module

Defined Under Namespace

Modules: Base, Combine, Doc, Generate, Render, Stub, Verify

Class Method Summary collapse

Class Method Details

.commandsArray

List of all available commands



29
30
31
# File 'lib/prmd/cli.rb', line 29

def self.commands
  @commands ||= make_command_parsers.keys
end

.make_command_parsers(props = {}) ⇒ Hash<Symbol, OptionParser>



15
16
17
18
19
20
21
22
23
24
# File 'lib/prmd/cli.rb', line 15

def self.make_command_parsers(props = {})
  {
    combine: CLI::Combine.make_parser(props),
    doc:     CLI::Doc.make_parser(props),
    init:    CLI::Generate.make_parser(props),
    render:  CLI::Render.make_parser(props),
    stub:    CLI::Stub.make_parser(props),
    verify:  CLI::Verify.make_parser(props)
  }
end

.make_parser(options, props = {}) ⇒ Object

Creates the CLI main parser



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/prmd/cli.rb', line 37

def self.make_parser(options, props = {})
  binname = props.fetch(:bin, 'prmd')

  # This is only used to attain the help commands
  commands = make_command_parsers(props)
  help_text = commands.values.map do |command|
    "   #{command.banner}"
  end.join("\n")

  global = OptionParser.new do |opts|
    opts.banner = "Usage: #{binname} [options] [command [options]]"
    opts.separator "\nAvailable options:"
    opts.on('--version', 'Return version') do
      puts "prmd #{Prmd::VERSION}"
      exit(0)
    end
    opts.on('--noop', 'Commands will not execute') do |v|
      options[:noop] = v
    end
    opts.separator "\nAvailable commands:"
    opts.separator help_text
  end

  global
end

.parse_options(argv, opts = {}) ⇒ Hash<Symbol, Object>

Parse top level CLI options from argv



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/prmd/cli.rb', line 68

def self.parse_options(argv, opts = {})
  options = {}
  parser = make_parser(options, opts)
  abort parser if argv.empty?
  com_argv = parser.order(argv)
  abort parser if com_argv.empty?
  command = com_argv.shift.to_sym
  abort parser unless commands.include?(command)
  options[:argv] = com_argv
  options[:command] = command
  options
end

.run(uargv, opts = {}) ⇒ void

This method returns an undefined value.

Execute the Prmd CLI, or its subcommands



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/prmd/cli.rb', line 86

def self.run(uargv, opts = {})
  options = parse_options(uargv, opts)
  argv = options.delete(:argv)
  command = options.delete(:command)

  case command
  when :combine
    CLI::Combine.run(argv, options)
  when :doc
    CLI::Doc.run(argv, options)
  when :init
    CLI::Generate.run(argv, options)
  when :render
    CLI::Render.run(argv, options)
  when :stub
    CLI::Stub.run(argv, options)
  when :verify
    CLI::Verify.run(argv, options)
  end
end