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, InitConfig, RDocCommand

Instance Attribute Summary

Attributes inherited from CommandLineToken

#aliases, #description, #long_description, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandLineToken

#<=>

Constructor Details

#initialize(names, description, arguments_name = nil, long_desc = 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

long_desc

a longer description of the command, possibly with multiple lines and text formatting



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

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

Class Method Details

.name_as_string(name) ⇒ Object



60
61
62
# File 'lib/gli/command.rb', line 60

def self.name_as_string(name)
  name.to_s
end

Instance Method Details

#action(&block) ⇒ Object



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

def action(&block)
  @action = block
end

#arg_name(name) ⇒ Object

describe the argument name of the next flag



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

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

#arguments_descriptionObject



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

def arguments_description; @arguments_description; end

#clear_nextsObject



64
65
66
67
68
# File 'lib/gli/command.rb', line 64

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



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

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

#desc(description) ⇒ Object

describe the next switch or flag



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

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

#execute(global_options, options, arguments) ⇒ Object



70
71
72
# File 'lib/gli/command.rb', line 70

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

#flag(*names) ⇒ Object



43
44
45
46
47
# File 'lib/gli/command.rb', line 43

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

#flagsObject



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

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

#namesObject



22
23
24
# File 'lib/gli/command.rb', line 22

def names
  all_forms
end

#switch(*names) ⇒ Object

Create a switch



50
51
52
53
54
# File 'lib/gli/command.rb', line 50

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

#switchesObject



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

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

#usageObject



26
27
28
29
30
31
# File 'lib/gli/command.rb', line 26

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