Method: ArgParser::Definition#collapse

Defined in:
lib/arg-parser/definition.rb

#collapse(cmd_inst) ⇒ Definition

Collapses an ArgumentScope into this Definition, representing the collapsed argument possibilities once a command has been identitfied for a CommandArgument. Think of the original Definition as being a superposition of possible argument definitions, with one possible state for each CommandInstance of each commad. Once the actual CommandInstance is known, we are collapsing the superposition of possible definitions to a lower dimensionality; only one possible definition remains once all CommandArgument objects are replaced by CommandInstances.

Parameters:

  • cmd_inst (CommandInstance)

    The instance of a command that has been specified.

Returns:

  • (Definition)

    A new Definition with a set of arguments combined from this Definition and the selected ArgumentScope for a specific command instance.



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/arg-parser/definition.rb', line 381

def collapse(cmd_inst)
    new_def = self.clone
    child = cmd_inst.argument_scope
    new_args = {}
    new_short_keys = {}
    @arguments.each do |key, arg|
        if arg == cmd_inst.command_arg
            new_args[key] = cmd_inst
            child.walk_arguments do |key, arg|
                new_args[key] = arg
                new_short_keys[arg.short_key] = arg if arg.short_key
            end
        else
            new_args[key] = arg
            new_short_keys[arg.short_key] = arg if arg.short_key
        end
    end
    new_children = @children.reject{ |c| c == cmd_inst.argument_scope } &
        child.instance_variable_get(:@children)
    new_def.instance_variable_set(:@arguments, new_args)
    new_def.instance_variable_set(:@short_keys, new_short_keys)
    new_def.instance_variable_set(:@children, new_children)
    new_def
end