Class: Incline::CLI::Usage

Inherits:
Object
  • Object
show all
Defined in:
lib/incline/cli/usage.rb

Overview

Defines the ‘usage’ command for the CLI.

Instance Method Summary collapse

Constructor Details

#initialize(command = nil) ⇒ Usage

Creates a new ‘usage’ command for the CLI.



11
12
13
# File 'lib/incline/cli/usage.rb', line 11

def initialize(command = nil)
  @command = command ? command.to_s.downcase.to_sym : nil
end

Instance Method Details

#runObject

Shows usage information for the application.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/incline/cli/usage.rb', line 17

def run
  msg = nil
  
  if @command
    command = Incline::CLI::valid_commands.find{|c| c[0] == @command}
    if command
      msg = "Usage: #{$PROGRAM_NAME} #{command[0]}"
      pend = ''
      command[2].each do |t,p|
        msg += ' '
        if t == :req
          msg += p.to_s
        elsif t == :opt
          msg += '[' + p.to_s
          pend += ']'
        else
          msg += '<...>'
        end
        msg += pend
      end
      msg = ANSI.ansi(:bold) { msg }
      msg += "\n"
      comment = get_run_comment(command[1])
      comment = "(No additional information)" if comment.to_s.strip == ''
      comment = '    ' + comment.gsub("\n", "\n    ")
      msg += comment + "\n"
    end
  end
  
  unless msg
    commands = Incline::CLI::valid_commands.sort{|a,b| a[0] <=> b[0]}
    
    msg = ANSI.ansi(:bold) { "Usage: #{$PROGRAM_NAME} command <arguments>" }
    if @command
      msg += "\nInvalid Command: #{@command}\n"
    end
    msg += "\nValid Commands:\n"
    commands.each do |(name,_,params)|
      msg += "  #{name}"
      pend = ''
      params.each do |t,p|
        msg += ' '
        if t == :req
          msg += p.to_s
        elsif t == :opt
          msg += '[' + p.to_s
          pend += ']'
        else
          msg += '<...>'
        end
      end
      msg += pend + "\n"
    end
    
  end
  

  STDOUT.print msg
  msg
end