Class: Git::Commands::Arguments::Bound Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Bound arguments object returned by #bind

Provides accessor methods for all defined options and positional arguments, with automatic normalization of aliases to their canonical names.

For every flag_option, both a plain accessor (e.g. bound.force) and a ?-suffixed predicate alias (e.g. bound.force?) are generated, following Ruby convention for boolean predicates. Plain accessors are kept for backward compatibility. value_option fields only receive plain accessors.

Reserved-name exception: if the ?-suffixed name conflicts with a name in RESERVED_NAMES (e.g. nil?, frozen?), the predicate alias is not generated to avoid overriding built-in Object methods. Use hash-style access (bound[:nil]) when the flag name is reserved.

Examples:

Accessing bound arguments

args_def = Arguments.define do
  flag_option :force
  flag_option :remotes, as: ['-r', '--remotes']
  operand :branch_names, repeatable: true
end
bound = args_def.bind('branch1', 'branch2', force: true, remotes: true)
bound.force          # => true
bound.force?         # => true   # ? alias for flag_option
bound.remotes        # => true
bound.remotes?       # => true   # ? alias for flag_option
bound.branch_names   # => ['branch1', 'branch2']

Splatting for command execution

args_def = Arguments.define do
  flag_option :force
  operand :file
end
bound = args_def.bind('test.txt', force: true)
bound.to_a  # => ['--force', 'test.txt']

Hash-style access for reserved names

args_def = Arguments.define do
  value_option :hash
end
bound = args_def.bind(hash: 'abc123')
bound[:hash]  # => 'abc123'

Constant Summary collapse

RESERVED_NAMES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Names that cannot have accessor methods defined (would override Object methods)

(Object.instance_methods + [:to_ary]).freeze
EMPTY_EXECUTION_OPTIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Canonical frozen empty hash returned by #execution_options when no non-nil execution options are present.

Returns:

{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args_array, options, positionals, execution_option_names = [], flag_names = []) ⇒ Bound

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Bound.

Parameters:

  • args_array (Array<String>)

    the CLI argument array (frozen)

  • options (Hash{Symbol => Object})

    normalized options hash (frozen)

  • positionals (Hash{Symbol => Object})

    positional arguments hash (frozen)

  • execution_option_names (Array<Symbol>) (defaults to: [])
  • flag_names (Array<Symbol>) (defaults to: [])

    option names declared via Git::Commands::Arguments#flag_option



3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
# File 'lib/git/commands/arguments.rb', line 3202

def initialize(args_array, options, positionals, execution_option_names = [], flag_names = [])
  @args_array = args_array.freeze
  @options = options.freeze
  @positionals = positionals.freeze
  @execution_options = build_execution_options(execution_option_names)

  # Define accessor methods (skip reserved names)
  @options.each_key { |name| define_accessor(name, @options) }
  @positionals.each_key { |name| define_accessor(name, @positionals) }
  define_flag_predicate_accessors(flag_names)

  freeze
end

Instance Attribute Details

#execution_optionsHash{Symbol => Object} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execution options and values for command execution.

Includes only options declared via Git::Commands::Arguments#execution_option and excludes options with nil values.

Returns:

  • (Hash{Symbol => Object})

    frozen hash of execution option values



3194
3195
3196
# File 'lib/git/commands/arguments.rb', line 3194

def execution_options
  @execution_options
end

Instance Method Details

#[](key) ⇒ Object?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Hash-style access to option and positional values

Use this for reserved names (like :hash, :class) that cannot have accessor methods defined.

Parameters:

  • key (Symbol)

    the option or positional name

Returns:

  • (Object, nil)

    the value, or nil if not found



3246
3247
3248
3249
3250
3251
# File 'lib/git/commands/arguments.rb', line 3246

def [](key)
  return @options[key] if @options.key?(key)
  return @positionals[key] if @positionals.key?(key)

  nil
end

#to_aArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the CLI arguments array for splatting

Ruby's splat operator in array literals uses to_a for expansion. This enables: ['git', 'branch', *bound_args].

Operands declared with skip_cli: true are intentionally excluded.

Returns:

  • (Array<String>)

    the CLI arguments



3235
3236
3237
# File 'lib/git/commands/arguments.rb', line 3235

def to_a
  @args_array
end

#to_aryArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the CLI arguments array for splatting

This enables direct splatting: command(*bound_args).

Operands declared with skip_cli: true are intentionally excluded.

Returns:

  • (Array<String>)

    the CLI arguments



3223
3224
3225
# File 'lib/git/commands/arguments.rb', line 3223

def to_ary
  @args_array
end