Sublime Forum

It is possible to pass an array to a command without **kargs?

#1

Currently I am passing arguments like:

class ExampleCommand( sublime_plugin.TextCommand ):

    def run( self, edit ):
        view = self.view
        old_selections = []

        for selection in view.sel():
            old_selections.append( selection.end() )

        window.run_command( "assistant", { "args" : old_selections } )


class AssistantCommand( sublime_plugin.TextCommand ):

    def run( self, edit, **kargs ):
        old_selections = kargs["args"]
        print( str( old_selections ) )

But I think it would be better for performance directly receiving it, then I tried this:

class AssistantCommand( sublime_plugin.TextCommand ):

    def run( self, edit, *old_selections ):
        print( str( old_selections ) )

But throws the error:

Traceback (most recent call last):
  File "D:\User\Dropbox\Applications\SoftwareVersioning\SublimeText\sublime_plugin.py", line 812, in run_
    return self.run(edit, **args)
TypeError: run() got an unexpected keyword argument 'args'
0 Likes

Complete whole word
#2

You can just pass it as any other argument. There is no noticable performance difference, but I would argue that it is better readable (and the next version of PackageDev will provide a better auto-completion).

class ExampleCommand( sublime_plugin.TextCommand ):
    def run( self, edit ):
        view = self.view
        old_selections = []

        for selection in view.sel():
            old_selections.append( selection.end() )

        window.run_command( "assistant", { "old_selections" : old_selections } )
class AssistantCommand( sublime_plugin.TextCommand ):
    def run( self, edit, old_selections ):
        print( str( old_selections ) )
2 Likes