Yes or no, and try
In the terminalela --spec yes-or-no-and-try
A yes-or-no question to a command is a condition, with no try: if cmd runs its block on
exit 0, if not cmd on any other exit, and while and else if read them the same.
ela <<'EOF'if grep -q TODO notes.md echo "notes.md has a TODO"end ifif not grep -q FIXME notes.md echo "notes.md has no FIXME"end ifEOF
try is for a value, never for yes or no (--lint points if try cmd == null to if cmd).
Each form gives null when nothing failed, or an Error, last:
| You want | Write |
|---|---|
| which failure it was | err = try grep -q x f, then if error.is($err, 2) (grep's 2 is an error, 1 is "not found") |
| the output of a command that may fail | out, err = try $(git describe) |
| why a call failed | data, err = try json.decode($text), then if $err != null |
| to run a command and go on whatever happens | try rm old.log |
The names count what the form gives: one for a command, two for $( ), a call's values and one
more for a call, and one for a call that returns nothing, err = try cleanup(). What try gave
is compared with null, never a number or "": if $code != 0 after code = try cmd stops
before line 1 and names error.is.
An Error prints as its chain, loading the site: reading config.toml: not found, and has
kind, message, cause, code and stderr. error.is($err, Error.NOT_FOUND) asks its kind
anywhere down the chain (ela --spec errors).
try covers a command that ran, a call that failed and a fail below it, nothing else:
| Wrong | ela says | Right |
|---|---|---|
err = try ls -d build/*/x | no files match build/*/x | if len(path.glob("build/*/x")) > 0, or file.exists |
err = try gti status | command not found: gti | the name spelled right; ela --lint - finds it before a run |
n = try $count + 1 | try takes a command or a proc call, and $count + 1 is an expression | n = $count + 1 |
Text already in a variable needs no program: str.contains($text, "TODO"),
re.match("^v[0-9]+", $tag). Whether a path is there is file.exists(p), not ls.
To stop with a message, fail "no config at $path" raises an Error: a try on any call above it
catches it, and with none the script writes ela: no config at … to stderr and exits 1. One more
value after the message, by its type: a kind, fail "no config", Error.NOT_FOUND; a cause to wrap,
fail "loading the site", $err; or an exit code, fail "bad input", 2. There is no die, and
warn then exit 1 is fail in two lines (ela --spec fail).
(ela --spec try)