Skip to content

Commit 919e201

Browse files
shunf4dom96
authored andcommitted
Handle flags correctly for nimble run
1. check for `isGlobalFlag` before `setRunOptions`. Flags like `--debug` should not be treated as `run` options. 2. in `handleUnknownFlags`, add unknown flags to `compileFlags` instead of overwriting `compileFlags`. This enables compilation flags after the `run` command to be correctly passed.
1 parent ce17300 commit 919e201

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

src/nimblepkg/options.nim

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,8 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
478478
result.action.compileOptions.add(getFlagString(kind, flag, val))
479479
of actionRun:
480480
result.showHelp = false
481-
result.setRunOptions(flag, getFlagString(kind, flag, val), false)
481+
if not isGlobalFlag:
482+
result.setRunOptions(flag, getFlagString(kind, flag, val), false)
482483
of actionCustom:
483484
if not isGlobalFlag:
484485
if result.action.command.normalize == "test":
@@ -517,9 +518,11 @@ proc parseMisc(options: var Options) =
517518

518519
proc handleUnknownFlags(options: var Options) =
519520
if options.action.typ == actionRun:
520-
# actionRun uses flags that come before the command as compilation flags.
521-
options.action.compileFlags =
521+
# In addition to flags that come after the command before binary,
522+
# actionRun also uses flags that come before the command as compilation flags.
523+
options.action.compileFlags.insert(
522524
map(options.unknownFlags, x => getFlagString(x[0], x[1], x[2]))
525+
)
523526
options.unknownFlags = @[]
524527
elif options.action.typ == actionCustom:
525528
# actionCustom uses flags that come before the command as compilation flags

tests/run/src/run.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ import os
22

33
when isMainModule:
44
echo("Testing `nimble run`: ", commandLineParams())
5+
when defined(sayWhee):
6+
echo "Whee!"

tests/tester.nim

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ suite "nimble run":
804804
"""Testing `nimble run`: @["\"", "\'", "\t", "arg with spaces"]"""
805805
)
806806

807-
test "Compile flags before executable name":
807+
test "Nimble options before executable name":
808808
cd "run":
809809
let (output, exitCode) = execNimble(
810810
"run", # Run command invokation
@@ -818,7 +818,7 @@ suite "nimble run":
818818
echo output
819819
check output.contains("""Testing `nimble run`: @["--test"]""")
820820

821-
test "Compile flags before --":
821+
test "Nimble options before --":
822822
cd "run":
823823
let (output, exitCode) = execNimble(
824824
"run", # Run command invokation
@@ -832,6 +832,72 @@ suite "nimble run":
832832
echo output
833833
check output.contains("""Testing `nimble run`: @["--test"]""")
834834

835+
test "Compilation flags before run command":
836+
cd "run":
837+
let (output, exitCode) = execNimble(
838+
"-d:sayWhee", # Compile flag to define a conditional symbol
839+
"run", # Run command invokation
840+
"--debug", # Flag to enable debug verbosity in Nimble
841+
"--", # Separator for arguments
842+
"--test" # First argument passed to the executed command
843+
)
844+
check exitCode == QuitSuccess
845+
check output.contains("tests$1run$1$2 --test" %
846+
[$DirSep, "run".changeFileExt(ExeExt)])
847+
echo output
848+
check output.contains("""Testing `nimble run`: @["--test"]""")
849+
check output.contains("""Whee!""")
850+
851+
test "Compilation flags before executable name":
852+
cd "run":
853+
let (output, exitCode) = execNimble(
854+
"--debug", # Flag to enable debug verbosity in Nimble
855+
"run", # Run command invokation
856+
"-d:sayWhee", # Compile flag to define a conditional symbol
857+
"run", # The executable to run
858+
"--test" # First argument passed to the executed command
859+
)
860+
check exitCode == QuitSuccess
861+
check output.contains("tests$1run$1$2 --test" %
862+
[$DirSep, "run".changeFileExt(ExeExt)])
863+
echo output
864+
check output.contains("""Testing `nimble run`: @["--test"]""")
865+
check output.contains("""Whee!""")
866+
867+
test "Compilation flags before --":
868+
cd "run":
869+
let (output, exitCode) = execNimble(
870+
"run", # Run command invokation
871+
"-d:sayWhee", # Compile flag to define a conditional symbol
872+
"--debug", # Flag to enable debug verbosity in Nimble
873+
"--", # Separator for arguments
874+
"--test" # First argument passed to the executed command
875+
)
876+
check exitCode == QuitSuccess
877+
check output.contains("tests$1run$1$2 --test" %
878+
[$DirSep, "run".changeFileExt(ExeExt)])
879+
echo output
880+
check output.contains("""Testing `nimble run`: @["--test"]""")
881+
check output.contains("""Whee!""")
882+
883+
test "Order of compilation flags before and after run command":
884+
cd "run":
885+
let (output, exitCode) = execNimble(
886+
"-d:compileFlagBeforeRunCommand", # Compile flag to define a conditional symbol
887+
"run", # Run command invokation
888+
"-d:sayWhee", # Compile flag to define a conditional symbol
889+
"--debug", # Flag to enable debug verbosity in Nimble
890+
"--", # Separator for arguments
891+
"--test" # First argument passed to the executed command
892+
)
893+
check exitCode == QuitSuccess
894+
check output.contains("-d:compileFlagBeforeRunCommand -d:sayWhee")
895+
check output.contains("tests$1run$1$2 --test" %
896+
[$DirSep, "run".changeFileExt(ExeExt)])
897+
echo output
898+
check output.contains("""Testing `nimble run`: @["--test"]""")
899+
check output.contains("""Whee!""")
900+
835901
suite "project local deps mode":
836902
beforeSuite()
837903

0 commit comments

Comments
 (0)