summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorGianmarco <gim.marcello@gmail.com>2024-07-02 08:49:21 +0200
committerGitHub <noreply@github.com>2024-07-02 08:49:21 +0200
commit96aba18963cbcf87250f93b453122d411aeb1d17 (patch)
tree90cd070fdafdc68f44b0dd80def7d2a00f84fa1f
parent43ee545789f94ca36c9f48012f0d1c8682b39756 (diff)
downloadNim-96aba18963cbcf87250f93b453122d411aeb1d17.tar.gz
Fixed issues when using `std/parseopt` in miscripts with cmdline = "" (#23785)
Using initOptParser with an empty cmdline (so that it gets the cmdline
from the command line) in nimscripts does not yield the expected
results.

Fixes #23774.
-rw-r--r--lib/pure/parseopt.nim18
1 files changed, 15 insertions, 3 deletions
diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim
index 24c903b58..03f151b66 100644
--- a/lib/pure/parseopt.nim
+++ b/lib/pure/parseopt.nim
@@ -176,6 +176,7 @@
 
 include "system/inclrtl"
 
+import std/strutils
 import std/os
 
 type
@@ -249,9 +250,20 @@ proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
       result.cmds[i] = cmdline[i]
   else:
     when declared(paramCount):
-      result.cmds = newSeq[string](paramCount())
-      for i in countup(1, paramCount()):
-        result.cmds[i-1] = paramStr(i)
+      when defined(nimscript):
+        var ctr = 0
+        var firstNimsFound = false
+        for i in countup(0, paramCount()):
+          if firstNimsFound: 
+            result.cmds[ctr] = paramStr(i)
+            inc ctr, 1
+          if paramStr(i).endsWith(".nims") and not firstNimsFound:
+            firstNimsFound = true 
+            result.cmds = newSeq[string](paramCount()-i)
+      else:
+        result.cmds = newSeq[string](paramCount())
+        for i in countup(1, paramCount()):
+          result.cmds[i-1] = paramStr(i)
     else:
       # we cannot provide this for NimRtl creation on Posix, because we can't
       # access the command line arguments then!