Module: Prmd::CLI

Defined in:
lib/prmd/cli.rb,
lib/prmd/cli/doc.rb,
lib/prmd/cli/base.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, Verify

Class Method Summary collapse

Class Method Details

.commandsArray

List of all available commands

Returns:

  • (Array)

    available commands



27
28
29
# File 'lib/prmd/cli.rb', line 27

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

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

Returns all dem parsers.

Parameters:

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

Returns:



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

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),
    verify:  CLI::Verify.make_parser(props)
  }
end

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

Creates the CLI main parser

Parameters:

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


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

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

Parameters:

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

Returns:

  • (Hash<Symbol, Object>)

    parsed options



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

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

Parameters:

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


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

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 :verify
    CLI::Verify.run(argv, options)
  end
end