Class: Nugrant::Vagrant::V2::Command::Root

Inherits:
Object
  • Object
show all
Defined in:
lib/nugrant/vagrant/v2/command/root.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments, environment) ⇒ Root

Returns a new instance of Root.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nugrant/vagrant/v2/command/root.rb', line 16

def initialize(arguments, environment)
  super(arguments, environment)

  @arguments, @subcommand, @subarguments = split_main_and_subcommand(arguments)

  # Change super class available arguments to main ones only
  @argv = @arguments

  @subcommands = ::Vagrant::Registry.new()
  @subcommands.register(:env) do
    Command::Env
  end

  @subcommands.register(:parameters) do
    Command::Parameters
  end

  @subcommands.register(:'restricted-keys') do
    Command::RestrictedKeys
  end

  @show_help = false
  @show_version = false
end

Class Method Details

.synopsisObject



12
13
14
# File 'lib/nugrant/vagrant/v2/command/root.rb', line 12

def self.synopsis
  "manage Nugrant user defined parameters (config.user)"
end

Instance Method Details

#create_parserObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nugrant/vagrant/v2/command/root.rb', line 41

def create_parser()
  return OptionParser.new do |parser|
    parser.banner = "Usage: vagrant user [-h] [-v] <subcommand> [<args>]"

    parser.separator ""
    parser.on("-h", "--help", "Prints this help") do
      @show_help = true
    end

    parser.on("-v", "--version", "Prints plugin version and exit.") do
      @show_version = true
    end

    parser.separator ""
    parser.separator "Available subcommands:"

    keys = []
    @subcommands.each { |key, value| keys << key.to_s }

    keys.sort.each do |key|
      parser.separator "     #{key}"
    end

    parser.separator ""
    parser.separator "For help on any individual command run `vagrant user <subcommand> -h`"
  end
end

#executeObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nugrant/vagrant/v2/command/root.rb', line 69

def execute
  parser = create_parser()
  arguments = parse_options(parser)

  return version() if @show_version
  return help(parser) if @show_help

  command_class = @subcommands.get(@subcommand.to_sym) if @subcommand
  return help(parser) if !command_class || !@subcommand

  @logger.debug("Invoking nugrant command class: #{command_class} #{@subarguments.inspect}")

  command_class.new(@subarguments, @env).execute
end

#help(parser) ⇒ Object



84
85
86
# File 'lib/nugrant/vagrant/v2/command/root.rb', line 84

def help(parser)
  @env.ui.info(parser.help, :prefix => false)
end

#versionObject



88
89
90
# File 'lib/nugrant/vagrant/v2/command/root.rb', line 88

def version()
  @env.ui.info("Nugrant version #{Nugrant::VERSION}", :prefix => false)
end