Sublime Forum

Target option in build system

#1

A question for JPS: if I set the “target” option in a build system to my own custom command, will that command receive the other build options (e.g. env, etc.)? Does the signature of the custom command have to be the same as that of exec? Thanks!

1 Like

#2

In case it helps, here’s a custom command for the “target” element:

bitbucket.org/guillermooo/aaapa … v.py#cl-91

Basically, you should be able to access all args through **kwargs.

0 Likes

#3

Perfect, just what I needed! Thanks!

0 Likes

#4

…and, even better, any named function argument that corresponds to an option in the .sublime-build file gets passed automagically. Cool!

0 Likes

#5

The link is broken, may you fix it?

What are these **kwargs and how to use them?

0 Likes

#6

What are **kwargs and how do you use them?.

In the context of Sublime, you can use them to collect all of the command arguments that a command was given:

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    # Command that collects all command arguments that it's given
    def run(self, edit, **kwargs):
        # Get the settings by name, and use a default if the argument
        # was not given.
        position = kwargs.get ("position", 0)
        text = kwargs.get ("text", "Hello, World!")

        # Do the thing in the place
        self.view.insert(edit, position, text)

This command can be invoked with all combinations of the arguments text and position even though the command doesn’t explicitly indicate that it requires them. Thus the following are all valid key bindings for this command; I leave it as an exercise to the reader to determine what each of them will do.

{"keys": ["ctrl+shift+a"], "command": "example"},

{"keys": ["ctrl+shift+b"], "command": "example", 
    "args": {"text": "Sample"}},

{"keys": ["ctrl+shift+c"], "command": "example", 
    "args": {"position": 2}},
    
{"keys": ["ctrl+shift+d"], "command": "example", 
    "args": {"text": "Sample", "position": 2}},
1 Like

#7

Thanks @OdatNurd, how to paste code within colors on this forum?

Mime get not colored:

    import sublime, sublime_plugin

    class ExampleCommand(sublime_plugin.TextCommand):
        # Command that collects all command arguments that it's given
        def run(self, edit, **kwargs):
            # Get the settings by name, and use a default if the argument
            # was not given.
            position = kwargs.get ("position", 0)
            text = kwargs.get ("text", "Hello, World!")

            # Do the thing in the place
            self.view.insert(edit, position, text)
0 Likes

#8

You do it by entering it like the following (replace python with the syntax you want, e.g. json). I’m not sure what the full list of available syntaxes is. There may also be some other way to do this, but this is the one I use (note that using this style, you don’t do any initial indent):

```python
import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    # Command that collects all command arguments that it's given
    def run(self, edit, **kwargs):
        pass
```

which becomes:

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    # Command that collects all command arguments that it's given
    def run(self, edit, **kwargs):
        pass
3 Likes

#9

Thanks, this is like the GitHub markdown syntax.

0 Likes