Module: Lightning::Commands

Extended by:
Commands, CommandsUtil
Included in:
Commands
Defined in:
lib/lightning/commands.rb,
lib/lightning/commands/bolt.rb,
lib/lightning/commands/core.rb,
lib/lightning/commands/function.rb,
lib/lightning/commands/shell_command.rb

Overview

Runs lightning commands which are methods in this namespace.

Command Basics

To get a list of commands and their description: lightning -h. To get usage and description on a command lightning COMMAND -h i.e lightning bolt -h. Any command and subcommand can be abbreviated. For example, lightning b c gem path1 is short for lightning bolt create gem path1.

Command Plugins

Command plugins are a way for users to define their own lightning commands. A command plugin is a .rb file in ~/.lightning/commands/. Each plugin can have multiple commands since a command is just a method in Lightning::Commands.

A sample command plugin looks like this:

module Lightning::Commands
  desc 'COMMAND', 'Prints hello'
  def hello(argv)
    puts "Hello with #{argv.size} arguments"
  end
end

To register a command, desc must be placed before a method, describing the command’s usage and description. Note that a command receives commandline arguments as an array. See CommandsUtil for helper methods to be used inside a command.

For command plugin examples read the source.

Instance Method Summary collapse

Methods included from CommandsUtil

call_subcommand, command_has_required_args, config, if_bolt_found, parse_args, print_sorted_hash, save_and_say

Instance Method Details

#command_usageString

Returns Command usage for current command.

Returns:

  • (String)

    Command usage for current command



66
67
68
# File 'lib/lightning/commands.rb', line 66

def command_usage
  "Usage: lightning #{@command} #{desc_array[0]}"
end

#commandsArray

Returns Available lightning commands.

Returns:

  • (Array)

    Available lightning commands



49
50
51
# File 'lib/lightning/commands.rb', line 49

def commands
  @desc.keys
end

#desc(*args) ⇒ Object

Place before a command method to set its usage and description



71
72
73
# File 'lib/lightning/commands.rb', line 71

def desc(*args)
  @next_desc = args
end

#run(argv = ARGV) ⇒ Object

Called by ‘lightning` to call proper lightning command, print help or print version



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lightning/commands.rb', line 34

def run(argv=ARGV)
  if (command = argv.shift) && (actual_command = unalias_command(command))
    run_command(actual_command, argv)
  elsif command && (commands << 'source_file').include?(command)
    run_command(command, argv)
  elsif %w{-v --version}.include?(command)
    puts "lightning #{VERSION}"
  else
    load_user_commands
    puts "Command '#{command}' not found.","\n" if command && !%w{-h --help}.include?(command)
    print_help
  end
end

#run_command(command, args) ⇒ Object

Calls proper lightning command with remaining commandline arguments



54
55
56
57
58
59
60
61
62
63
# File 'lib/lightning/commands.rb', line 54

def run_command(command, args)
  @command = command.to_s
  if %w{-h --help}.include?(args[0])
    print_command_help
  else
    send(command, args)
  end
rescue StandardError
  $stderr.puts "Error: "+ $!.message
end

#source_file(argv) ⇒ Object

Silent command used by ‘lightning-reload` which prints Builder’s shell file



4
5
6
# File 'lib/lightning/commands/core.rb', line 4

def source_file(argv)
  puts config.source_file
end