Class: GLI::Command

Inherits:
CommandLineToken show all
Defined in:
lib/gli/command.rb

Overview

A command to be run, in context of global flags and switches

Direct Known Subclasses

DefaultHelpCommand

Instance Attribute Summary

Attributes inherited from CommandLineToken

#aliases, #description, #name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names, description, arguments_name = nil) ⇒ Command

Create a new command

names

the name or names of this command (symbol or Array of symbols)

description

description of this command

arguments_name

description of the arguments, or nil if this command doesn’t take arguments



13
14
15
16
17
# File 'lib/gli/command.rb', line 13

def initialize(names,description,arguments_name=nil)
  super(names,description)
  @arguments_description = arguments_name || ''
  clear_nexts
end

Class Method Details

.name_as_string(name) ⇒ Object



57
58
59
# File 'lib/gli/command.rb', line 57

def self.name_as_string(name)
  name.to_s
end

Instance Method Details

#action(&block) ⇒ Object



53
54
55
# File 'lib/gli/command.rb', line 53

def action(&block)
  @action = block
end

#arg_name(name) ⇒ Object

describe the argument name of the next flag



36
# File 'lib/gli/command.rb', line 36

def arg_name(name); @next_arg_name = name; end

#clear_nextsObject



61
62
63
64
65
# File 'lib/gli/command.rb', line 61

def clear_nexts
  @next_desc = nil
  @next_arg_name = nil
  @next_default_value = nil
end

#default_value(val) ⇒ Object

set the default value of the next flag



38
# File 'lib/gli/command.rb', line 38

def default_value(val); @next_default_value = val; end

#desc(description) ⇒ Object

describe the next switch or flag



34
# File 'lib/gli/command.rb', line 34

def desc(description); @next_desc = description; end

#execute(global_options, options, arguments) ⇒ Object



67
68
69
# File 'lib/gli/command.rb', line 67

def execute(global_options,options,arguments)
  @action.call(global_options,options,arguments)
end

#flag(names) ⇒ Object



40
41
42
43
44
# File 'lib/gli/command.rb', line 40

def flag(names)
  flag = Flag.new(names,@next_desc,@next_arg_name,@next_default_value)
  flags[flag.name] = flag
  clear_nexts
end

#flagsObject



30
# File 'lib/gli/command.rb', line 30

def flags; @flags ||= {}; end

#namesObject



19
20
21
# File 'lib/gli/command.rb', line 19

def names
  all_forms
end

#switch(names) ⇒ Object

Create a switch



47
48
49
50
51
# File 'lib/gli/command.rb', line 47

def switch(names)
  switch = Switch.new(names,@next_desc)
  switches[switch.name] = switch
  clear_nexts
end

#switchesObject



31
# File 'lib/gli/command.rb', line 31

def switches; @switches ||= {}; end

#usageObject



23
24
25
26
27
28
# File 'lib/gli/command.rb', line 23

def usage
  usage = name.to_s
  usage += ' [options]' if !flags.empty? || !switches.empty?
  usage += ' ' + @arguments_description if @arguments_description
  usage
end