Skip to content

Commit

Permalink
#16 Pass command-line arguments to Lua programs
Browse files Browse the repository at this point in the history
  • Loading branch information
billbirchatcoles committed Apr 10, 2021
1 parent c4f07f5 commit f7c9a3e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 10 deletions.
61 changes: 51 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,9 @@ Which results in an indented bullet list:
## Lua Examples

### JSON (again)
### Accessing the Tree from Lua and output to JSON

First, we are going tell frangipanni to output via a Lua program called 'json.lua', and we will format the json with the 'jp' program.
First, we are going tell frangipanni to output via a Lua program called `json.lua`, and we will format the json with the 'jp' program.

```
$ <test/fixtures/simplechars.txt frangipanni -lua json.lua | jp @
Expand All @@ -453,19 +453,17 @@ $ <test/fixtures/simplechars.txt frangipanni -lua json.lua | jp @
The Lua script uses the `github.com/layeh/gopher-json` module which is imported in the Lua. The data
is made available in the variable `frangipanni` which has a table for each node, with fields

* depth - in the tree starting from 0
* lineNumber - the token was first detected
* numMatched - the number of times the token was seen
* sep - separation characters preceding the token
* text - the token itself
* children - a table containing the child nodes
* `depth` - in the tree starting from 0
* `lineNumber` - the token was first detected
* `numMatched` - the number of times the token was seen
* `sep` - separation characters preceding the token
* `text` - the token itself
* `children` - a table containing the child nodes

```Lua
local json = require("json")

print(json.encode(frangipanni))
```

The output shows that all the fields of the parsed nodes are passed to Lua in a Table.
The root node is empty except for it's children. The Lua script is therafore able to use
the fields intelligently.
Expand Down Expand Up @@ -498,6 +496,39 @@ the fields intelligently.
}
```

### Accessing frangipanni command-line arguments from Lua

frangipanni hands the command arguments to the Lua interpreter in the variable `frangipanni_args`. This holds a table keyed on the argument switch and holding the current value in a string. Command-line arguments found after switches are collected in the `args` array in this table. This example Lua program, `args.lua`, prints the table in JSON format:
```Lua
local json = require("json")
print(json.encode(frangipanni_args))
```
The output is:
```JSON
$ ./frangipanni -breaks / -counts -depth 3 -level 20 -lua args.lua arg1 arg2 </dev/null | jp '@'
{
"args": [
"arg1",
"arg2"
],
"breaks": "/",
"chars": "false",
"counts": "true",
"depth": "3",
"down": "false",
"format": "indent",
"indent": "4",
"level": "20",
"lua": "args.lua",
"no-fold": "false",
"separators": "false",
"skip": "0",
"sort": "input",
"spacer": " ",
"version": "false"
}
```

### Markdown

This example shows recursive scanning of the tree, with output format controlled by
Expand Down Expand Up @@ -549,6 +580,16 @@ or with `NUMBERED_LIST=1 ./frangipanni -lua markdown.lua <test/fixtures/simplech
1. 1
1. 2
```
Which renders like this:
1. Z
1. 1.2
1. A
1. C
1. 2
1. D
1. x.a
1. 1
1. 2

### XML

Expand Down
5 changes: 5 additions & 0 deletions args.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--
-- Output command-line in JSON format
--
local json = require("json")
print(json.encode(frangipanni_args))
17 changes: 17 additions & 0 deletions frangipanni.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,26 @@ func makeLuaTableFromNode(L *lua.LState, n *node) *lua.LTable {
return tb
}

func makeLuaTableFromFlags(L *lua.LState) *lua.LTable {
tb := L.CreateTable(6, 6)
flag.VisitAll(func(f *flag.Flag) {
if f.Name == "order" { // Deprecated
return
}
tb.RawSet(lua.LString(f.Name), lua.LString(f.Value.String()))
})
args := L.CreateTable(len(flag.Args()), len(flag.Args()))
for i, a := range flag.Args() {
args.RawSetInt(i+1, lua.LString(a))
}
tb.RawSet(lua.LString("args"), args)
return tb
}

func luaRun(out io.Writer, root *node) {
L := lua.NewState()
luajson.Preload(L)
L.SetGlobal("frangipanni_args", makeLuaTableFromFlags(L))
L.SetGlobal("frangipanni", makeLuaTableFromNode(L, root))
defer L.Close()
if err := L.DoFile(luaFile); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions test/confidence.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ then
exit 1
fi
tempfile=$(mktemp)

echo "--- -breaks / -counts -depth 3 -level 20 -lua args.lua ----------------------------------------------------------------------------------------------------" >> "$tempfile"
../frangipanni -breaks / -counts -depth 3 -level 20 -lua ../args.lua arg1 arg2 </dev/null | jp '@' >> "$tempfile"

for tf in fixtures/*
do
for sw in '' -chars '-breaks /' -separators -counts -no-fold '-level 2' '-depth 2' '-spacer ^ -indent 1 -counts'
Expand Down
22 changes: 22 additions & 0 deletions test/fixtures.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
--- -breaks / -counts -depth 3 -level 20 -lua args.lua ----------------------------------------------------------------------------------------------------
{
"args": [
"arg1",
"arg2"
],
"breaks": "/",
"chars": "false",
"counts": "true",
"depth": "3",
"down": "false",
"format": "indent",
"indent": "4",
"level": "20",
"lua": "../args.lua",
"no-fold": "false",
"separators": "false",
"skip": "0",
"sort": "input",
"spacer": " ",
"version": "false"
}
fixtures/find.txt--- ----------------------------------------------------------------------------------------------------
proc/22468
io
Expand Down

0 comments on commit f7c9a3e

Please sign in to comment.