Sublime Forum

Exec command arguments for menu entry

#1

Can I specify working directory for sublime-menu’s?

“working_dir” does not seem to work with exec command, can I use this for a menu entry?

i tried

{
    "caption": "testCommand",
    "command": "exec",
    "args": 
    {
        "cmd": "${packages}/test"
    }
}

and

{
    "caption": "testCommand",
    "working_dir": "${packages}",
    "command": "exec",
    "args": 
    {
        "cmd": "./test"
    }
}

it only works when the file is relative to the current file, i.e., it behaves as if “working_dir” was ignored.

thank you

0 Likes

#2

did you try setting working_dir inside args?

2 Likes

#3

@kingkeith,
thanks for the hint that helped. However:

"args": 
{
  "working_dir": "${packages}/",
  "cmd": ["./test"]
}

the above doesn’t work but below does…

"args": 
{
  "working_dir": "/home/user/.config/sublime-text-3/Packages/",
  "cmd": ["./test"]
}
0 Likes

#4

so probably Packages/Default/exec.py isn’t expanding variables in working_dir

1 Like

#5

It works for working_dir in sublime-build, not sure if sublime-menu is different.

0 Likes

#6

The root cause of your issue is that the exec command doesn’t perform any variable expansions at all; you can use View Package File to view how it’s implemented, for example. The only expansion it does is to the env argument, and even then it uses os.path.expandvars() which isn’t Sublime specific.

It’s the build command that uses exec as part of a build system that is expanding variables in the arguments of the command, which it does before it invokes exec at all; that allows you to provide a custom build target without having to do your own variable expansion.

As such if you want to invoke exec from a menu and expand arguments, I think you need to implement your own custom wrapper on exec that does that for you.

For example:

import sublime
import sublime_plugin


class MenuExecCommand(sublime_plugin.WindowCommand):
    def run(self, **kwargs):
        var_list = self.window.extract_variables()
        args = sublime.expand_variables(kwargs, var_list)
        self.window.run_command("exec", args)
2 Likes

#7

ok, I’ll try this. Thanks, @OdatNurd :thumbsup:

0 Likes