2012-07-21 34 views
1

我刚开始使用Node.js和受commander.js所以它可能是一个愚蠢的问题...Commander.js:脚本没有等待用户输入

所以我想做一个命令行工具我需要向用户询问一些信息。我试图使用commander.js,因为它看起来很简单。 问题是,当我运行脚本时,它不会等待用户键入答案,并执行所有脚本。 我该如何做这项工作?

这是我如何安排我的代码:

#!/usr/bin/env node 

var program = require('commander'); 

program 
    .version('0.0.1') 
    .option('-c, --create', 'Create new stuff') 
    .parse(process.argv); 

if(program.create){ 
    console.log('Creating new stuff'); 

    program.prompt('Name of the stuff: ', function(name){ 
     var stuffName = name; 
    }); 

    program.prompt('Description of the stuff: ', function(description){ 
     var stuffDescription = description; 
    }); 
} 

感谢

回答

0

你必须把第二个提示的回调中从第一个。

program.prompt('Name of the stuff: ', function(name){ 
    var stuffName = name; 
    program.prompt('Description of the stuff: ', function(description){ 
     var stuffDescription = description; 
    }); 

}); 

如果您不这样做,那么两个提示都会立即打印出来;该API是异步的。

+0

非常感谢! – romainberger 2012-07-21 21:22:26