Build a CLI Tool
Create a command-line application
Let’s build a simple CLI tool that counts words in a file.
Setup
nanyx new word_counter
cd word_counter
The code
import (
nanyx/file
nanyx/string
nanyx/collections
)
match file.read("input.txt")
| #some(content) ->
def words = content
\split(on = " ")
\filter { != "" }
def count = words.length
println("Word count: {count}")
| #error(err) ->
println("Error reading file: {err}")
Run it
echo "hello world from nanyx" > input.txt
nanyx run
# Word count: 4
Key Concepts
- File I/O — the
filemodule returns result types for safe error handling - Pipelines — chain transformations with
\for readable data processing - Pattern matching — handle success and failure cases explicitly