Your first command-line tool

Jennifer

One small, useful program, built step by step: arguments, help, errors, exit codes, colour, logging, a spinner, and a command you can install.

November 2026, by @mplx.
Based on Jennifer 0.26.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 22

What we build: tidy

$ ls
Holiday Photo 01.JPG   IMG 2041 FINAL.png   myBeachDay.jpeg
$ ../tidy.j *
Holiday Photo 01.JPG -> holiday-photo-01.jpg
IMG 2041 FINAL.png -> img-2041-final.png
myBeachDay.jpeg -> my-beach-day.jpeg
$ ../tidy.j -c snake *
holiday-photo-01.jpg -> holiday_photo_01.jpg
...
2 / 22

A program sees its words: os.ARGS

use io;
use os;

for (def i in 0..len(os.ARGS)) {
    io.printf("%d: %s\n", $i, os.ARGS[$i]);
}
$ jennifer run argv.j -n "My Photo.JPG" --case=snake
0: argv.j
1: -n
2: My Photo.JPG
3: --case=snake
3 / 22

Describe the options, let args parse

import "args.j" as args;

func buildParser() {
    def p as args.Parser init args.parser("tidy", "rename files tidily");
    $p = args.flag($p, "case", "c", "kebab", "naming style");
    $p = args.choices($p, ["kebab", "snake", "camel", "pascal"]);
    $p = args.boolFlag($p, "dry-run", "n", "only show the new names");
    $p = args.countFlag($p, "verbose", "v", "log more (-v, -vv)");
    $p = args.positionalList1($p, "files", "the files to rename");
    return args.version($p, "tidy 1.0.0");
}
4 / 22

--help comes for free

$ ../tidy.j --help
Usage: tidy [options] files [files ...]
rename files tidily
Positional arguments:
  files    the files to rename
Options:
  -h, --help    show this help and exit
  -c, --case CASE    naming style (one of: kebab, snake, camel, pascal)
                     (default: kebab)
  -n, --dry-run    only show the new names
  -v, --verbose    log more (-v, -vv)
5 / 22

Reading the answers

if ($r.done) {
    io.printf("%s\n", $r.helpText);
    exit OK;
}
def style as string init args.asString($r, "case");
def dryRun as bool init args.asBool($r, "dry-run");
def verbose as int init args.count($r, "verbose");
def files as list of string init args.asList($r, "files");
6 / 22

Catching the user's mistake

def r as args.Result init args.Result{};
try {
    $r = args.parse(buildParser(), os.ARGS);
} catch (e) {
    if ($e.kind != "args") {
        throw $e;
    }
    io.eprintf("tidy: %s\n", $e.message);
    io.eprintf("try 'tidy --help'\n");
    exit USAGE;
}
7 / 22

When the user types it wrong

$ ../tidy.j
tidy: args: positional files needs at least one value
try 'tidy --help'
$ ../tidy.j --case shouty x.jpg
tidy: args: argument --case: 'shouty' is not one of kebab, snake, camel, pascal
try 'tidy --help'
$ ../tidy.j --colour *
tidy: args: unknown flag --colour
try 'tidy --help'
$ ../tidy.j -c
tidy: args: flag -c needs a value
try 'tidy --help'
8 / 22

Exit codes: the answer for scripts

CodeMeaning in tidyConstant
0everything workedOK
1some files could not be renamedFAILED
2wrong arguments, nothing was doneUSAGE
$ ../tidy.j gone.jpg 2>/dev/null; echo $?
1
$ ../tidy.j -c camel * >/dev/null && echo fine || echo "failed: $?"
failed: 1
9 / 22

stdout for results, stderr for people

use io;

io.printf("a result, for the next program\n");
io.eprintf("a message, for the human\n");
exit 3;
$ jennifer run streams.j > out.txt; echo $?
a message, for the human
3
$ cat out.txt
a result, for the next program
10 / 22

Borrowing code: two decks

$ jvc init tidy
created ./deck.toml for deck tidy
$ jvc add @skiffyard/texttransform "^0.5.0"
added @skiffyard/texttransform ^0.5.0 to requirements
$ jvc add @skiffyard/clispinner "^0.4.0"
added @skiffyard/clispinner ^0.4.0 to requirements
$ jvc install
installed 2 deck(s):
  ok    @skiffyard/clispinner 0.4.0 -> https://github.com/skiffyard/deck-clispinner.git
  ok    @skiffyard/texttransform 0.5.0 -> https://github.com/skiffyard/deck-texttransform.git
