diff --git a/docs/api/commander.md b/docs/api/commander.md index 4d67c81..dcb6306 100644 --- a/docs/api/commander.md +++ b/docs/api/commander.md @@ -9,11 +9,10 @@ menu: ## Introduction -Commander is a library for writing custom commands in Lua. -In order to make it easier to write commands for Hilbish, -not require separate scripts and to be able to use in a config, -the Commander library exists. This is like a very simple wrapper -that works with Hilbish for writing commands. Example: +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. ```lua local commander = require 'commander' @@ -28,20 +27,20 @@ that will print `Hello world!` to output. One question you may have is: What is the `sinks` parameter? The `sinks` parameter is a table with 3 keys: `in`, `out`, -and `err`. The values of these is a Sink. +and `err`. All of them are a Sink. -- `in` is the standard input. You can read from this sink -to get user input. -- `out` is standard output. This is usually where text meant for -output should go. -- `err` is standard error. This sink is for writing errors, as the -name would suggest. +- `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. ## Functions ||| |----|----| -|deregister(name)|Deregisters any command registered with `name`| -|register(name, cb)|Register a command with `name` that runs `cb` when ran| +|deregister(name)|Removes the named command. Note that this will only remove Commander-registered commands.| +|register(name, cb)|Adds a new command with the given `name`. When Hilbish has to run a command with a name,|

@@ -51,9 +50,11 @@ commander.deregister(name)

-Deregisters any command registered with `name` +Removes the named command. Note that this will only remove Commander-registered commands. #### Parameters -This function has no parameters. +`string` **`name`** +Name of the command to remove. +

@@ -64,8 +65,27 @@ commander.register(name, cb) -Register a command with `name` that runs `cb` when ran +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. + + #### Parameters -This function has no parameters. +`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) +````
diff --git a/emmyLuaDocs/commander.lua b/emmyLuaDocs/commander.lua index c6738dd..285c4b5 100644 --- a/emmyLuaDocs/commander.lua +++ b/emmyLuaDocs/commander.lua @@ -2,13 +2,13 @@ local commander = {} ---- Deregisters any command registered with `name` ---- @param name string +--- Removes the named command. Note that this will only remove Commander-registered commands. function commander.deregister(name) end ---- Register a command with `name` that runs `cb` when ran ---- @param name string ---- @param cb function +--- 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. +--- +--- function commander.register(name, cb) end return commander diff --git a/golibs/commander/commander.go b/golibs/commander/commander.go index a21de1c..ea2da7a 100644 --- a/golibs/commander/commander.go +++ b/golibs/commander/commander.go @@ -1,10 +1,9 @@ // library for custom commands /* -Commander is a library for writing custom commands in Lua. -In order to make it easier to write commands for Hilbish, -not require separate scripts and to be able to use in a config, -the Commander library exists. This is like a very simple wrapper -that works with Hilbish for writing commands. Example: +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. ```lua local commander = require 'commander' @@ -19,14 +18,14 @@ that will print `Hello world!` to output. One question you may have is: What is the `sinks` parameter? The `sinks` parameter is a table with 3 keys: `in`, `out`, -and `err`. The values of these is a @Sink. +and `err`. All of them are a @Sink. -- `in` is the standard input. You can read from this sink -to get user input. -- `out` is standard output. This is usually where text meant for -output should go. -- `err` is standard error. This sink is for writing errors, as the -name would suggest. +- `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. */ package commander @@ -67,9 +66,22 @@ func (c *Commander) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { } // register(name, cb) -// Register a command with `name` that runs `cb` when ran -// --- @param name string -// --- @param cb function +// 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. +// #param name string Name of the command +// #param cb function Callback to handle command invocation +/* +#example +-- 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) +#example +*/ func (c *Commander) cregister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) { cmdName, cmd, err := util.HandleStrCallback(t, ct) if err != nil { @@ -82,8 +94,8 @@ func (c *Commander) cregister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) { } // deregister(name) -// Deregisters any command registered with `name` -// --- @param name string +// Removes the named command. Note that this will only remove Commander-registered commands. +// #param name string Name of the command to remove. func (c *Commander) cderegister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) { if err := ct.Check1Arg(); err != nil { return nil, err