Building a CLI Tool
This tutorial will guide you through creating a simple command-line interface tool in Axon that echoes back arguments with a prefix.
Prerequisites
Section titled “Prerequisites”- Axon compiler installed (v0.1.0 or higher)
- Basic understanding of S-expressions
Step 1: Initializing the Project
Section titled “Step 1: Initializing the Project”Create a new directory and initialize an Axon project using axon-pkg:
mkdir echo-clicd echo-cliaxon initThis will generate a project.axs manifest file and a src/main.axs file.
Step 2: Parsing Arguments
Section titled “Step 2: Parsing Arguments”In Axon, command line arguments are accessed via the std/os standard library module. Open src/main.axs and replace its contents with:
(import std/os)(import std/io)
(fn main () (let args (os.args))
(if (<= (len args) 1) (do (io.println "Usage: echo-cli <message>") (os.exit 1))
;; Else, echo the arguments (let message (join (slice args 1 (len args)) " ")) (io.println (+ "Echo: " message))))Step 3: Compiling the Tool
Section titled “Step 3: Compiling the Tool”To compile the tool into a standalone native binary, run:
axon build --releaseBecause Axon compiles directly to machine code via LLVM, this will produce a highly optimized binary in the build/ directory without any dependencies on an interpreter or virtual machine.
Step 4: Running
Section titled “Step 4: Running”Test your new CLI tool:
./build/echo-cli "Hello Axon World!"Output:
Echo: Hello Axon World!Congratulations! You’ve just built your first high-performance native CLI tool using Axon.