Class: Pfm::CLI

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI, Helpers
Defined in:
lib/iapi-idlc-sdk-pfm/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

debug, err, msg, system_command

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



34
35
36
37
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 34

def initialize(argv)
  @argv = argv
  super() # mixlib-cli #initialize doesn't allow arguments
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



32
33
34
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 32

def argv
  @argv
end

Instance Method Details

#commands_mapObject



94
95
96
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 94

def commands_map
  Pfm.commands_map
end

#exit(n) ⇒ Object



90
91
92
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 90

def exit(n)
  Kernel.exit(n)
end

#handle_optionsObject

If no subcommand is given, then this class is handling the CLI request.



66
67
68
69
70
71
72
73
74
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 66

def handle_options
  parse_options(argv)
  if config[:version]
    show_version
  else
    show_help
  end
  exit 0
end

#have_command?(name) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 98

def have_command?(name)
  commands_map.have_command?(name)
end

#instantiate_subcommand(name) ⇒ Object



118
119
120
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 118

def instantiate_subcommand(name)
  commands_map.instantiate(name)
end

#option?(param) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 110

def option?(param)
  param =~ /^-/
end

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 39

def run
  subcommand_name, *subcommand_params = argv

  ENV['DEBUG'] = true if verbose?

  #
  # Runs the appropriate subcommand if the given parameters contain any
  # subcommands.
  #
  if subcommand_name.nil? || option?(subcommand_name)
    handle_options
  elsif have_command?(subcommand_name)
    subcommand = instantiate_subcommand(subcommand_name)
    exit_code = subcommand.run_with_default_options(subcommand_params)
    exit normalized_exit_code(exit_code)
  else
    err "Unknown command `#{subcommand_name}'."
    show_help
    exit 1
  end
rescue OptionParser::InvalidOption => e
  err(e.message)
  show_help
  exit 1
end

#show_helpObject



80
81
82
83
84
85
86
87
88
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 80

def show_help
  msg(banner)
  msg("\nAvailable Commands:")

  justify_length = subcommands.map(&:length).max + 2
  subcommand_specs.each do |name, spec|
    msg("    #{name.ljust(justify_length)}#{spec.description}")
  end
end

#show_versionObject



76
77
78
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 76

def show_version
  msg("Pfm Version: #{Pfm::VERSION}")
end

#subcommand_specsObject



106
107
108
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 106

def subcommand_specs
  commands_map.command_specs
end

#subcommandsObject



102
103
104
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 102

def subcommands
  commands_map.command_names
end

#verbose?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/iapi-idlc-sdk-pfm/cli.rb', line 114

def verbose?
  @config[:verbose]
end