shim: command line library for lua.
Write scripts as Lua libraries, execute them in the shell.
This is a tiny Lua module that makes it easy to write utilities
that work from either bash or Lua.
Usage:
#!/usr/bin/env -S lua -e "require'pkglib'()"
local shim = require'shim'
local M = shim.cmd'myModName' {
'arg [type]: documentation',
arg = 'default-value',
}
-- ... nomral lua module
function M.someFunction() ... end
if shim.isMain(M) then M:main(arg) end
return M -- return as library
shim: command line library for lua.
Functions
- fn parse(v) -> args
Parse either a string or list and convert them to key=value table.
v can be either a list of {'strings', '--option=foo'}
or {'strings", option='foo'} or a combination of both. If v is a string then
it is split on whitespace and parsed as a list.
Note: this handles repeat keys by creating and appending a list for that key.
- fn parseStr(str) -> args
parses the string by splitting via whitespace.
Asserts the string contains no special chars: '"[]
This is for convinience, use a table if it's not enough.
Note: if the input is already a table it just returns it.
- fn parseList(strlist) -> args
Note: typically use parse() or parseStr() instead.
- fn short(args, short, long, value) -> nil
Helper for dealing with -s --short arguments. Mutates
args to convert short paramaters to their long counterpart.
- fn boolean(v)
Duck type: always return a boolean (except for nil).
See BOOLS (above) for mapping.
- fn boolean(v)
Duck type: always return a boolean (except for nil).
See BOOLS (above) for mapping.
- fn bools(args, ...)
- fn number(num)
Duck type: always return a number
- fn string(v)
Duck type: always return a string
This is useful for some APIs where you want to convert
number/true/false to strings
Converts nil to ''
- fn list(val, default, empty) -> {string}
Duck type: always return a list.
- default controls val==nil
- empty controls val==''
- fn listSplit(val, sep)
Duck type: if val is a string then splits it
if it's a list leaves alone.
- fn file(v, default, mode--[[w+]]) -> file, error?
Duck type: get a file handle.
If v or default is a string then open the file in mode default='w+'
- fn expand(args)
expand string keys into --key=value, ordered alphabetically.
This is mostly useful for interfacing with non-lua shells.
- fn getEnvBool(k)
return nil if env var does not exist, else boolean (true for 'true' or '1')
- fn popRaw(args, to) -> to
pop raw arguments after '--'
Removes them (including '--') from args.
- fn runSetup(args, force)
Setup lua using require(LUA_SETUP).setup(args, force).
The default is to use ds.setup.
- fn construct(Cmd, args) -> ok, err?
- fn constructNew(Cmd, args) -> ok, err?
- fn init(Cmd, args)
FIXME delete
- fn run(Args, args)
- fn isCmd(R)
Return whether a record was created with fn cmd
- fn cmd(name)
Create a new command as your module.
local M = shim.cmd'mycmd' {
'...: argument documentation',
'foo: some paramater',
'to [path|file]: where to write output',
}
-- ... rest of your module.
if shim.isMain(M) then M:main(arg) end
return M
- fn subcmds(name)
Create a command composed of subcommands.
- fn isMain(cmd)
Usage: if shim.isMain(M) then os.exit(M:main(arg)) end