What: Carapace is a toy shell and embeddable command language. It's mainly for programmers who want to add a command line to their applications but not another external dependency.
Why: I like the kind of interpreter described in this article, but it's too limited for serious use. Carapace proves it can be done better easily enough.
How: The key insight enabling Carapace is that a string can be split into quoted arguments by rules similar to shell script with a simple regular expression:
/'[^']*'|"[^"]*"|`[^`]*`|\S+/g
It's simplified and not entirely correct, but works well enough for most cases. The one big downside is a complete inability to escape delimiters.
As of , Carapace is available as a Python prototype. A Javascript intepreter is planned, along with documentation.
The Python prototype works as a very simple OS shell, but that's intended as a demo, having only minimal facilities. It can be embedded in Python 2.7 code, but needs Python 3 to run stand-alone.
(License to be added soon.)
This one-liner uses only built-ins:
foreach i `seq 1 10` echo "i = $i"
This one falls back on the date
utility, so it only runs in stand-alone mode:
set name Carapace
echo 'Hi there!' "My name is $name."
echo "Today it's `date`"
# Deliberately cause an error to see what happens.
echo $novar
Run Carapace with the --help
argument for usage instructions.
Carapace was initially a C++ library that never saw use. I considered adding a "2" to the name, but decided against it because it's supposed to get more implementations anyway.