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

Returns a new instance of FileArgument.



75
76
77
78
# File 'lib/shellopts/argument_type.rb', line 75

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



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/shellopts/argument_type.rb', line 127

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

Returns:

  • (Boolean)


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
114
115
116
117
118
119
120
121
122
# File 'lib/shellopts/argument_type.rb', line 80

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

  # Special-case standard I/O files. These files have read-write (rw)
  # filesystem permissions so they need to be handled individually
  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 "#{literal} is not a directory"
    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

#subjectObject

Used in error messages



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/shellopts/argument_type.rb', line 63

def subject # Used in error messages
  @subject ||=
      case kind
        when :file, :efile; "regular file"
        when :nfile, :ifile, :ofile; "file"
        when :dir, :edir, :ndir; "directory"
        when :path, :epath, :npath; "path"
      else
        raise ArgumentError
      end
end

#value?(value) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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