module Commander::CLI
Public Instance Methods
alias_command(alias_name, name, *args)
click to toggle source
Alias command name with alias_name. Optionally args may be passed as if they were being passed straight to the original command via the command-line.
# File lib/commander/cli.rb, line 114 def alias_command(alias_name, name, *args) commands[alias_name.to_s] = command name aliases[alias_name.to_s] = args end
aliases()
click to toggle source
A hash of known aliases
# File lib/commander/cli.rb, line 106 def aliases @aliases ||= {} end
command(name) { |cmd| ... }
click to toggle source
Define and get a command by name
# File lib/commander/cli.rb, line 98 def command(name) name = name.to_s (commands[name] ||= Command.new(name)).tap do |cmd| yield cmd if block_given? end end
commands()
click to toggle source
Hash of Command
objects
# File lib/commander/cli.rb, line 81 def commands @commands ||= {} end
default_command(name = nil)
click to toggle source
Default command name to be used when no other command is found in the arguments.
# File lib/commander/cli.rb, line 73 def default_command(name = nil) @default_command = name unless name.nil? @default_command end
global_slop()
click to toggle source
Hash of Global Options
# File lib/commander/cli.rb, line 88 def global_slop @global_slop ||= Slop::Options.new.tap do |slop| slop.bool '-h', '--help', 'Display help documentation' slop.bool '--version', 'Display version information' end end
program(key, *args, &block)
click to toggle source
Assign program information.
Examples¶ ↑
# Set data program :name, 'Commander' program :version, Commander::VERSION program :description, 'Commander utility program.' program :help, 'Copyright', '2008 TJ Holowaychuk' program :help, 'Anything', 'You want' # Get data program :name # => 'Commander'
Keys¶ ↑
:version (required) Program version triple, ex: '0.0.1' :description (required) Program description :name Program name, defaults to basename of executable :config An optional argument to be passed into the action :help_formatter Defaults to Commander::HelpFormatter::Terminal :help Allows addition of arbitrary global help blocks :help_paging Flag for toggling help paging
# File lib/commander/cli.rb, line 47 def program(key, *args, &block) @program ||= { help_formatter: HelpFormatter::Terminal, name: File.basename($PROGRAM_NAME), help_paging: true, } if key == :help && !args.empty? @program[:help] ||= {} @program[:help][args.first] = args.at(1) elsif key == :help_formatter && !args.empty? @program[key] = (@help_formatter_aliases[args.first] || args.first) elsif block @program[key] = block else unless args.empty? @program[key] = args.count == 1 ? args[0] : args end @program[key] end end
run(*args)
click to toggle source
# File lib/commander/cli.rb, line 13 def run(*args) instance = Runner.new( @program, commands, default_command, global_slop, aliases, args ) instance.run end
run!(*args)
click to toggle source
Wrapper run command with error handling
# File lib/commander/cli.rb, line 7 def run!(*args) Commander::ErrorHandler.new(program(:name)).start do |handler| run(*handler.parse_trace(*args)) end end