Class: FFI::Libfuse::FuseArgs

Inherits:
Struct
  • Object
show all
Includes:
Accessors
Defined in:
lib/ffi/libfuse/fuse_args.rb

Overview

struct fuse_args

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Accessors

#ffi_attr_fill, #ffi_attr_reader_member, #ffi_attr_writer_member, #inspect, #to_h

Methods included from Accessors::ClassMethods

#attr_accessor, #attr_reader, #attr_writer, #ffi_attr_accessor, #ffi_attr_reader, #ffi_attr_reader_method, #ffi_attr_readers, #ffi_attr_writer, #ffi_attr_writer_method, #ffi_attr_writers, #ffi_bitflag_accessor, #ffi_bitflag_reader, #ffi_bitflag_writer, #ffi_public_attr_readers, #ffi_public_attr_writers

Instance Attribute Details

#argvArray<String> (readonly)

Returns list of args.

Returns:

  • (Array<String>)

    list of args



47
48
49
50
# File 'lib/ffi/libfuse/fuse_args.rb', line 47

ffi_attr_reader(:argv) do
  # noinspection RubyResolve
  self[:argv].get_array_of_pointer(0, argc).map(&:read_string)
end

Class Method Details

.create(*argv) ⇒ FuseArgs

Create a fuse_args struct from command line options

Examples:

FFI::Libfuse::FuseArgs.create($0,*ARGV)

Parameters:

  • argv (Array<String>)

    command line args

    first arg is expected to be program name and is ignored by fuse_opt_parse it is handled specially only in fuse_parse_cmdline (ie no subtype is given)

Returns:



26
27
28
29
# File 'lib/ffi/libfuse/fuse_args.rb', line 26

def self.create(*argv)
  argv.unshift('ffi-libfuse') if argv.empty? || argv[0].start_with?('-')
  new.fill(*argv)
end

Instance Method Details

#add(arg) ⇒ Object

Add an arg to this arg list

Parameters:

  • arg (String)


54
55
56
# File 'lib/ffi/libfuse/fuse_args.rb', line 54

def add(arg)
  Libfuse.fuse_opt_add_arg(self, arg)
end

#insert(pos, arg) ⇒ Object

Insert arg at pos in this struct via fuse_opt_insert_arg

Parameters:

  • pos (Integer)

    index to insert arg at

  • arg (String)


61
62
63
# File 'lib/ffi/libfuse/fuse_args.rb', line 61

def insert(pos, arg)
  Libfuse.fuse_opt_insert_arg(self, pos, arg)
end

#parse!(opts, data = nil, ignore: %i[non_option unmatched],) {|key:, value:, match:, data:, out:| ... } ⇒ self

Option parsing function

Wraps fuse_opt_parse() in ruby sugar and safety

Parameters:

  • opts (Hash<String,Symbol>)

    option schema

    hash keys are a String template to match arguments against

    1. "-x", "-foo", "--foo", "--foo-bar", etc. These match only themselves. Invalid values are "--" and anything beginning with "-o"
    2. "foo", "foo-bar", etc. These match "-ofoo", "-ofoo-bar" or the relevant option in a comma separated option list
    3. "bar=", "--foo=", etc. These are variations of 1) and 2) which have a parameter value
    4. '%' Formats Not Supported (or needed for Ruby!)
    5. "-x ", etc. Matches either "-xparam" or "-x param" as two separate arguments
    6. '%' Formats Not Supported

    hash values are the Symbol sent to the block for a matching argument

    • :keep Argument is not passed to block, but behave as if the block returns :keep
    • :discard Argument is not passed to block, but behave as if the block returns :discard
    • any other value is yielded as 'key' property on matching argument

    note that multiple templates can refer to the same key to support multiple option styles

  • data (Object) (defaults to: nil)

    an optional object that will be passed to the block

  • ignore (Array<Symbol>|nil) (defaults to: %i[non_option unmatched],)

    the keys in this list will be kept without being passed to the block. pass nil to observe all keys

Yields:

  • (key:, value:, match:, data:, out:)

    block is called for each remaining arg

Yield Parameters:

  • key (Symbol)

    determines why the processing function was called

    • :unmatched for arguments that do not match any supplied option
    • :non_option for non-option arguments (after -- or not beginning with -)
    • with appropriate value from opts hash for a matching argument
  • value (String|Boolean)

    the option value or true for option without a value

  • match (String)

    the matching template specification (ie a key in opts)

  • data (Object)
  • out (FuseArgs)

    can #add or #insert additional args as required eg. if one arg implies another

Yield Returns:

  • (Symbol)

    the argument action

    • :error an error, alternatively raise Error
    • :keep retain the current argument for further processing
    • :handled,:discard remove the current argument from further processing

Returns:

  • (self)

Raises:

  • Error if an error is raised during parsing



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ffi/libfuse/fuse_args.rb', line 114

def parse!(opts, data = nil, ignore: %i[non_option unmatched], &block)
  ignore ||= []

  # first create an array of unique symbols such that positive indexes are custom options and negative indexes are
  # special values (see fuse_opt.h), ie so we can turn the integer received in fuse_opt_proc back into a symbol
  symbols = opts.values.uniq + %i[discard keep non_option unmatched]

  # transform our symbol keys into integers suitable for FuseOpts
  int_opts = opts.transform_values do |v|
    %i[discard keep].include?(v) ? symbols.rindex(v) - symbols.size : symbols.index(v)
  end

  # keep track of opt templates by key so we extract parameter values from arg
  param_opts, bool_opts = opts.keys.partition { |t| t =~ /(\s+|=)$/ }.map do |opt_list|
    opt_list.group_by { |t| opts[t] }
  end

  fop = fuse_opt_proc(symbols, bool_opts, param_opts, ignore, &block)
  raise Error unless Libfuse.fuse_opt_parse(self, data, int_opts, fop).zero?

  self
end