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:

  • (Hash{Symbol => Object})

    frozen empty execution options hash

{}.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.

Initialize a new frozen Bound object with accessor methods for all defined arguments

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

Options Hash (options):

  • :"option_name" (Object)

    bound value for any registered option name



4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
# File 'lib/git/commands/arguments.rb', line 4037

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



4021
4022
4023
# File 'lib/git/commands/arguments.rb', line 4021

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



4082
4083
4084
4085
4086
4087
# File 'lib/git/commands/arguments.rb', line 4082

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



4070
4071
4072
# File 'lib/git/commands/arguments.rb', line 4070

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



4058
4059
4060
# File 'lib/git/commands/arguments.rb', line 4058

def to_ary
  @args_array
end