11 / 22

Case conversion with @skiffyard/texttransform

use io;
import "@skiffyard/texttransform/" as texttransform;

def name as string init "Holiday Photo 01";
for (def style in ["kebab", "snake", "camel", "pascal", "constant"]) {
    io.printf("%s|pad=9 %s\n", $style, texttransform.toCase($name, $style));
}
kebab     holiday-photo-01
snake     holiday_photo_01
camel     holidayPhoto01
pascal    HolidayPhoto01
constant  HOLIDAY_PHOTO_01
12 / 22

The new name

func newName(file as string, style as string) {
    def stem as string init texttransform.toCase(path.stem($file), $style);
    def ext as string init strings.lower(path.ext($file));
    return path.join(path.dir($file), $stem + $ext);
}
CallFor photos/IMG 2041.JPG
path.dirphotos
path.stemIMG 2041
path.ext.JPG
13 / 22

Look before you leap

func plan(file as string, style as string) {
    if (not fs.isFile($file)) {
        return Step{file: $file, target: "", problem: "not a file"};
    }
    def target as string init newName($file, $style);
    return Step{file: $file, target: $target, problem: ""};
}

fs.rename(old, new) replaces an existing file at new, silently.

14 / 22

The bug I found writing this talk

$ ls
My Beach Day.jpeg   my-beach-day.jpeg
$ ../tidy.j -c camel *        # first version: one file is gone
if (maps.has($taken, $s.target) or fs.exists($s.target)) {
    log.error($lg, "exists", {"file": $s.file, "target": $s.target});
    $failures = $failures + 1;
    continue;
}
$taken[$s.target] = true;
my-beach-day.jpeg -> myBeachDay.jpeg
time=... level=error msg=exists file="My Beach Day.jpeg" target=myBeachDay.jpeg
15 / 22

Dry run first

$ ../tidy.j -n *
Holiday Photo 01.JPG -> holiday-photo-01.jpg
IMG 2041 FINAL.png -> img-2041-final.png
myBeachDay.jpeg -> my-beach-day.jpeg
if (not $dryRun) {
    try {
        fs.rename($s.file, $s.target);
    } catch (e) {
        ...
    }
}
io.printf("%s -> %s\n", $s.file, ansi.green($s.target));
16 / 22

One bad file is not the end

try {
    fs.rename($s.file, $s.target);
} catch (e) {
    def why as string init $e.message;
    log.error($lg, "rename failed", {"file": $s.file, "why": $why});
    $failures = $failures + 1;
    continue;
}
$ ../tidy.j "ro/Bad Name.txt"; echo $?
time=... level=error msg="rename failed" file="ro/Bad Name.txt"
  why="fs.rename: ro/Bad Name.txt -> ro/bad-name.txt: rename ...:
  permission denied"
1
17 / 22

Colour with ansi, and when it stays off

io.printf("%s -> %s\n", $s.file, ansi.green($s.target));
SituationColour
stdout is a terminalon
stdout goes to a pipe or a fileoff
NO_COLOR is set (to anything)off, always
FORCE_COLOR is seton, even in a pipe
18 / 22

Logging with levels

def level as string init logLevel($verbose);
def lg as log.Logger init log.toStderr("logfmt", $level);
log.info($lg, "planned", {"files": "{$total}", "case": $style});
$ ../tidy.j -vv -n * >/dev/null
time=... level=info msg=planned files=4 case=kebab
time=... level=debug msg="already tidy" file=holiday-photo-01.jpg
time=... level=error msg=exists file="My Beach Day.jpeg" target=...
time=... level=info msg=done renamed=0 failed=1
19 / 22

A spinner while it works

import "@skiffyard/clispinner/" as spinner;

def opts as spinner.Options init spinner.options("dots", "checking");
def sp as spinner.Spinner init spinner.startSpinner($opts);
for (def file in $files) {
    $sp = spinner.statusSpinner($sp, $file);
    $steps[] = plan($file, $style);
}
spinner.removeSpinner($sp);

Print nothing while it spins: it owns the line.

20 / 22

Make it a command

$ head -1 tidy.j
#!/usr/bin/env -S jennifer run
$ chmod +x tidy.j
$ ln -s "$PWD/tidy.j" ~/.local/bin/tidy
$ tidy --version
tidy 1.0.0

A copy of tidy.j alone fails: it needs vendor/ next to its real path.

21 / 22

The checklist

22 / 22