Module: Jenkins::CLI::Command::ClassMethods

Defined in:
lib/jenkins/cli/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#run_blockObject (readonly)

Returns the value of attribute run_block.



60
61
62
# File 'lib/jenkins/cli/command.rb', line 60

def run_block
  @run_block
end

#slop_blockObject (readonly)

Returns the value of attribute slop_block.



60
61
62
# File 'lib/jenkins/cli/command.rb', line 60

def slop_block
  @slop_block
end

Instance Method Details

#arguments { ... } ⇒ Object

Set up the block passed to Slop.parse. See the Slop README for information on the way this block works: github.com/injekt/slop/blob/master/README.md

Example: arguments { on :v, :verbose, “Be verbose” }

Yields:

  • a declaration of arguments and options of this command



80
81
82
# File 'lib/jenkins/cli/command.rb', line 80

def arguments(&slop_block)
  @slop_block = slop_block
end

#command_name(command_name = nil) ⇒ String

Get/set the name by which the command will be invoked.

This is what the user has to call the command as. The default value is the class name, with any ‘Command’ suffix removed, and the ‘CamelCase’ words separated using hypen, into ‘camel-case’. For example this turns ‘HelloWorldCommand` into `hello-world`.

To use a custom name, just invoke it with that name. E.g.

command_name "my-cooler-name"

Parameters:

  • the (String)

    name with which the command will be invoked

Returns:

  • (String)

    the command name



109
110
111
# File 'lib/jenkins/cli/command.rb', line 109

def command_name(command_name = nil)
  command_name ? @command_name = command_name : @command_name
end

#default_command_nameObject

We use this to initialize the default value of @command_name. You can override this using the above ‘command_name`.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/jenkins/cli/command.rb', line 115

def default_command_name
  command = name

  # Replace any 'CLICommand' or 'Command' suffix on class name.
  command = command.sub(/(CLI)?Command$/, '')

  # Then convert "FooBarZot" into "Foo-Bar-Zot"
  command = command.gsub(/([a-z0-9])([A-Z])/, '\1-\2')

  # Then lower-case it.
  command.downcase
end

#description(desc = nil) ⇒ String

Set (or get) the description shown by this command in the ‘help’ CLI command. Also used in the default implementation of Slop’s banner - meaning the my-command –help output.

Example: description “Cool command that’ll rock your world!”

Parameters:

  • description (String)

    the description of the command

Returns:

  • (String)

    the description of the command



69
70
71
# File 'lib/jenkins/cli/command.rb', line 69

def description(desc = nil)
  desc ? @description = desc : @description
end

#run { ... } ⇒ Object

Define the actual implementation of the command

This block specified with #run will be invoked in the scope of a fresh instance for each command invocation.

Example: run { puts “Hello world” }

Yields:

  • the command body



92
93
94
# File 'lib/jenkins/cli/command.rb', line 92

def run(&run_block)
  @run_block = run_block
end