javascript - 在节点脚本中包含“无颜色”选项?
问题描述
我制作了一个节点脚本,并通过在我的console.logs
:
const noColorOption = args[1] === '--no-color' || args[2] === '--no-color';
const colors = {
green: noColorOption ? '' : '\x1b[32m%s\x1b[0m',
cyanRed: noColorOption ? '' : '\x1b[36m%s\x1b[91m%s\x1b[0m'
};
// ... examples of console.logs in my script ...
console.log(colors.cyanRed,
filename + '\n ',
redundantModules.join('\n '));
console.log(colors.green, `\nTotal files searched: ${totalFilesSearched}`);
但是,该--no-color
选项无法按预期工作,因为它console.log
只是将空字符串打印为空格。
我应该console.logs
在没有第一个参数的情况下添加新的,还是有办法分配--no-color
选项以使其正确打印为默认颜色?
解决方案
您可以使用%s
而不是空字符串。
const noColorOption = process.argv[2] === '--no-color';
const colors = {
green: noColorOption ? '%s' : '\x1b[32m%s\x1b[0m',
cyanRed: noColorOption ? '%s' : '\x1b[36m%s\x1b[91m%s\x1b[0m'
};
console.log(colors.cyanRed, __filename + '\n ');
console.log(colors.green, `Total files searched: 0`);
推荐阅读
- django - 带有 apache 的 wsgi 无法读取管道安装的 python 包
- django - Django 发生错误时如何将用户信息添加到 Sentry 的报告中?
- apache-kafka - Confluent Schema Registry 超时错误
- python - 如何使用链表中存在的变量在链表类之外的函数中调用函数并接收参数
- c - 我可以创建一个有条件地“goto”并用作表达式的宏吗?
- python - 附加两个csv文件时如何修复pandas concat
- html - Angular:将div的高度与组件内的div匹配
- c++ - 一些奇怪的异常抛出
- typescript - 使用数组中对象的某个属性的值创建类型?
- paypal - 如何解决错误 Paypal 代码没有业务