finiteloop
finiteloop

Reputation: 4496

Grunt Task That Can be Run With Non-Option Arguments

I'm trying to write a grunt task with an api identical to that of the unix mv command.

I'd like to be able to call it with grunt mv path/to/old/file path/to/new/file.

I was hoping I'd be able to do it by accessing node's process.argv, however when I call the task using this API, grunt tries to treat path/to/old/file as another task I'm trying to run, throws an error that Warning: Task "path/to/old/file" not found. Use --force to continue. and exits.

Is there a way to do what I'm trying to do?

Upvotes: 4

Views: 168

Answers (3)

Creynders
Creynders

Reputation: 4583

You can pass arguments to your tasks by separating them with ":", e.g.

grunt mv:path/to/old/file:path/to/new/file

It's definitely not ideal though. Inside your task you can get their values with this.args. See https://github.com/Grunt-generate/grunt-generate/blob/master/tasks/generate.js for an example. And here's the API doc: http://gruntjs.com/api/inside-tasks

Upvotes: 1

Gonza
Gonza

Reputation: 360

You should use process.argv from de documentation:

An array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.

In your case:

File: mv.js

console.log(process.argv[2]);
console.log(process.argv[3]);

Then if you run: node mv.js path/to/old/file path/to/new/file

This will output:

path/to/old/file
path/to/new/file

Upvotes: 0

Robin Pokorny
Robin Pokorny

Reputation: 10705

I think you are looking for --options parameter http://gruntjs.com/frequently-asked-questions#options.

So you can call it with grunt mv --from=path/to/old/file --to=path/to/new/file.

Upvotes: 1

Related Questions