How do I get console input in javascript?

前端 未结 4 1906
独厮守ぢ
独厮守ぢ 2020-12-04 17:21

I\'m currently using spidermonkey to run my JavaScript code. I\'m wondering if there\'s a function to get input from the console similar to how Python does this:

<         


        
相关标签:
4条回答
  • 2020-12-04 17:53

    You can try something like process.argv, that is if you are using node.js to run the program.
    console.log(process.argv) => Would print an array containing

    [                                                                                                                                                                                          
      '/usr/bin/node',                                                                                                                                                                         
      '/home/user/path/filename.js',                                                                                                                                            
      'your_input'                                                                                                                                                                                   
    ]
    

    You get the user provided input via array index, i.e., console.log(process.argv[3]) This should provide you with the input which you can store.


    Example:

    var somevariable = process.argv[3]; // input one
    var somevariable2 = process.argv[4]; // input two
    
    console.log(somevariable);
    console.log(somevariable2);
    

    If you are building a command-line program then the npm package yargs would be really helpful.

    0 讨论(0)
  • 2020-12-04 18:05

    In plain JavaScript, simply use response = readline() after printing a prompt.

    In Node.js, you'll need to use the readline module: const readline = require('readline')

    0 讨论(0)
  • 2020-12-04 18:06

    Good old readline();

    See MDN docs: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Introduction_to_the_JavaScript_shell#readline.28.29

    0 讨论(0)
  • 2020-12-04 18:13

    As you mentioned, prompt works for browsers all the way back to IE:

    var answer = prompt('question', 'defaultAnswer');
    

    For Node.js > v7.6, you can use console-read-write, which is a wrapper around the low-level readline module:

    const io = require('console-read-write');
    
    async function main() {
      // Simple readline scenario
      io.write('I will echo whatever you write!');
      io.write(await io.read());
    
      // Simple question scenario
      io.write(`hello ${await io.ask('Who are you?')}!`);
    
      // Since you are not blocking the IO, you can go wild with while loops!
      let saidHi = false;
      while (!saidHi) {
        io.write('Say hi or I will repeat...');
        saidHi = await io.read() === 'hi';
      }
    
      io.write('Thanks! Now you may leave.');
    }
    
    main();
    // I will echo whatever you write!
    // > ok
    // ok
    // Who are you? someone
    // hello someone!
    // Say hi or I will repeat...
    // > no
    // Say hi or I will repeat...
    // > ok
    // Say hi or I will repeat...
    // > hi
    // Thanks! Now you may leave.
    

    Disclosure I'm author and maintainer of console-read-write

    For SpiderMonkey, simple readline as suggested by @MooGoo and @Zaz.

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