Skip to content

Commit 2a3c176

Browse files
committed
std/cmdline.commandLineParams NimScript fix
Now consistently returns only the script arguments, skipping compiler arguments.
1 parent 9ed4077 commit 2a3c176

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ errors.
3535

3636
- Adds a new warning `--warning:ImplicitRangeConversion` that detects downsizing implicit conversions to range types (e.g., `int -> range[0..255]` or `range[1..256] -> range[0..255]`) that could cause runtime panics. Safe conversions like `range[0..255] -> range[0..65535]` and explicit casts do not trigger warnings. `int` to `Natural` and `Positive` conversions do not trigger warnings, which can be enabled with `--warning:systemRangeConversion`.
3737

38+
39+
- `std/cmdline.commandLineParams()` for NimScript now consistently returns only the script arguments (excluding the `nim` executable and its flags), fixing inconsistency between `std/cmdline` and `std/parseopt`.
40+
41+
3842
## Standard library additions and changes
3943

4044
[//]: # "Additions:"

lib/std/cmdline.nim

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,14 @@ when declared(paramCount) or defined(nimdoc):
279279
proc commandLineParams*(): seq[string] =
280280
## Convenience proc which returns the command line parameters.
281281
##
282-
## This returns **only** the parameters. If you want to get the application
282+
## Unlike `paramStr proc`_, returns **only** the parameters.
283+
## If you want to get the application
283284
## executable filename, call `getAppFilename() <os.html#getAppFilename>`_.
284285
##
286+
## When used from NimScript, arguments preceding and including the `.nims`
287+
## file are also excluded, returning only the arguments intended for the
288+
## script.
289+
##
285290
## **Availability**: On Posix there is no portable way to get the command
286291
## line from a DLL and thus the proc isn't defined in this environment. You
287292
## can test for its availability with `declared()
@@ -303,8 +308,23 @@ when declared(paramCount) or defined(nimdoc):
303308
## # Do something else!
304309
## ```
305310
result = @[]
306-
for i in 1..paramCount():
307-
result.add(paramStr(i))
311+
when defined(nimscript):
312+
func isNimScriptExt(s: openArray[char]): bool =
313+
let L = s.len
314+
(L >= 5 and s[L-5] == '.' and (
315+
(s[L-4] in {'n', 'N'}) and
316+
(s[L-3] in {'i', 'I'}) and
317+
(s[L-2] in {'m', 'M'}) and
318+
(s[L-1] in {'s', 'S'})))
319+
var firstNimsFound = false
320+
for i in 0..paramCount(): # nimscript needs to check index 0
321+
if firstNimsFound:
322+
result.add(paramStr(i))
323+
elif isNimScriptExt(paramStr(i)):
324+
firstNimsFound = true
325+
else:
326+
for i in 1..paramCount():
327+
result.add(paramStr(i))
308328
else:
309329
proc commandLineParams*(): seq[string] {.error:
310330
"commandLineParams() unsupported by dynamic libraries".} =

0 commit comments

Comments
 (0)