Module: Lightning::CommandsUtil
- Defined in:
- lib/lightning/commands_util.rb
Overview
Utility methods to be used inside lightning commands.
Instance Method Summary
- - (Object) call_subcommand(argv, cmds) Handles abbreviated subcommands and printing errors for invalid subcommands.
- - (nil, true) command_has_required_args(argv, required) Determines if command has required arguments.
- - (Object) config Shortcut to Lightning.config.
- - (Object) if_bolt_found(bolt) Yields a block for an existing bolt or prints an error message.
- - (Array<Array, Hash>) parse_args(args, names = []) Parses arguments into non-option arguments and hash of options.
- - (Object) print_sorted_hash(hash, indent = false) Prints a hash as a 2 column table sorted by keys.
- - (Object) save_and_say(message) Saves config and prints message.
Instance Method Details
- (Object) call_subcommand(argv, cmds)
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 |
- (nil, true) command_has_required_args(argv, required)
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 |
- (Object) config
Shortcut to Lightning.config
59 |
# File 'lib/lightning/commands_util.rb', line 59 def config; Lightning.config end |
- (Object) if_bolt_found(bolt)
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 |
- (Array<Array, Hash>) parse_args(args, names = [])
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 |
- (Object) print_sorted_hash(hash, indent = false)
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 |
- (Object) save_and_say(message)
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 |