Class: Xcodeproj::XCScheme::CommandLineArguments

Inherits:
XMLElementWrapper show all
Defined in:
lib/xcodeproj/scheme/command_line_arguments.rb

Overview

This class wraps the CommandLineArguments node of a .xcscheme XML file. This is just a container of CommandLineArgument objects. It can either appear on a LaunchAction or TestAction scheme group.

Instance Attribute Summary

Attributes inherited from XMLElementWrapper

#xml_element

Instance Method Summary collapse

Methods inherited from XMLElementWrapper

#to_s

Constructor Details

#initialize(node_or_arguments = nil) ⇒ CommandLineArguments

Returns a new instance of CommandLineArguments.

Parameters:

  • node_or_arguments (nil, REXML::Element, Array<CommandLineArgument>, Array<Hash{Symbol => String,Bool}>) (defaults to: nil)

    The 'CommandLineArguments' XML node, or list of command line arguments, that this object represents.

    - If nil, an empty 'CommandLineArguments' XML node will be created
    - If an REXML::Element, it must be named 'CommandLineArguments'
    - If an Array of objects or Hashes, they'll each be passed to {#assign_argument}
    

19
20
21
22
23
24
# File 'lib/xcodeproj/scheme/command_line_arguments.rb', line 19

def initialize(node_or_arguments = nil)
  create_xml_element_with_fallback(node_or_arguments, COMMAND_LINE_ARGS_NODE) do
    @all_arguments = []
    node_or_arguments.each { |var| assign_argument(var) } unless node_or_arguments.nil?
  end
end

Instance Method Details

#[](argument) ⇒ CommandLineArgument

Argument Returns the matching command line argument for a specified key

Parameters:

  • key (String)

    The key to lookup

Returns:

  • (CommandLineArgument)

    argument Returns the matching command line argument for a specified key


76
77
78
# File 'lib/xcodeproj/scheme/command_line_arguments.rb', line 76

def [](argument)
  all_arguments.find { |var| var.argument == argument }
end

#[]=(argument, enabled) ⇒ CommandLineArgument

Assigns a value for a specified key

Parameters:

  • key (String)

    The key to update in the command line arguments

  • value (String)

    The value to lookup

Returns:


89
90
91
92
# File 'lib/xcodeproj/scheme/command_line_arguments.rb', line 89

def []=(argument, enabled)
  assign_argument(:argument => argument, :enabled => enabled)
  self[argument]
end

#all_argumentsArray<CommandLineArgument>

Returns The key value pairs currently set in @xml_element.

Returns:


29
30
31
# File 'lib/xcodeproj/scheme/command_line_arguments.rb', line 29

def all_arguments
  @all_arguments ||= @xml_element.get_elements(COMMAND_LINE_ARG_NODE).map { |argument| CommandLineArgument.new(argument) }
end

#assign_argument(argument) ⇒ Array<CommandLineArgument>

Adds a given argument to the set of command line arguments, or replaces it if that key already exists

Parameters:

  • argument (CommandLineArgument, Hash{Symbol => String,Bool})

    The argument to add or update, backed by an CommandLineArgument node.

    - If an CommandLineArgument, the previous reference will still be valid
    - If a Hash, must conform to {CommandLineArgument#initialize} requirements
    

Returns:


42
43
44
45
46
47
48
49
50
51
# File 'lib/xcodeproj/scheme/command_line_arguments.rb', line 42

def assign_argument(argument)
  command_line_arg = if argument.is_a?(CommandLineArgument)
                       argument
                     else
                       CommandLineArgument.new(argument)
                     end
  all_arguments.each { |existing_var| remove_argument(existing_var) if existing_var.argument == command_line_arg.argument }
  @xml_element.add_element(command_line_arg.xml_element)
  @all_arguments << command_line_arg
end

#remove_argument(argument) ⇒ Array<CommandLineArgument>

Removes a specified argument (by string or object) from the set of command line arguments

Parameters:

Returns:


60
61
62
63
64
65
66
67
68
69
# File 'lib/xcodeproj/scheme/command_line_arguments.rb', line 60

def remove_argument(argument)
  command_line_arg = if argument.is_a?(CommandLineArgument)
                       argument
                     else
                       CommandLineArgument.new(argument)
                     end
  raise "Unexpected parameter type: #{command_line_arg.class}" unless command_line_arg.is_a?(CommandLineArgument)
  @xml_element.delete_element(command_line_arg.xml_element)
  @all_arguments -= [command_line_arg]
end

#to_aArray<Hash{Symbol => String,Bool}>

Returns The current command line arguments represented as an array.

Returns:

  • (Array<Hash{Symbol => String,Bool}>)

    The current command line arguments represented as an array


97
98
99
# File 'lib/xcodeproj/scheme/command_line_arguments.rb', line 97

def to_a
  all_arguments.map(&:to_h)
end