summary refs log tree commit diff stats
path: root/lib/pure/parseopt.nim
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 /lib/pure/parseopt.nim
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.
Diffstat (limited to 'lib/pure/parseopt.nim')
-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!
l.com> 2021-01-15 18:16:24 +0000 committer GitHub <noreply@github.com> 2021-01-15 18:16:24 +0000 Fix 16722 (#16730)' href='/ahoang/Nim/commit/tests/arc/t14383.nim?h=devel&id=fc9cf2088d8ba969115a335239d57c05fbee9ad4'>fc9cf2088 ^
ccb11a63f ^














































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217