Command-line interface

Command-line interface#

command-line interface

a means of interacting with software via commands. Also called command-line shell.

main arguments#

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
  for (size_t i = 0; i < argc; ++i)
    puts(argv[i]);
}
$ code-wi/main-arguments.exe geko --1 🦎
code-wi/main-arguments.exe
geko
--1
🦎
🤔 Question to ponder

Three arguments are provided to main-arguments.exe. But why do we see 4 lines that are printed?

Activity 33 (Basic CLI)

Create a program that accepts any number of words as arguments and prints them back as a single, space-separated string in reverse order (last argument printed first), but only if the verb is reverse. Otherwise it prints usage:

$ ./main reverse a b c
c b a
$ ./main a b c
Usage: ./main reverse STRING1 STRING2 ...

CLI design#

Here are good examples for CLI programs.

VS Code considerations#

You cannot use F5 directly, because our program’s runtime is now dependent on arguments. There are two solutions:

  1. Only build using CtrlShiftb. Then run manually on the command line:

    ./main e caesar LEVE
    
  2. You change launch.json to cater for command-line arguments, e.g.,

    {
    "version": "0.2.0",
    "configurations": [
      {
        // ...
        "program": "${workspaceFolder}/main",
        "args": [
          "${input:verb}",
          "${input:cipher}",
          "${input:plaintext}"
        ],
        // ...
      }
    ],
    "inputs": [
      {
        "id": "verb",
        "type": "promptString",
        "description": "Enter verb"
      },
      // ...
     ]