Skip to content

Command-Line Interface (kalc-cli)

kalc-cli is a standalone, high-performance command-line utility wrapping the headless KalcCore calculation engine. It allows you to run calculations, unit conversions, and multi-line financial models directly inside your terminal, shell scripts, and CI/CD pipelines.


1. One-Shot Expression Evaluation

Pass any expression as a command-line argument in quotes. kalc-cli evaluates the expression using the full Pratt parser and prints the formatted result:

bash
$ kalc-cli "20 inches in cm"
50.8 cm

$ kalc-cli "sqrt(256) * 10"
160

$ kalc-cli "$100 + 15% in EUR"
105.80 EUR

$ kalc-cli "time to download 45 GB at 150 Mbps"
40 min

2. Piped Standard Input Stream Processing

kalc-cli accepts text streams from standard input (stdin). When piping multi-line documents, kalc-cli outputs each line alongside its aligned calculation result, mirroring the desktop application's gutter:

bash
$ cat project.kalc | kalc-cli

Inline Shell Here-Doc

bash
$ kalc-cli << 'EOF'
dev_rate = $125 / hr
hours = 42 hrs
subtotal = dev_rate * hours
tax = 8.5% on subtotal
total = subtotal + tax
EOF

dev_rate = $125 / hr                            $125.00 / hr
hours = 42 hrs                                  42 hrs
subtotal = dev_rate * hours                     $5,250.00
tax = 8.5% on subtotal                          $5,696.25
total = subtotal + tax                          $5,696.25

3. The --last Scalar Extraction Flag

When writing shell automation scripts, you often want only the final computed scalar value—without formatting, units, comments, or intermediate line outputs.

The --last flag isolates and prints only the final computed result:

bash
$ echo -e "a = 50\nb = 20\na * b" | kalc-cli --last
1000

Practical Shell Scripting Example

Automate server cost threshold checks in a deployment bash script:

bash
#!/usr/bin/env bash

MONTHLY_BUDGET=500

PROJECTED_COST=$(kalc-cli --last << 'EOF'
instances = 6 * $0.096/hr * 720 hrs
storage = 850 GB * $0.023/GB
instances + storage
EOF
)

echo "Calculated Monthly Cloud Spend: \$${PROJECTED_COST}"

if (( $(echo "${PROJECTED_COST} > ${MONTHLY_BUDGET}" | bc -l) )); then
    echo "⚠️ Warning: Projected cost exceeds \$${MONTHLY_BUDGET} budget!"
fi

4. Compiling & Installing kalc-cli

To compile and install the CLI binary locally:

bash
# Inside apps/macos
DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer swift build -c release --product kalc-cli

# Copy the optimized universal binary to your PATH
sudo cp .build/release/kalc-cli /usr/local/bin/

Verify installation:

bash
$ which kalc-cli
/usr/local/bin/kalc-cli

$ kalc-cli "2^16 in hex"
0x10000

Native macOS Computational Notepad • Pure Swift with Strict Concurrency