cmd v1.2.0

Command-line interface module for ComputerCraft that provides a REPL-style command processor with support for custom commands, autocompletion, and command history. Features: Built-in commands (clear, exit, help), command history navigation, tab autocompletion for commands and arguments, colored output for different message types, table pretty-printing functionality, pager for long output, string utility functions, command categories for organized help display, and proper alias handling.

Installation

⚠️ Dependencies: This library requires: pager
Using installer will automatically install all dependencies.

Recommended: Install via installer (handles dependencies automatically):

wget run https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua cmd
View on GitHub →

This pattern downloads and runs libraries at runtime, automatically installing any that are missing:

-- Auto-install and require libraries
local libs = {"cmd"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true

for _, lib in ipairs(libs) do
    if not fs.exists(libDir .. lib .. ".lua") then
        allExist = false
        break
    end
end

if not allExist then
    shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end

local cmd = require(libDir .. "cmd")

-- Use the library
-- (your code here)

Examples

Using with Runtime Installation

This example shows how to download and use libraries with automatic installation:

-- Auto-install and require libraries
local libs = {"cmd"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true

for _, lib in ipairs(libs) do
    if not fs.exists(libDir .. lib .. ".lua") then
        allExist = false
        break
    end
end

if not allExist then
    shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end

local cmd = require(libDir .. "cmd")

-- Use the library

Usage Examples

local cmd = require("cmd")
local customCommands = {
 hello = {
   description = "Say hello to someone",
   category = "general",
   aliases = {"hi", "greet"},
   execute = function(args, context)
     local name = args[1] or "World"
     context.succ("Hello, " .. name .. "!")
   end
 },
 longlist = {
   description = "Show a long list with pagination",
   category = "utilities",
   execute = function(args, context)
     local p = context.pager("My Long List")
     for i = 1, 100 do
       p.print("Item " .. i)
     end
     p.show()
   end
 }
}
cmd("MyApp", "1.0.0", customCommands)

Functions

string.split(self, sep?, plain?)

View source on GitHub

Split a string into an array of substrings based on a separator

Parameters:
Returns: string[] # Array of split substrings

string.startsWith(self, target, caseSensitive?)

View source on GitHub

Check if a string starts with a target substring

Parameters:
Returns: boolean # True if the string starts with the target