Skip to content

Accepting commands in the command window

Pavel Anisko edited this page Mar 23, 2018 · 1 revision

Concepts

In the command window plug-in can be called to handle the command entered by the user. Calling plug-in always starts by a word plugin followed by a plug-in name (or a short name) and optionally by its parameters:

plugin <plugin_name> [<parameters>];

For instance:

plugin demo;
plugin demo command 123;
plugin dm command;

In order to create a plug-in which accepts commands in the command window, you need:

  • Export CommandLine — it's mandatory. This function is called whenever your plug-in is called from the command window.
  • Register IDE_CommandFeedback callback — it gives your plug-in ability to print output (give feedback) to the command window.
  • Export PlugInName — the long name of the command, which user must enter to call your plug-in.
  • Export PlugInShortName — the short name of the command for quick typing, which user must enter to call your plug-in.

PlugInName and PlugInShortName are optional for export. If you don't export these, then DLL file name must be typed as a command, which is not convenient.

This code sample (shortened for thesake of simplicity) from the demo plug-in demonstrates, what is explained in this article:

namespace DemoCommandLinePluginNet
{

    delegate void IdeCommandFeedback(int feedbackHandle, string text);

    public class DemoCommandPluginNet
    {
        private static DemoCommandPluginNet me;

        private const int COMMAND_FEEDBACK_CALLBACK = 180;
        private static IdeCommandFeedback commandFeedbackCallback;

        [DllExport("CommandLine", CallingConvention = CallingConvention.Cdecl)]
        public static void CommandLine(int feedbackHandle, string command, string parameters)
        {
            string output = "Hello world!\n";

            if (command != null)
            {
                output += "command=" + command + "\n";
            }

            if (parameters != null)
            {
                output += "parameters=" + parameters + "\n";
            }

            me.PrintOutput(feedbackHandle, output);
        }

        [DllExport("PlugInShortName", CallingConvention = CallingConvention.Cdecl)]
        public static string PluginShortName()
        {
            return "dm";
        }

        [DllExport("PlugInName", CallingConvention = CallingConvention.Cdecl)]
        public static string PluginName()
        {
            return "demo";
        }

        private void PrintOutput(int feedbackHandle, string text)
        {
            commandFeedbackCallback(feedbackHandle, text);
        }
    }

}

Usage notes

When the user types command for your plug-in in the command window, CommandLine function is called.

[DllExport("CommandLine", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static void CommandLine(int feedbackHandle, string command, string parameters)

It receives 3 arguments:

  1. feedbackHandle — this is id of the command window, where the feedback from your plug-in will be returned. You'll use the value of feedbackHandle later when calling IDE_CommandFeedback callback.
  2. command — the first word after a plug-in name. This is treated as command name your plug-in must execute. You define these names. You always receive it in UPPER case, even if you typed in lower. To type command name with space uses quotes, for instance, 'with space' or "with space".
  3. parameters — your command parameters. In other words it is everything hat's left on the command line.

Thus when the user types:

plugin demo text text2 text3;

CommandLine receives:

  • feedbackHandle = some integer value.
  • command = "TEXT".
  • parameters = "text2 text3".

You can try this with the demo plug-in.

Clone this wiki locally