USE CASE

?? Build a CLI Tool

Create command-line applications with argument parsing

What You'll Build

A file statistics tool that counts lines, words, and characters:

filestat myfile.txt
# Lines: 42
# Words: 256
# Characters: 1847

1 Create the Script

// filestat.ifa - File statistics tool

// Get command line arguments
ayanmo args = Ogbe.args();

// Check if file was provided
ti (Ogunda.len(args) < 2) {
    Irosu.fo("Usage: filestat ");
    Oyeku.exit(1);
}

ayanmo filename = Ogunda.get(args, 1);

// Check if file exists
ti (!Odi.exists(filename)) {
    Irosu.kigbe("Error: File not found: " + filename);
    Oyeku.exit(1);
}

// Read the file
ayanmo content = Odi.read(filename);

// Count statistics
ayanmo lines = Ika.split(content, "\n");
ayanmo line_count = Ogunda.len(lines);

ayanmo words = Ika.split(content, " ");
ayanmo word_count = Ogunda.len(words);

ayanmo char_count = Ika.len(content);

// Display results
Irosu.fo("?? File Statistics for: " + filename);
Irosu.fo("-------------------------");
Irosu.fo("Lines:      " + line_count);
Irosu.fo("Words:      " + word_count);
Irosu.fo("Characters: " + char_count);

2 Run Your Tool

ifa run filestat.ifa README.md

Output:

?? File Statistics for: README.md
-------------------------
Lines:      156
Words:      892
Characters: 5437

3 Add Flags and Options

// filestat.ifa - Enhanced version with flags

ayanmo args = Ogbe.args();

// Parse flags
ayanmo show_lines = otito;
ayanmo show_words = otito;
ayanmo show_chars = otito;
ayanmo filename = nil;

fun arg ninu args {
    ti (arg == "-l") {
        ayanmo show_words = iro;
        ayanmo show_chars = iro;
    } bib?k? ti (arg == "-w") {
        ayanmo show_lines = iro;
        ayanmo show_chars = iro;
    } bib?k? ti (arg == "-c") {
        ayanmo show_lines = iro;
        ayanmo show_words = iro;
    } bib?k? ti (arg != Ogunda.get(args, 0)) {
        ayanmo filename = arg;
    }
}

ti (filename == nil) {
    Irosu.fo("Usage: filestat [-l] [-w] [-c] ");
    Irosu.fo("  -l  Show only line count");
    Irosu.fo("  -w  Show only word count");
    Irosu.fo("  -c  Show only character count");
    Oyeku.exit(1);
}

// ... rest of the code

Interactive Input

Get input from the user:

// greeting.ifa - Interactive greeter

Irosu.so("What is your name? ");
ayanmo name = Irosu.ka();

Irosu.so("What is your favorite color? ");
ayanmo color = Irosu.ka();

Irosu.fo("");
Irosu.fo("Hello, " + name + "!");
Irosu.fo("I see you like " + color + ". Great choice!");

Colored Output

Use Ose (Terminal UI) for colors:

// colored.ifa - Colorful CLI output

Ose.color("green");
Irosu.fo("? Success: Operation completed");

Ose.color("yellow");
Irosu.fo("? Warning: File is large");

Ose.color("red");
Irosu.fo("? Error: Could not connect");

Ose.reset();
Irosu.fo("Back to normal text");

Building for Distribution

Compile your CLI tool for distribution:

# Build to standalone executable
ifa build filestat.ifa -o filestat

# Now users can run without ifa installed
./filestat myfile.txt