Closed Benjamin-Lee closed 4 years ago
Definitely!
I am not familiar with using Docopt with Nim but this is definitely possible if you use a small (1 file) Python wrapper around the Nim code.
Basically, you have to pass the CLI arguments from Python sys.argv
to a Nim function that runs your CLI handler code.
Example:
import sys, nimporter, nim_cli
def main(args=[]):
return nim_cli.run_cli(args)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Inside your Nim file, you would just have a function that can accept CLI arguments and run with them:
# nim_cli.nim
import nimpy
proc run_cli(args: seq[string]): int {.exportpy.} =
echo args
return -1
Let me know if you have any other questions and I will be happy to help!
Thanks for the suggestion, that's exactly what I needed. For reference for anyone else having the issue, I solved the problem using the Python docopt package and just passed the parsed args in. All of my argument validation etc which I had done in Nim worked just as before.
When compiling a CLI created in Nim using docopt, I get the following error:
This appears to be because functions like
paramCount
don't work in dynamic libraries. So, is there way to have pure Nim CLIs distributed in Python? Or do I need to rewrite the CLI in Python?