Class: ShellOpts::Grammar::FileArgument

Inherits:
ArgumentType show all
Defined in:
lib/shellopts/argument_type.rb

Instance Attribute Summary collapse

Attributes inherited from ArgumentType

#message

Instance Method Summary collapse

Methods inherited from ArgumentType

#name, #to_s

Constructor Details

#initialize(kind) ⇒ FileArgument



63
64
65
66
# File 'lib/shellopts/argument_type.rb', line 63

def initialize(kind)
  constrain kind, :file, :dir, :path, :efile, :edir, :epath, :nfile, :ndir, :npath, :ifile, :ofile
  @kind = kind 
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



61
62
63
# File 'lib/shellopts/argument_type.rb', line 61

def kind
  @kind
end

Instance Method Details

#convert(value) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/shellopts/argument_type.rb', line 116

def convert(value)
  if value == "-"
    case kind
      when :ifile; "/dev/stdin"
      when :ofile; "/dev/stdout"
    else
      value
    end
  else
    value
  end
end

#match?(name, literal) ⇒ Boolean



68
69
70
71
72
73
74
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
# File 'lib/shellopts/argument_type.rb', line 68

def match?(name, literal)
  # Special-case '-' keyword
  if literal == '-' && [:ifile, :ofile].include?(kind)
    true

  # Special-case standard I/O files
  elsif %w(/dev/stdin /dev/stdout /dev/stderr /dev/null).include?(literal)
    case kind
      when :file, :path, :efile, :epath, :nfile, :npath
        true
      when :ifile
        %w(/dev/stdin /dev/null).include? literal or 
            set_message "Can't read #{literal}"
      when :ofile
        %w(/dev/stdout /dev/stderr /dev/null).include?(literal) or 
            set_message "Can't write to #{literal}"
      when :dir, :edir, :ndir
        set_message "Expected file, got directory #{literal}"
    else
      raise ArgumentError, "Unhandled kind: #{kind.inspect}"
    end

  # All other files or directories
  else
    case kind # TODO: node, enode, npath - special files: character, block, socket, etc.
      when :file; match_path(name, literal, kind, :file?, :default)
      when :dir; match_path(name, literal, kind, :directory?, :default)
      when :path; match_path(name, literal, kind, :exist?, :default)

      when :efile; match_path(name, literal, kind, :file?, :exist)
      when :edir; match_path(name, literal, kind, :directory?, :exist)
      when :epath; match_path(name, literal, kind, :exist?, :exist)

      when :nfile; match_path(name, literal, kind, :file?, :new)
      when :ndir; match_path(name, literal, kind, :directory?, :new)
      when :npath; match_path(name, literal, kind, :exist?, :new)

      when :ifile; match_path(name, literal, kind, :readable?, :default)
      when :ofile; match_path(name, literal, kind, :writable?, :default)
    else
      raise InternalError, "Illegal kind: #{kind.inspect}"
    end
  end
end

#value?(value) ⇒ Boolean

Note: No checks done, not sure if it is a feature or a bug



114
# File 'lib/shellopts/argument_type.rb', line 114

def value?(value) value.is_a?(String) end