Command Line Interface

This section describes how to write macros using the command line interface.

Command Line Arguments

When a command is invoked, the expression arg(1) contains the rest of the command line after the name with leading spaces removed. Alternatively, the command can declare a named argument whose value is the same as arg(1). For example, invoking the edit command e file1 file2 calls the e command with file1 file2 in arg(1). The parse built-in is an excellent function for parsing a command line string. When another macro calls a command, more than one argument string can be passed. Calling the arg function with no parameters returns the number of parameters with which the command or procedure was called.

Example:

#include "slick.sh"
// This command supports completion on a filename followed by an
// environment variable argument.
_command test1() name_info(FILE_ARG","ENV_ARG)
{
    parse arg(1) with file_name env_name;
    message("file_name="file_name" env_name="env_name);
}

The string constant expression given to the name_info keyword is used for argument completion, restricting when the command can be executed, and a few other options.

get_string Procedure

The get_string procedure reads a single argument from the user.

Example:

#include "slick.sh"
_command test2()
{
    if (get_string(file_name,"Filename: ",FILE_ARG";Help message")) {
       return(1); // Cancel key pressed.
    }
    if (get_string(env_name,"Environment variable name: ",
                ENV_ARG";Help message","PATH") ) {
       return(1); // Cancel key pressed.
    }
    message("file_name="file_name" env_name="env_name);
}

Single Argument Prompting with Support for Prompt Style

Use the prompt procedure to write a command that accepts one command line argument, or prompts for the argument if it is not given. If the user presses Esc while being prompted for the argument, file execution does not continue.

Example:

// This command supports completion on an environment variable argument.
 
#include "slick.sh"
_command test3() name_info(ENV_ARG)
{
    // If the user selects to abort, the prompt procedure stops execution.
    env_name=prompt(arg(1),"Environment variable name: ");
    message("env_name="env_name);
}