- Notifications
You must be signed in to change notification settings - Fork 0
Creating Custom Commands
RafaPear edited this page Jun 23, 2025 · 1 revision
One of CLILib's strengths is how easy it is to create your own commands. Implement the Command
interface with the required fields and the behaviour you want.
object HelloCmd : Command { override val description = "Say hello to the user" override val usage = "hello <name>" override val aliases = listOf("hello", "hi") override val minArgs = 1 override val maxArgs = 1 override fun run(args: List<String>): Boolean { println("Hello, ${'$'}{args[0]}!") return true } }
Then simply register the command:
CmdRegister.register(HelloCmd)
-
description
– short description -
usage
– how to use the command (shown inhelp
) -
aliases
– names the command can be called by -
run(args: List<String>)
– execution logic
-
minArgs
/maxArgs
— expected number of arguments -
requiresFile
/fileExtension
— validate file paths -
commands
— define subcommands or flags (e.g.-h
,--list
) -
longDescription
— longer description shown withhelp <command>
You can also create simple commands from a .json
file using mkcmd
.
mkcmd example.json
Example example.json
:
{ "description": "Example command via JSON", "longDescription": "This command was created from a JSON file.", "usage": "exjson <arg>", "aliases": ["exjson"], "minArgs": 1, "maxArgs": 1, "requiresFile": false, "fileExtension": "", "run": "print arg[0]" }
- Call
validateArgs(args, this)
at the start ofrun
- Store temporary results in
lastCmdDump
(so they can be used withvar
) - Use
println
sparingly (only for useful messages) - Give aliases clear and short names
With these tools you can extend CLILib to support any functionality you need — from system commands to external API integrations.
Made with ❤️ by Rafael Vermelho Pereira This project is under the MIT License · Contributions are welcome! 🤝 How to Contribute