我的应用上有以下代码:
import commander = require('commander');
commander
.option('-u, --user [user]', 'user code')
.option('-p, --pass [pass]', 'pass code')
.parse(process.argv);
然后,我尝试访问:
commander.user
但是我收到一个错误(DefinitelyTyped中的commander.d.ts):
user does not exist on type IExportedCommand
我尝试添加这个
interface IExportedCommand {
user: string;
pass: string;
}
但是我仍然得到错误。我怎样才能解决这个问题?
最佳答案
使用以下命令创建文件commander-expansion.d.ts
:
declare namespace commander {
interface IExportedCommand extends ICommand {
user: string;
pass: string;
}
}
小费
因为我最近做了类似的事情,所以推荐
--auth user:password
。节省您处理缺少用户名或密码的用户的麻烦。但是禁止使用:
作为密码属性¯\_(ツ)_/¯
更多:https://github.com/alm-tools/alm/blob/master/src/server/commandLine.ts#L24