Command-Line Interfaces (CLI)

We commonly associate Command-Line Interfaces (CLI) with system engineers i.e. people who deal with server-related tasks.

A lot of people have this misconception that CLIs are used with remote servers because they don’t have GUI; however that is not the case. Most power users use CLIs to automate tons of things on their personal computers. Although there are plenty of GUIs available to manage remote servers with mouse clicks, CLI is way more popular for many reasons across the spectrum such as:

  1. CLI allows you to connect different unrelated processes (thanks to Unix pipes).
  2. You can create scripts for repeated tasks saving you a lot of time from typing them often.
  3. Creating CLI scripts takes less effort than more complex applications.

In the WordPress ecosystem, CLI has takers all along.

  1. Power WordPress users make use of WP-CLI.
  2. Backend WordPress engineers often develop custom WP-CLI commands to process.
  3. Frontend WordPress engineers use CLI to handle CSS preprocessors, web packs, etc.
  4. Our systems team maintains EasyEngine — a CLI project to automate server automation.

Using CLI

All major operating systems come with a CLI client. In some cases, you can override it.

  • Linux/Ubuntu – Default pre-installed terminal.
  • macOS comes with a pre-installed terminal with BASH. In Catalina, Apple made ZSH the default shell, however, macOS retained the (now deprecated) Bash. You can use Fish (Friendly interactive shell), a shell replacement for BASH (the standard Mac Terminal shell), which provides command coloring, tab completions, themes, and much more. Alternatively, you can also use the Zsh/Oh my Zsh shell replacement for BASH if you need it. We recommend checking this to get some great tips on using Zsh/Oh my Zsh.
  • Windows – Windows 11 comes up with its native command prompt. To open the Command Prompt, type cmd in the Start menu search bar and select the Best Match. Alternatively, press Windows key + R, type cmd into the Run utility, and press Enter to launch the Command Prompt. F.Y.I. Windows also comes with WSL2 which you can use as a replacement for the native command prompt.

WP-CLI

When working with WP CLI, commands can be added with WP_CLI::add_command(). e.g.,

/**
 * Example command.
 *
 * <message>
 * : A message to display
 *
 * --append=<message>
 * : A message to append to the original message.
 *
 * @when before_wp_load
 */
WP_CLI::add_command( 'command sub_command', function( $args, $assoc_args ) {
    WP_CLI::success( $args[0] . ' ' . $assoc_args['append'] );
} );

This is just a gist of WP_CLI that WordPress provides by default to manage websites and automate tasks. We will dive deeper into this tool in a separate course later in the sequence.

References