Class: CommandLine::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/command_line/arguments.rb

Overview

Parse argument lists and return an argument object containing all set flags and switches.

Constant Summary collapse

FLAG_REGEXP =
/^--?[A-z0-9]/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = $*, &block) ⇒ Arguments

Initializer. arguments The arguments which are going to be parsed (defaults to $*).



22
23
24
25
26
# File 'lib/command_line/arguments.rb', line 22

def initialize(arguments = $*, &block)
  @arguments = arguments
  @flag_definitions = {}
  @begins_with_command = false
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



16
17
18
# File 'lib/command_line/arguments.rb', line 16

def command
  @command
end

#filesObject (readonly)

Returns the value of attribute files.



15
16
17
# File 'lib/command_line/arguments.rb', line 15

def files
  @files
end

#flag_definitionsObject (readonly)

Returns the value of attribute flag_definitions.



12
13
14
# File 'lib/command_line/arguments.rb', line 12

def flag_definitions
  @flag_definitions
end

#flagsObject (readonly)

Returns the value of attribute flags.



14
15
16
# File 'lib/command_line/arguments.rb', line 14

def flags
  @flags
end

#required_filesObject

Returns the value of attribute required_files.



18
19
20
# File 'lib/command_line/arguments.rb', line 18

def required_files
  @required_files
end

Class Method Details

.parse(arguments = $*) {|cla| ... } ⇒ Object

Parse a list of arguments. Intatiates a Argument object with the given arguments and yeilds it so that flags and switches can be set by the application. arguments The arguments which are going to be parsed (defaults to $*). Returns the arguments object.parse!

Yields:

  • (cla)


32
33
34
35
36
# File 'lib/command_line/arguments.rb', line 32

def self.parse(arguments = $*, &block)
  cla = Arguments.new(arguments)
  yield(cla)
  return cla.parse!
end

Instance Method Details

#[](name) ⇒ Object



69
70
71
# File 'lib/command_line/arguments.rb', line 69

def [](name)
  return flags[name.to_s.gsub(/_/, '-').to_sym]
end

#begins_with_command!(begins_w_command = true) ⇒ Object

If called argument list must begin with a command. begins_w_command Defaults to true.



59
60
61
# File 'lib/command_line/arguments.rb', line 59

def begins_with_command!(begins_w_command=true)
  @begins_with_command = begins_w_command
end

#check_parsed_arguments!Object

Check if the parsed arguments meet their requirements. Raises CommandLineexception on error.



117
118
119
120
121
122
123
124
125
126
# File 'lib/command_line/arguments.rb', line 117

def check_parsed_arguments!
  if @begins_with_command && @command.nil? 
    raise CommandLine::CommandMissing.new
  end

  if @required_files && @files.length < @required_files
    raise CommandLine::FileMissing.new("You need at least #{@required_files} files")
  end

end

#flag(flag, options) ⇒ Object

Handle argument flags for the application flag A flag symbol like :fast Options

  • :expects Expects a value after the flag



49
50
51
52
53
54
55
# File 'lib/command_line/arguments.rb', line 49

def flag(flag, options)
  options[:expects] = String unless options.has_key?(:expects)
  argument = Flag.new(flag, options)
  @flag_definitions[argument.to_argument]  = argument
  @flag_definitions[argument.to_alias]     = argument if argument.has_alias?
  return argument
end

#ignore_unknown_flags!(ignore = true) ⇒ Object

Unknown flags will be silently ignored. ignore Defaults to true.



65
66
67
# File 'lib/command_line/arguments.rb', line 65

def ignore_unknown_flags!(ignore = true)
  @ignore_unknown_flags = ignore
end

#parse!Object

Parse the flags and switches set by the application. Returns an arguments object containing the flags and switches found in the commandline.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/command_line/arguments.rb', line 75

def parse!
  @flags = {}
  @files = []
  
  i = 0
  while @arguments.length > i do
    arg = @arguments[i]
    if FLAG_REGEXP =~ arg
      if @flag_definitions.has_key?(arg)
        flag = @flag_definitions[arg]
        if flag.expects_argument?
          
          if @arguments.length > (i + 1) && @arguments[i + 1]
            @flags[flag.name] = @arguments[i + 1]
            i += 1
          else
            raise CommandLine::FlagExpectsArgument.new(arg)
          end              
          
        else
          @flags[flag.name] = true
        end
      else
        raise CommandLine::UnknownFlag.new(arg) unless @ignore_unknown_flags
      end
    else
      if @begins_with_command && @command.nil?
        @command = arg
      else
        @files << arg
      end
    end
    i += 1        
  end
  
  check_parsed_arguments!
  
  return self
end

#switch(switch, switch_alias = nil) ⇒ Object

Handle argument switches for the application switch A switch symbol like :fast switch_alias An short alias for the same switch (:f).



41
42
43
# File 'lib/command_line/arguments.rb', line 41

def switch(switch, switch_alias = nil)
  return self.flag(switch, :alias => switch_alias, :expects => nil)
end