Class: Kommand::CLI

Inherits:
Object
  • Object
show all
Includes:
Scripts::Script
Defined in:
lib/kommand/cli.rb

Direct Known Subclasses

KLI

Class Method Summary collapse

Methods included from Scripts::Script

included

Class Method Details

.add_arguments(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kommand/cli.rb', line 34

def self.add_arguments(*args)
  keep = []
  args.each_with_index do |arg,a|
    if Commands::exists?(arg)
      begin
        @command = Commands::command(arg)
        @command.set_arguments(*args.slice(a+1,args.length-a))
      rescue Scripts::InvalidArgument => e
        puts e.message
        Commands::command(arg).usage
        exit 1
      end
      break
    end

    keep << arg
  end
  super(*keep)
end

.binaryObject



58
59
60
# File 'lib/kommand/cli.rb', line 58

def self.binary
  @binary ||= Kommand.kommand
end

.binary=(bin) ⇒ Object



54
55
56
# File 'lib/kommand/cli.rb', line 54

def self.binary=(bin)
  @binary = bin
end

.command_nameObject



62
63
64
# File 'lib/kommand/cli.rb', line 62

def self.command_name
  binary
end

.debug?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/kommand/cli.rb', line 22

def self.debug?
  user_arguments.map(&:key).include?("-d")
end

.exec(cmd) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/kommand/cli.rb', line 26

def self.exec(cmd)
  if verbose?
    system cmd
  else
    `#{cmd}`
  end
end

.inherited(sub) ⇒ Object



10
11
12
13
# File 'lib/kommand/cli.rb', line 10

def self.inherited(sub)
  sub.send :valid_argument, Scripts::Argument.new("-v, --verbose", :valid => [], :summary => "Output additional information from command executions to STDOUT")
  sub.send :valid_argument, Scripts::Argument.new("-d, --debug", :valid => [], :summary => "Output full backtraces for exceptions and errors")
end

.start(*args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/kommand/cli.rb', line 95

def self.start(*args)
  args = args[0] if args.length == 1 && args.first.is_a?(Array)
  begin
    set_arguments(*args)
  rescue Scripts::InvalidArgument => e
    puts e.message
    return
  rescue Exception => e
    usage
    return
  end

  if @command.nil?
    usage
    return
  end

  @command.run
end

.usageObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kommand/cli.rb', line 74

def self.usage
  puts "#{binary} v#{version}"
  puts
  puts "usage: #{binary} #{valid_arguments.to_s} <command> [<args>]"
  unless valid_arguments.empty?
    puts
    puts "Arguments:"
    puts valid_arguments.to_help
  end
  puts
  puts "The available #{binary} commands are:"
  
  Commands::commands.each do |cmd|
    print "   %-#{Commands::commands.sort { |a,b| a.command_name.length <=> b.command_name.length }.last.command_name.length + 2}s" % cmd.command_name
    puts "# #{cmd.command_summary}"
  end

  puts
  puts "See `#{binary} help <command>` for more information on a specific command."
end

.verbose?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/kommand/cli.rb', line 18

def self.verbose?
  user_arguments.map(&:key).include?("-v")
end

.versionObject



70
71
72
# File 'lib/kommand/cli.rb', line 70

def self.version
  @version ||= VERSION
end

.version=(ver) ⇒ Object



66
67
68
# File 'lib/kommand/cli.rb', line 66

def self.version=(ver)
  @version = ver
end