Sublime Forum

How to call a Command with args

#1

hi

in my plugin, i call show_quick_panel , to show a C-Style Include paths, and want to click some path link (with a href=subl:command {args,value } ), to open some directory,

sublime.active_window().show_quick_panel(menu_items, on_done, sublime.KEEP_OPEN_ON_FOCUS_LOST)

now, only a command works fine,
but with args, will not work. with The Error Message shows

"Unable to parse command: brentdir {'path': '/usr/include' }"

please, help.

 for x in res:
				path = os.path.dirname(x) 

				args = ("{'path': '%s' }") % (path)
				searchPath = ('Search Path: <a href=\"subl:brentdir %s\">%s</a>') % (args, path)

				print(searchPath)

				package = Read_Output("dpkg -S "+x,True)
				if "no path" in package :
					package = ""
				else:
					r = list( package.split() )
					if len(r):
						package = r[0].rstrip(":")

				quick_panel_item = sublime.QuickPanelItem(
					x, 
					[
						str(searchPath),
					],
					package,
					sublime.KIND_MARKUP,
				)
				menu_items.append(quick_panel_item)
0 Likes

#2

The Command Code

import sublime, sublime_plugin

import Brent.cmd_module_funcs		as bFuncs
import Brent.cmd_module_taglist		as bTaglist
import Brent.cmd_module_find_header as bHeaders

class BrentdirCommand(sublime_plugin.WindowCommand):
	def run(self, path):
		sublime.active_window().run_command(
			"open_dir", {"dir": path}
		)
0 Likes

#3

Command arguments use a JSON format. Single quotes are not valid JSON.

0 Likes

#4

I suggest using the format_command or even command_url to format a command instead of rolling your own.

0 Likes

#5

thank you,
when i use double quotes, will not show “HTML A tags content” in quick panel item list.

0 Likes

#6

That would be because you’re using double quotes without escaping them in HTML. As I said, use the command_url function as it properly formats and also escapes the command for use in HTML.

0 Likes

#7

hi,

format_command still not works, A tag will not show.

args = sublime.format_command("brentdir",'{"path":path }' )
print("Format-Command" + args)
0 Likes

#8

use this hard code,
args = sublime.format_command(“brentdir”,’{“path”:"/usr/bin/include" }’ )
print(“Format-Command” + args)

shows error in my image.

0 Likes

#9

For one the format_command function takes the arguments of the command, not some string. Secondly you still need to escape the " to put it inside HTML, which is what command_url does. Please see the documentation: https://www.sublimetext.com/docs/api_reference.html

0 Likes

#10

@ bschaaf

thank you .
fixed with sublime.command_url , with Right Command call

1 Like