Reputation: 14667
How do I pass command line arguments to a nimble task? For example, say I have the task
task mytask, "my task":
echo &"my task {args}"
When I run
nimble mytask --foo --bar
I would like nimble to output
mytask --foo --bar
or something like that.
Upvotes: 5
Views: 860
Reputation: 4953
Passing just the right arguments:
proc forward_args(task_name: string): seq[string] =
let args = command_line_params()
let arg_start = args.find(task_name) + 1
return args[arg_start..^1]
task dev, "Start the project in dev mode":
const params = forward_args("dev").join(" ")
exec &"nimble run {bin[0]} {params} --silent"
Upvotes: 1
Reputation: 1473
from nimble documentation (i.e. github's README.md):
Tasks support two kinds of flags:
nimble <compflags> task <runflags>
. Compile flags are those specified before the task name and are forwarded to the Nim compiler that runs the .nimble task. This enables setting--define:xxx
values that can be checked with whendefined(xxx)
in the task, and other compiler flags that are applicable in Nimscript mode. Run flags are those after the task name and are available as command-line arguments to the task. They can be accessed per usual fromcommandLineParams: seq[string]
.
commandLineParams is available in std/os
. For your example:
import std / [os, strformat]
task mytask, "my task":
echo &"my task {commandLineParams()}"
Update:
Setting up a new nimble project with the above code added and running:
nimble mytask --foo --bar
you will actually find that it prints a nim sequence with ALL arguments and not only the runtime flags. For example on Windows and anonymizing specific folder names:
my task @["e", "--hints:off", "--verbosity:0", "--colors:on", "XXX\\nimblecache-0\\test_nimble_2483249703\\test_nimble.nims", "XXY\\test_nimble\\test_nimble.nimble", "XXZ\\nimble_23136.out", "mytask", "--foo", "--bar"]
So in order to get only --foo
and --bar
you need to select arguments after mytask
Note: we probably should fix nimble docs about that.
Upvotes: 3