Make a script which accept command-line arguments

前端 未结 5 1458
长情又很酷
长情又很酷 2021-01-01 08:38

What is the correct syntax for running a Node.js script with command-line arguments on Linux or Mac?

To run the script with no arguments, I would simply use the comm

相关标签:
5条回答
  • 2021-01-01 09:12

    This simple node module is also helpfull: command-line-args

    It allows to define a simple definition:

    const optionDefinitions = [
      { name: 'verbose', alias: 'v', type: Boolean },
      { name: 'src', type: String, multiple: true, defaultOption: true },
      { name: 'timeout', alias: 't', type: Number }  
    ]
    

    It validates your options and allows you to access them in a simple way.

    0 讨论(0)
  • 2021-01-01 09:14

    If you want to do more sophisticated stuff, the following modules are really helpful:

    • yargs by Benjamin Coe
    • commander by TJ Holowaychuk
    • vorpal by David Caccavella
    • nopt by Isaac Schlueter

    And for fun

    • cli-table by Guillermo Rauch
    • node-multimeter by substack
    • chalk by Sindre Sorhus
    0 讨论(0)
  • 2021-01-01 09:15

    The arguments are stored in

    process.argv and to pass the arguments in command line please check below example:

    ex. in this example below i have used commander NPM Module. var args = require('commander')

    Options with commander are defined with the .option() method. The example below parses args and options from process.argv, leaving remaining args as the program.args array which were not consumed by options. here process.argv is 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 after executing.

    function list(val) {
        return val.split(',');
    }
    args.version('0.11.2')
        .option('-t, --tag [value]', 'tags to ignore', list, undefined)
        .parse(process.argv);
    

    here to take input from command-line, we have to execute .js file with -t and after that arguments separated by comma(,)incase of multiple arguments ex. : node example.js -t tagname here i have used list to process multiple command line arguments ,so that we can pass multiple command line arguments ex. node example.js -t tagname1, tagname2 so after this , all input passed as command line arguments will be available in array named args, so can use this array for your purpose and you can read more about it from here:-

    https://nodejs.org/docs/latest/api/process.html#process_process_argv

    or you can make use of the following modules :

    1. commander:-

    https://www.npmjs.com/package/commander

    1. yargs :-

    https://www.npmjs.com/package/yargs

    1. vorpal :-

    https://www.npmjs.com/package/vorpal

    0 讨论(0)
  • 2021-01-01 09:17

    Nomnom is another possible solution.

    0 讨论(0)
  • 2021-01-01 09:29

    See http://nodejs.org/docs/latest/api/process.html#process_process_argv

    In summary you'll run it like

    node stuff.js blah hee "whoohoo!"

    Then your arguments are available in process.argv

    0 讨论(0)
提交回复
热议问题