Add CLI file feature

This commit is contained in:
Ganesh Viswanathan 2020-05-12 22:01:42 -05:00
commit 37fa2ad7fb
6 changed files with 35 additions and 18 deletions

View file

@ -58,6 +58,8 @@ https://github.com/nimterop/nimterop/compare/v0.4.4...v0.5.0
- `toast` also includes `--typeMap | -T` to map C types to another type. E.g. `--typeMap:GLint64=int64` generates a wrapper where all instances of `GLint64` are remapped to the Nim type `int64` and `GLint64` is not defined. (since v0.5.2)
- CLI flags can now be specified one or more per line in a file and path provided to `toast`. They will be expanded in place. [#196][i196] (since v0.5.3)
- Nimterop is now able to detect Nim configuration of projects and can better handle cases where defaults such as `nimcacheDir` or `nimblePath` are overridden. This especially enables better interop with workflows that do not depend on Nimble. [#151][i151] [#153][i153]
- Nimterop defaults to `cmake`, followed by `autoconf` for building libraries with `getHeader()`. It is now possible to change the order of discovery with the `buildType` value. [#200][i200]
@ -78,5 +80,6 @@ https://github.com/nimterop/nimterop/compare/v0.4.4...v0.5.0
[i174]: https://github.com/nimterop/nimterop/issues/174
[i176]: https://github.com/nimterop/nimterop/issues/176
[i181]: https://github.com/nimterop/nimterop/issues/181
[i196]: https://github.com/nimterop/nimterop/issues/196
[i197]: https://github.com/nimterop/nimterop/issues/197
[i200]: https://github.com/nimterop/nimterop/issues/200

View file

@ -195,14 +195,14 @@ Nimterop also provides a [docs](https://nimterop.github.io/nimterop/docs.html) A
### Command line API
The `toast` binary can also be used directly on the CLI, similar to `c2nim`. The `cPlugin()` interface
The `toast` binary can also be used directly on the CLI, similar to `c2nim`. These flags can be specified on the command line or via a file, one or more flags per line, and the path provided to `toast` instead, or a combination. The file contents will be expanded in place.
Note: unlike the wrapper API, the `-p | --preprocess` flag is not enabled by default but is *highly* recommended.
```
> toast -h
Usage:
main [optional-params] C/C++ source/header
main [optional-params] C/C++ source/header(s) and command line file(s)
Options:
-h, --help print this cligen-erated help
--help-syntax advanced: prepend,plurals,..

View file

@ -59,8 +59,8 @@ task test, "Test":
execTest "tests/tnimterop_c.nim", "-d:FLAGS=\"-f:ast2 -H\""
execCmd "nim cpp --hints:off -f -r tests/tnimterop_cpp.nim"
execCmd "./nimterop/toast -pnk -E=_ tests/include/toast.h"
execCmd "./nimterop/toast -pnk -E=_ -f:ast2 tests/include/toast.h"
execCmd "./nimterop/toast tests/toast.cfg tests/include/toast.h"
execCmd "./nimterop/toast tests/toast.cfg -f:ast2 tests/include/toast.h"
execTest "tests/tpcre.nim"
execTest "tests/tpcre.nim", "-d:FLAGS=\"-f:ast2\""

View file

@ -139,7 +139,7 @@ proc newConstDef(gState: State, node: TSNode, fname = "", fval = ""): PNode =
else:
gecho &"# const '{origname}' is duplicate, skipped"
else:
gecho &"# const '{origname}' has invalid value '{val}'"
gecho &"# const '{origname}' has unsupported value '{val}'"
gState.skippedSyms.incl origname
proc addConst(gState: State, node: TSNode) =
@ -1736,19 +1736,9 @@ proc setupPragmas(gState: State, root: TSNode, fullpath: string) =
# Add `{.experimental: "codeReordering".} for #206
gState.pragmaSection.add gState.newPragma(root, "experimental", newStrNode(nkStrLit, "codeReordering"))
proc printNimHeader*(gState: State) =
# Top level output with context info
gecho """# Generated at $1
# Command line:
# $2 $3
{.hint[ConvFromXtoItselfNotNeeded]: off.}
import nimterop/types
""" % [$now(), getAppFilename(), commandLineParams().join(" ")]
proc initNim*(gState: State) =
# Initialize for parseNim() one time
gecho "import nimterop/types\n"
# Track identifiers already rendered and corresponding PNodes
gState.identifiers = newTable[string, string]()

View file

@ -141,7 +141,6 @@ proc main(
elif source.nBl:
# Print source after preprocess or Nim output
if gState.pnim:
gState.printNimHeader()
gState.initNim()
for src in source:
gState.process(src.expandSymlinkAbs(), astTable)
@ -199,6 +198,29 @@ proc main(
if check and output.len == 0:
stdout.write outputFile.readFile()
proc mergeParams(cmdNames: seq[string], cmdLine = commandLineParams()): seq[string] =
# Load command-line params from `source` if it is a .cfg file
if cmdNames.len != 0:
# https://github.com/c-blake/cligen/issues/149
for param in cmdLine:
if param.fileExists() and param.splitFile().ext == ".cfg":
echo &"# Loading flags from '{param}'"
for line in param.readFile().splitLines():
let
line = line.strip()
if line.len > 1 and line[0] != '#':
result.add line.parseCmdLine()
else:
result.add param
if result.len != 0 and "-h" notin result and "--help" notin result:
echo &"""# Generated @ {$now()}
# Command line:
# {getAppFilename()} {result.join(" ")}
"""
else:
result = cmdLine
when isMainModule:
# Setup cligen command line help and short flags
import cligen
@ -223,7 +245,7 @@ when isMainModule:
"preprocess": "run preprocessor on header",
"recurse": "process #include files",
"replace": "replace X with Y in identifiers, X1=Y1,X2=Y2, @X for regex",
"source" : "C/C++ source/header",
"source" : "C/C++ source/header(s) and command line file(s)",
"stub": "stub out undefined type references as objects",
"suffix": "strip suffix from identifiers",
"symOverride": "skip generating specified symbols",

2
tests/toast.cfg Normal file
View file

@ -0,0 +1,2 @@
--preprocess
-nk -E=_