Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exit with code, stdout & stderr

Overview

Minimalistic command-line utility that terminates immediately after starting, while controlling the exit code, standard output (stdout) and standard error (stderr) messages, all in a single invocation.

Usage

exit-with [arg]... 

All arguments are optional. Each argument can be either a literal value, or a file name. If a file name is provided, the file’s contents is used as the argument value.

Note

Because of the specific usage of this utility application, the only outputs are: exit code, stdout message and stderr message. There are no additional options such as displaying version information, displaying help text, or handling invalid arguments with error messages.

How it works

The behavior of this application is defined by the following decision table:

123
i32(arg1)intruetruefalse
arg1in= 0!= 0-
codeout0arg10
stdoutoutarg2arg3…arg1
stderroutarg3…arg2arg2…

i32(arg1), arg1 are inputs, code, stdout, stderr are outputs. i32(x) is a function that returns true when x is a 32-bit signed integer.

There are three rules defined in columns numbered 1 to 3.

RULE 1

WHEN

  • the first argument arg1 provided to exit-with is a 32-bit signed integer

AND

  • the first argument arg1 is equal to zero

THEN

  • the returned exit code will be 0,
  • the second argument arg2 will be printed to stdout,
  • all arguments starting fromarg3... will be printed to stderr.

RULE 2

WHEN

  • the first argument arg1 provided to exit-with is a 32-bit signed integer

AND

  • the first argument arg1 is not equal to zero

THEN

  • the returned exit code will be the value of arg1,
  • all arguments starting fromarg3... will be printed to stdout (sic!),
  • the second argument arg2 will be printed to stderr (sic!).

RULE 3

WHEN

  • the first argument arg1 provided to exit-with is not a 32-bit signed integer

THEN

  • the returned exit code will be 0,
  • the first argument arg1 will be printed to stdout,
  • all arguments starting fromarg2... will be printed to stderr.

Tip

The destination where messages are printed out depends on the value of code.

Examples

No.argumentscodestdoutstderr
10
200
3hello0hello
40 hello0hello
50 hello world0helloworld
60 “” world0world
711
81 hello1hello
91 hello world1worldhello
101 “” world1world