Module: Lightning::CommandsUtil
- Included in:
- Commands
- Defined in:
- lib/lightning/commands_util.rb
Overview
Utility methods to be used inside lightning commands.
Instance Method Summary collapse
-
#call_subcommand(argv, cmds) ⇒ Object
Handles abbreviated subcommands and printing errors for invalid subcommands.
-
#command_has_required_args(argv, required) ⇒ nil, true
Determines if command has required arguments.
-
#config ⇒ Object
Shortcut to Lightning.config.
-
#if_bolt_found(bolt) ⇒ Object
Yields a block for an existing bolt or prints an error message.
-
#parse_args(args, names = []) ⇒ Array<Array, Hash>
Parses arguments into non-option arguments and hash of options.
-
#print_sorted_hash(hash, indent = false) ⇒ Object
Prints a hash as a 2 column table sorted by keys.
-
#save_and_say(message) ⇒ Object
Saves config and prints message.
Instance Method Details
#call_subcommand(argv, cmds) ⇒ Object
Handles abbreviated subcommands and printing errors for invalid subcommands. Defaults to ‘list’ subcommand if none given.
28 29 30 31 32 33 |
# File 'lib/lightning/commands_util.rb', line 28 def call_subcommand(argv, cmds) subcommand = argv.shift || 'list' (subcmd = cmds.find {|e| e[/^#{subcommand}/]}) ? subcommand_has_required_args(subcmd, argv) && yield(subcmd, argv) : puts("Invalid subcommand '#{subcommand}'", command_usage) end |
#command_has_required_args(argv, required) ⇒ nil, true
Returns Determines if command has required arguments.
36 37 38 39 |
# File 'lib/lightning/commands_util.rb', line 36 def command_has_required_args(argv, required) return true if argv.size >= required puts "'lightning #{@command}' was called incorrectly.", command_usage end |
#config ⇒ Object
Shortcut to Lightning.config
59 |
# File 'lib/lightning/commands_util.rb', line 59 def config; Lightning.config; end |
#if_bolt_found(bolt) ⇒ Object
Yields a block for an existing bolt or prints an error message
5 6 7 8 |
# File 'lib/lightning/commands_util.rb', line 5 def if_bolt_found(bolt) bolt = config.unalias_bolt(bolt) config.bolts[bolt] ? yield(bolt) : puts("Can't find bolt '#{bolt}'") end |
#parse_args(args, names = []) ⇒ Array<Array, Hash>
Parses arguments into non-option arguments and hash of options. Options can have values with an equal sign i.e. ‘–option=value’. Options can be abbreviated with their first letter. Options without a value are set to true.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/lightning/commands_util.rb', line 46 def parse_args(args, names=[]) , args = args.partition {|e| e =~ /^-/ } = .inject({}) do |hash, flag| key, value = flag.split('=') name = key.sub(/^--?/,'') name = names.sort.find {|e| e[/^#{name}/] } || name hash[name.intern] = value.nil? ? true : value hash end [args, ] end |
#print_sorted_hash(hash, indent = false) ⇒ Object
Prints a hash as a 2 column table sorted by keys
11 12 13 14 15 16 17 18 |
# File 'lib/lightning/commands_util.rb', line 11 def print_sorted_hash(hash, indent=false) offset = hash.keys.map {|e| e.size }.max + 2 offset += 1 unless offset % 2 == 0 indent_char = indent ? ' ' : '' hash.sort.each do |k,v| puts "#{indent_char}#{k}" << ' ' * (offset - k.size) << (v || '') end end |
#save_and_say(message) ⇒ Object
Saves config and prints message
21 22 23 24 |
# File 'lib/lightning/commands_util.rb', line 21 def save_and_say() config.save puts end |