我在 Node.js 中使用 Commander 时遇到了一些问题:parseInt 在我的代码中无法正常工作:
commander = require 'commander'
#parseInt = (str) => parseInt str #I tried to include this line but not work.
commander
.option '-n, --connection [n]', 'number of connection', parseInt, 5000
.option '-m, --message [n]', 'number of messages', parseInt, 5000
.parse process.argv
console.log commander.connection
console.log commander.message
当我使用选项 -n 10000 -m 10000 时,控制台会生成:
NaN
NaN
我还注意到这个代码与课堂作业:
commander = require 'commander'
class MyCommand
parseOpt: =>
commander
.option '-n, --connection [n]', 'number of connection', @parseInt, 5000
.option '-m, --message [n]', 'number of messages', @parseInt, 5000
.parse process.argv
(@connection, @message} = commander
run: =>
@parseOpt()
console.log @connection
console.log @message
parseInt: (str) => parseInt str
new MyCommand().run()
为什么我的代码在“类”代码工作时不起作用?如何在不使用类的情况下使我的代码工作?谢谢~