Class: Avalanche::CLI::Application
- Inherits:
-
Object
- Object
- Avalanche::CLI::Application
- Defined in:
- lib/avalanche/cli/application.rb
Instance Method Summary collapse
- #command(usage, description, options, handler) ⇒ Object
-
#initialize(name, version) ⇒ Application
constructor
A new instance of Application.
- #run(argv) ⇒ Object
- #to_proc ⇒ Object
Constructor Details
#initialize(name, version) ⇒ Application
Returns a new instance of Application.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/avalanche/cli/application.rb', line 4 def initialize(name, version) @name = name @version = version @commands = {} @opts = OptionParser.new @opts.program_name = @name @opts. = "Usage: #{@name} [--version] [--help] command [options] [args]" @opts.top.append("", nil, nil) @opts.top.append("Available #{@name} commands are:", nil, nil) @opts.base.append("", nil, nil) @opts.base.append("Common options:", nil, nil) @opts.on_tail("-h", "--help", "Show this message") { print_help } @opts.on_tail('-v', '--version', "Show version") { print_version } end |
Instance Method Details
#command(usage, description, options, handler) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/avalanche/cli/application.rb', line 24 def command(usage, description, , handler) parts = usage.split(" ") command = parts.shift @opts.separator(sprintf(" %-10.10s %s", command, description)) parts.unshift("#{@name}-#{command}") @commands[command] = Command.new(parts.join(" "), , handler) end |
#run(argv) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/avalanche/cli/application.rb', line 35 def run(argv) if argv.empty? print_help end command = @commands[argv.first] if command.nil? @opts.permute!(argv) print_help else argv.shift command.run(argv) end rescue OptionParser::InvalidOption => e @opts.warn(e.) print_help rescue Interrupt exit 0 rescue => e @opts.abort("#{e.class.name}: #{e.}") end |
#to_proc ⇒ Object
58 59 60 |
# File 'lib/avalanche/cli/application.rb', line 58 def to_proc proc { |opts, args| run(args) } end |