xref: /freebsd/stand/lua/cli.lua.8 (revision d4f745563d0dd9bce64696c5005b00b2e1b2e365)
1*d4f74556SKyle Evans.\"
2*d4f74556SKyle Evans.\" SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d4f74556SKyle Evans.\"
4*d4f74556SKyle Evans.\" Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
5*d4f74556SKyle Evans.\"
6*d4f74556SKyle Evans.\" Redistribution and use in source and binary forms, with or without
7*d4f74556SKyle Evans.\" modification, are permitted provided that the following conditions
8*d4f74556SKyle Evans.\" are met:
9*d4f74556SKyle Evans.\" 1. Redistributions of source code must retain the above copyright
10*d4f74556SKyle Evans.\"    notice, this list of conditions and the following disclaimer.
11*d4f74556SKyle Evans.\" 2. Redistributions in binary form must reproduce the above copyright
12*d4f74556SKyle Evans.\"    notice, this list of conditions and the following disclaimer in the
13*d4f74556SKyle Evans.\"    documentation and/or other materials provided with the distribution.
14*d4f74556SKyle Evans.\"
15*d4f74556SKyle Evans.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*d4f74556SKyle Evans.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*d4f74556SKyle Evans.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*d4f74556SKyle Evans.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*d4f74556SKyle Evans.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*d4f74556SKyle Evans.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*d4f74556SKyle Evans.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*d4f74556SKyle Evans.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*d4f74556SKyle Evans.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*d4f74556SKyle Evans.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*d4f74556SKyle Evans.\" SUCH DAMAGE.
26*d4f74556SKyle Evans.\"
27*d4f74556SKyle Evans.\" $FreeBSD$
28*d4f74556SKyle Evans.\"
29*d4f74556SKyle Evans.Dd June 9, 2018
30*d4f74556SKyle Evans.Dt CLI.LUA 8
31*d4f74556SKyle Evans.Os
32*d4f74556SKyle Evans.Sh NAME
33*d4f74556SKyle Evans.Nm cli.lua
34*d4f74556SKyle Evans.Nd FreeBSD Lua CLI module
35*d4f74556SKyle Evans.Sh DESCRIPTION
36*d4f74556SKyle Evans.Nm
37*d4f74556SKyle Evanscontains the main functionality required to add new CLI commands, which can be
38*d4f74556SKyle Evansexecuted at the loader prompt.
39*d4f74556SKyle Evans.Pp
40*d4f74556SKyle EvansBefore hooking into the functionality provided by
41*d4f74556SKyle Evans.Nm ,
42*d4f74556SKyle Evansit must be included with a statement such as the following:
43*d4f74556SKyle Evans.Pp
44*d4f74556SKyle Evans.Dl local cli = require("cli")
45*d4f74556SKyle Evans.Ss Adding new commands
46*d4f74556SKyle EvansNew loader commands may be created by adding functions to the object returned by
47*d4f74556SKyle Evansrequiring the
48*d4f74556SKyle Evans.Nm
49*d4f74556SKyle Evansmodule.
50*d4f74556SKyle Evans.Pp
51*d4f74556SKyle EvansFor instance:
52*d4f74556SKyle Evans.Pp
53*d4f74556SKyle Evans.Bd -literal -offset indent -compact
54*d4f74556SKyle Evanslocal cli = require("cli")
55*d4f74556SKyle Evans
56*d4f74556SKyle Evanscli.foo = function(...)
57*d4f74556SKyle Evans	-- Expand args to command name and the rest of argv.  These arguments
58*d4f74556SKyle Evans	-- are pushed directly to the stack by loader, then handed off to
59*d4f74556SKyle Evans	-- cli_execute.  cli_execute then passes them on to the invoked
60*d4f74556SKyle Evans	-- function, where they appear as varargs that must be peeled apart into
61*d4f74556SKyle Evans	-- their respective components.
62*d4f74556SKyle Evans	local _, argv = cli.arguments(...)
63*d4f74556SKyle Evans
64*d4f74556SKyle Evans	print("This is the foo command!")
65*d4f74556SKyle Evans	for k, v in ipairs(argv) do
66*d4f74556SKyle Evans		print("arg #" .. tostring(k) .. ": '" .. v .. "'")
67*d4f74556SKyle Evans	end
68*d4f74556SKyle Evans	-- Perform a loader command directly.  This will not get dispatched back
69*d4f74556SKyle Evans	-- to Lua, so it is acceptable to have a function of the exact same name
70*d4f74556SKyle Evans	-- in loader.  Lua will have the first chance to handle any commands
71*d4f74556SKyle Evans	-- executed at the loader prompt.
72*d4f74556SKyle Evans	loader.perform("foo")
73*d4f74556SKyle Evansend
74*d4f74556SKyle Evans.Ed
75*d4f74556SKyle Evans.Pp
76*d4f74556SKyle EvansThis function may be invoked by a user at the loader prompt by simply typing
77*d4f74556SKyle Evans.Ic foo .
78*d4f74556SKyle EvansArguments may be passed to it as usual, space-delimited.
79*d4f74556SKyle Evans.Ss Default Commands
80*d4f74556SKyle EvansAs of present, the
81*d4f74556SKyle Evans.Nm
82*d4f74556SKyle Evansmodule by default provides commands for
83*d4f74556SKyle Evans.Ic autoboot
84*d4f74556SKyle Evansand
85*d4f74556SKyle Evans.Ic boot .
86*d4f74556SKyle EvansIn both cases, the
87*d4f74556SKyle Evans.Xr core.lua 8
88*d4f74556SKyle Evansmodule will load all ELF modules as-needed before executing the equivalent
89*d4f74556SKyle Evansbuilt-in loader commands.
90*d4f74556SKyle EvansAll non-kernel arguments to these commands are passed in the same order to the
91*d4f74556SKyle Evansloader command.
92*d4f74556SKyle Evans.Ss Exported Functions
93*d4f74556SKyle EvansThe following functions are exported from
94*d4f74556SKyle Evans.Nm :
95*d4f74556SKyle Evans.Bl -tag -width cli.arguments -offset indent
96*d4f74556SKyle Evans.It Fn cli.arguments ...
97*d4f74556SKyle EvansTakes varargs passed on the stack from
98*d4f74556SKyle Evans.Xr loader 8
99*d4f74556SKyle Evansto
100*d4f74556SKyle Evans.Ic cli_execute ,
101*d4f74556SKyle Evanssplits them out into two return values: the command name, traditionally argv[0],
102*d4f74556SKyle Evansand the rest of argv.
103*d4f74556SKyle Evans.El
104*d4f74556SKyle Evans.Sh SEE ALSO
105*d4f74556SKyle Evans.Xr loader.conf 5 ,
106*d4f74556SKyle Evans.Xr core.lua 8 ,
107*d4f74556SKyle Evans.Xr loader 8
108*d4f74556SKyle Evans.Sh AUTHORS
109*d4f74556SKyle EvansThe
110*d4f74556SKyle Evans.Nm
111*d4f74556SKyle Evansfile was originally written by
112*d4f74556SKyle Evans.An Kyle Evans Aq Mt kevans@FreeBSD.org .
113