inim-repl / INim

Interactive Nim shell / REPL / Playground
MIT License
642 stars 23 forks source link

[feature] `inim -s:foo.nim -- args...` => passes args to script foo.nim #35

Open timotheecour opened 6 years ago

timotheecour commented 6 years ago

proposal

inim -s:foo.nim -- arg1 arg2 # passes arguments arg1, arg2 to script foo.nim (eg see [1])
cat foo.nim | inim -- arg1 arg2
inim -- arg1 arg2 # now typing `echo paramStr(1)` on inim prompt would print `arg1`

The -- is needed

it avoids unnecessary ambiguities (and makes it easy to parse visually which is an argument to inim, which is an argument to the script) eg: with -- required:

inim -s:foo.nim -- -h # -h passed to foo
inim -s:foo.nim -h -- # -h passed to inim; same as: `inim -s:foo.nim -h`
cat foo.nim | inim -- -s:bar.nim # -s:bar.nim bassed to foo (contrived example...)

if -- were not required:

inim -s:foo.nim -h # ambiguous: is -h a inim argument or an argument for foo? this ambiguity affects programs like grep which add a `-e` to disambiguate when patterns begin with `-`
cat foo.nim | inim -s:bar.nim # ambiguous

here's how other programs handle passing arguments:

[1] example for foo.nim: bugs/inim/t03_cmdline.nim:

import os
for i in 1..paramCount():
  echo (i, paramStr(i))
Tangdongle commented 4 years ago

It looks like it might be difficult to do this with cligen and use the -- parameter. AFAIK, each argument to inim needs to have a corresponding argument in the dispatching function, where -- would be an illegal parameter name. It might be possible to add an "extra args" kind of parameter (like -a or --scriptArgs) that will in turn pass it to the nim compiler when compiling the INim buffer (in which the srcFile is injected on startup)