Class: Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/aml/Argument.rb

Instance Method Summary collapse

Constructor Details

#initialize(argument = {}) ⇒ Argument

Returns a new instance of Argument.



2
3
4
5
6
7
8
# File 'lib/aml/Argument.rb', line 2

def initialize(argument={})
  @item = {}
  @item_key = {}
  argument.each do |argument|
    initialize_add(argument)
  end
end

Instance Method Details

#define(name, value, usage, required = false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aml/Argument.rb', line 26

def define(name,value,usage,required=false)
  regex = /\[(?<key>\w)\]/
  keys = []
  name = name.gsub(regex).each do |match|
    match = match.match(regex)[:key]
    keys << match
    match
  end
  @item[name.to_sym] = {
    :value => value,
    :usage => usage,
    :require => required
  }
  @item_key[keys.join.to_sym] = name.to_sym if keys.count > 0
end

#get(name) ⇒ Object



47
48
49
50
51
52
# File 'lib/aml/Argument.rb', line 47

def get(name)
  begin
    @item[_name(name)][:value].to_s
  rescue
  end
end

#has_requirements?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/aml/Argument.rb', line 67

def has_requirements?
  @item.select{|k,v| v[:require] == true and v[:value] == nil}.count == 0 ? true : false
end

#itemsObject



53
54
55
# File 'lib/aml/Argument.rb', line 53

def items
  @item
end

#parse(argument) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aml/Argument.rb', line 9

def parse(argument)
  arguments = argument.split('--')
  if arguments.count == 1
    arguments = []
    io = argument.split(' ')
    arguments << "build " + io[0] #input file
    if io.count == 2
      arguments << "path " + File.dirname(io[1]) #output path
      arguments << "name " + File.basename(io[1],'.*') #output name
      arguments << "extension " + File.extname(io[1])[1..-1] if io[1].split('.').count > 1 #output extension
    end
  end
  arguments.each do |argument|
    string = argument.split(' ')
    set(string[0], string[1..string.count].join(' ').to_s) if argument.strip.length > 0
  end
end

#set(name, value = nil) ⇒ Object



41
42
43
44
45
46
# File 'lib/aml/Argument.rb', line 41

def set(name, value=nil)
  begin
    @item[_name(name)][:value] = value.to_s
  rescue
  end
end

#show_helpObject



59
60
61
62
63
64
65
66
# File 'lib/aml/Argument.rb', line 59

def show_help
  pad = _pad(@item)
  @item.each do |name,data|
    value = data[:value].to_s
    value = nil.to_s if name == :help
    puts "--#{name.to_s.ljust(pad[:name]+1)}\t#{value.ljust(pad[:value]+1)}\t#{data[:usage].ljust(pad[:usage])}"
  end
end

#show_help?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/aml/Argument.rb', line 56

def show_help?
  @item.select{|k,v| k == :help and v[:value] != false}.count == 0 ? false : true
end

#show_requiredObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aml/Argument.rb', line 70

def show_required
  required = @item.select{|k,v| v[:require] == true and v[:value] == nil}
  if required.count == 1
    puts "Please define the --#{required.first.first} argument; #{required.first.last[:usage]}"
  else
    pad = _pad(required)
    required.each do |name,data|
      value = data[:value].to_s
      value = nil.to_s if name == :help
      puts "--#{name.to_s.ljust(pad[:name]+1)}\t#{value.ljust(pad[:value]+1)}\t#{data[:usage].ljust(pad[:usage])}"
    end
  end
end