How to take in text input from a keyboard and store it into a variable?

前端 未结 7 2091
清酒与你
清酒与你 2020-12-30 05:43

I would just like something simple to read text from a keyboard and store it into a variable. So for:

var color = \'blue\'

I would like the

7条回答
  •  悲&欢浪女
    2020-12-30 06:09

    You can use stdio for this. It is as simple as follows:

    import { ask } from 'stdio';
    const color = await ask('What is your keyboard color?');
    

    This module includes retries if you decide to accept only some predefined answers:

    import { ask } from 'stdio';
    const color = await ask('What is your keyboard color?', { options: ['red', 'blue', 'orange'], maxRetries: 3 });
    

    Take a look at stdio, it includes other features that may be useful for you (like command-line arguments parsing, standard input reading at once or by lines...).

提交回复
热议问题