Class: Puppet::Util::CommandLine

Inherits:
Object
  • Object
show all
Includes:
Limits
Defined in:
lib/puppet/util/command_line.rb,
lib/puppet/util/command_line/trollop.rb,
lib/puppet/util/command_line/puppet_option_parser.rb

Overview

This is the main entry point for all puppet applications / faces; it is basically where the bootstrapping process / lifecycle of an app begins.

Defined Under Namespace

Modules: Trollop Classes: ApplicationSubcommand, ExternalSubcommand, NilSubcommand, PuppetOptionError, PuppetOptionParser, TrollopCommandlineError, UnknownSubcommand

Constant Summary collapse

OPTION_OR_MANIFEST_FILE =
/^-|\.pp$/

Instance Method Summary collapse

Methods included from Limits

#setpriority

Constructor Details

#initialize(zero = $0, argv = ARGV, stdin = STDIN) ⇒ CommandLine

Returns a new instance of CommandLine.

Parameters:

  • zero (String) (defaults to: $0)

    the name of the executable

  • argv (Array<String>) (defaults to: ARGV)

    the arguments passed on the command line

  • stdin (IO) (defaults to: STDIN)

    (unused)



32
33
34
35
# File 'lib/puppet/util/command_line.rb', line 32

def initialize(zero = $0, argv = ARGV, stdin = STDIN)
  @command = File.basename(zero, '.rb')
  @argv = argv
end

Instance Method Details

#argsArray<String>

Returns the command line arguments being passed to the subcommand.

Returns:

  • (Array<String>)

    the command line arguments being passed to the subcommand



51
52
53
54
55
56
57
58
59
# File 'lib/puppet/util/command_line.rb', line 51

def args
  return @argv if @command != 'puppet'

  if subcommand_name.nil?
    @argv
  else
    @argv[1..-1]
  end
end

#executevoid

This method returns an undefined value.

Run the puppet subcommand. If the subcommand is determined to be an external executable, this method will never return and the current process will be replaced via Kernel#exec.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/puppet/util/command_line.rb', line 66

def execute
  require_config = true
  if @argv.first =~ /help|-h|--help|-V|--version/
    require_config = false
  end
  Puppet::Util.exit_on_fail(_("Could not initialize global default settings")) do
    Puppet.initialize_settings(args, require_config)
  end

  setpriority(Puppet[:priority])

  find_subcommand.run
end

#external_subcommandObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
# File 'lib/puppet/util/command_line.rb', line 81

def external_subcommand
  Puppet::Util.which("puppet-#{subcommand_name}")
end

#subcommand_nameString

Returns name of the subcommand is being executed.

Returns:

  • (String)

    name of the subcommand is being executed



39
40
41
42
43
44
45
46
47
# File 'lib/puppet/util/command_line.rb', line 39

def subcommand_name
  return @command if @command != 'puppet'

  if @argv.first =~ OPTION_OR_MANIFEST_FILE
    nil
  else
    @argv.first
  end
end