5

I have a javascript file with a function that receives an Array with random numbers and returns a solution, I need to be able to run this function from the Command Line.

What i want is to be able to type something like: myFunction 1,2,3,4,5,6,7

9
  • you could use nodejs I guess Commented Jan 22, 2018 at 4:23
  • you are saying that you need run that function from browser console right? Commented Jan 22, 2018 at 4:26
  • 1
    Possible duplicate of How do I load my script into the node.js REPL? Commented Jan 22, 2018 at 4:27
  • Which command line are you referencing? Commented Jan 22, 2018 at 5:08
  • @iSkore Why did you add node.js tag to the Question? Commented Jan 22, 2018 at 5:30

4 Answers 4

3

You can use Node.js for JavaScript CLI.

You can pass arguments like this -

node calendar.js --year 2020 --month 7

Using above command process.argv[] array will have this value -

['node', 'calendar.js', '--year', '2020', '--month', '7']

In your code, it can be read using array process.argv[] like this -

var x = +process.argv[2];  //For third argument
var y = +process.argv[3];  //For fourth argument

1st and 2nd arguments will be node and calendar.js respectively

Sign up to request clarification or add additional context in comments.

1 Comment

What is + in +process.argv[2]; when calling the cmd arguments?
1

Export the function from your file:

module.exports = function myFunction(){
    // ...
}

and then use that exported module in your command line with Node's REPL by first running node then executing:

> const myFunction = require('./path/to/the/file-containing-myFunction')`

after which you'll be able to use myFunction like so:

> myFunction()

Comments

1

Considering NodeJS, create a wrapper script where your script with myFunction is imported (would have to export it with module.exports.myFunction = myFunction in your original file first), then pass it the arguments from process.args, skipping the 1st element (as it's always path of the script):

// argsrun.js
var myFunction = require('thescript.js').myFunction;
myFunction(process.args.slice(2));

and call it from CLI:

node argsrun.js 1 2 3 4

1 Comment

process.args should be process.argv
0

The demo

'use strict';

function add( n1, n2 ) {
    return n1 + n2;
}

function subtract( n1, n2 ) {
    return n1 - n2;
}

On browser:

Invoke the function calling:

window.add( 1, 2 );
window.subtract( 1, 2 );

With Node.js:

Invoke the function from the command line:

Note: this is only for development purposes

Do not use eval in production

Put this at the bottom of the file:

eval( process.argv[ 2 ] )

And call your function by doing:

node index.js "add( 1, 2 )"
node index.js "subtract( 1, 2 )"

with your terminal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.