2022-12-15 04:00:54 +00:00
|
|
|
---
|
|
|
|
title: Module commander
|
|
|
|
description: library for custom commands
|
|
|
|
layout: doc
|
|
|
|
menu:
|
|
|
|
docs:
|
|
|
|
parent: "API"
|
|
|
|
---
|
|
|
|
|
|
|
|
## Introduction
|
2023-01-20 23:07:42 +00:00
|
|
|
|
2023-12-01 02:19:13 +00:00
|
|
|
Commander is the library which handles Hilbish commands. This makes
|
|
|
|
the user able to add Lua-written commands to their shell without making
|
|
|
|
a separate script in a bin folder. Instead, you may simply use the Commander
|
|
|
|
library in your Hilbish config.
|
2023-01-20 23:07:42 +00:00
|
|
|
|
|
|
|
```lua
|
|
|
|
local commander = require 'commander'
|
|
|
|
|
|
|
|
commander.register('hello', function(args, sinks)
|
|
|
|
sinks.out:writeln 'Hello world!'
|
|
|
|
end)
|
|
|
|
```
|
|
|
|
|
|
|
|
In this example, a command with the name of `hello` is created
|
|
|
|
that will print `Hello world!` to output. One question you may
|
|
|
|
have is: What is the `sinks` parameter?
|
|
|
|
|
2023-02-07 19:39:22 +00:00
|
|
|
The `sinks` parameter is a table with 3 keys: `in`, `out`,
|
2023-12-01 02:19:13 +00:00
|
|
|
and `err`. All of them are a <a href="/Hilbish/docs/api/hilbish/#sink" style="text-decoration: none;">Sink</a>.
|
2023-01-20 23:07:42 +00:00
|
|
|
|
2023-12-01 02:19:13 +00:00
|
|
|
- `in` is the standard input.
|
|
|
|
You may use the read functions on this sink to get input from the user.
|
|
|
|
- `out` is standard output.
|
|
|
|
This is usually where command output should go.
|
|
|
|
- `err` is standard error.
|
|
|
|
This sink is for writing errors, as the name would suggest.
|
2023-01-20 23:07:42 +00:00
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
## Functions
|
2023-09-02 18:18:40 +00:00
|
|
|
|||
|
|
|
|
|----|----|
|
2023-12-01 02:19:13 +00:00
|
|
|
|<a href="#deregister">deregister(name)</a>|Removes the named command. Note that this will only remove Commander-registered commands.|
|
|
|
|
|<a href="#register">register(name, cb)</a>|Adds a new command with the given `name`. When Hilbish has to run a command with a name,|
|
2023-09-02 18:18:40 +00:00
|
|
|
|
|
|
|
<hr><div id='deregister'>
|
|
|
|
<h4 class='heading'>
|
|
|
|
commander.deregister(name)
|
|
|
|
<a href="#deregister" class='heading-link'>
|
|
|
|
<i class="fas fa-paperclip"></i>
|
|
|
|
</a>
|
|
|
|
</h4>
|
|
|
|
|
2023-12-01 02:19:13 +00:00
|
|
|
Removes the named command. Note that this will only remove Commander-registered commands.
|
2023-08-26 15:30:51 +00:00
|
|
|
#### Parameters
|
2023-12-01 02:19:13 +00:00
|
|
|
`string` **`name`**
|
|
|
|
Name of the command to remove.
|
|
|
|
|
2023-09-02 20:01:57 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<hr><div id='register'>
|
2023-09-02 18:18:40 +00:00
|
|
|
<h4 class='heading'>
|
|
|
|
commander.register(name, cb)
|
|
|
|
<a href="#register" class='heading-link'>
|
|
|
|
<i class="fas fa-paperclip"></i>
|
|
|
|
</a>
|
|
|
|
</h4>
|
2022-12-15 04:00:54 +00:00
|
|
|
|
2023-12-01 02:19:13 +00:00
|
|
|
Adds a new command with the given `name`. When Hilbish has to run a command with a name,
|
|
|
|
it will run the function providing the arguments and sinks.
|
|
|
|
|
|
|
|
|
2023-08-26 15:30:51 +00:00
|
|
|
#### Parameters
|
2023-12-01 02:19:13 +00:00
|
|
|
`string` **`name`**
|
|
|
|
Name of the command
|
|
|
|
|
|
|
|
`function` **`cb`**
|
|
|
|
Callback to handle command invocation
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
```lua
|
|
|
|
-- When you run the command `hello` in the shell, it will print `Hello world`.
|
|
|
|
-- If you run it with, for example, `hello Hilbish`, it will print 'Hello Hilbish'
|
|
|
|
commander.register('hello', function(args, sinks)
|
|
|
|
local name = 'world'
|
|
|
|
if #args > 0 then name = args[1] end
|
|
|
|
|
|
|
|
sinks.out:writeln('Hello ' .. name)
|
|
|
|
end)
|
|
|
|
````
|
2023-09-02 20:01:57 +00:00
|
|
|
</div>
|
|
|
|
|