mirror of https://github.com/jkjoy/sunpeiwen.git
75 lines
2.1 KiB
Plaintext
75 lines
2.1 KiB
Plaintext
|
#!/usr/bin/env node
|
|||
|
'use strict';
|
|||
|
|
|||
|
var _yargs = require('yargs');
|
|||
|
|
|||
|
var _yargs2 = _interopRequireDefault(_yargs);
|
|||
|
|
|||
|
var _utils = require('../../lib/node/utils');
|
|||
|
|
|||
|
var _node = require('../../lib/node');
|
|||
|
|
|||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|||
|
|
|||
|
// eslint-disable-line import/no-unresolved
|
|||
|
|
|||
|
const STDIN_ENCODING = 'utf-8'; // eslint-disable-line import/no-unresolved
|
|||
|
|
|||
|
const options = {
|
|||
|
unknown: '[?]',
|
|||
|
replace: [],
|
|||
|
ignore: []
|
|||
|
};
|
|||
|
|
|||
|
const { argv } = _yargs2.default.version().usage('Usage: $0 <unicode> [options]').option('u', {
|
|||
|
alias: 'unknown',
|
|||
|
default: options.unknown,
|
|||
|
describe: 'Placeholder for unknown characters',
|
|||
|
type: 'string'
|
|||
|
}).option('r', {
|
|||
|
alias: 'replace',
|
|||
|
default: options.replace,
|
|||
|
describe: 'Custom string replacement',
|
|||
|
type: 'array'
|
|||
|
}).option('i', {
|
|||
|
alias: 'ignore',
|
|||
|
default: options.ignore,
|
|||
|
describe: 'String list to ignore',
|
|||
|
type: 'array'
|
|||
|
}).option('S', {
|
|||
|
alias: 'stdin',
|
|||
|
default: false,
|
|||
|
describe: 'Use stdin as input',
|
|||
|
type: 'boolean'
|
|||
|
}).help('h').option('h', {
|
|||
|
alias: 'help'
|
|||
|
}).example('$0 "你好, world!" -r 好=good -r "world=Shi Jie"', 'Replace `,` into `!`, `world` into `shijie`.\nResult: Ni good, Shi Jie!').example('$0 "你好,世界!" -i 你好 -i ,', 'Ignore `你好` and `,`.\nResult: 你好,Shi Jie !').wrap(100);
|
|||
|
|
|||
|
options.unknown = argv.u;
|
|||
|
if (argv.replace.length) {
|
|||
|
for (const repl of argv.replace) {
|
|||
|
const tmp = (0, _utils.parseCmdEqualOption)(repl);
|
|||
|
if (tmp === false) {
|
|||
|
console.error(`Bad argument -r or --replace. Please type '${argv.$0} --help' for help.`);
|
|||
|
process.exit(1);
|
|||
|
}
|
|||
|
options.replace.push(tmp);
|
|||
|
}
|
|||
|
}
|
|||
|
options.ignore = argv.ignore;
|
|||
|
|
|||
|
if (argv.stdin) {
|
|||
|
process.stdin.setEncoding(STDIN_ENCODING);
|
|||
|
process.stdin.on('readable', () => {
|
|||
|
const chunk = process.stdin.read();
|
|||
|
if (chunk !== null) {
|
|||
|
process.stdout.write((0, _node.transliterate)(chunk, options));
|
|||
|
}
|
|||
|
});
|
|||
|
process.stdin.on('end', () => console.log(''));
|
|||
|
} else {
|
|||
|
if (argv._.length !== 1) {
|
|||
|
console.error(`Invalid argument. Please type '${argv.$0} --help' for help.`);
|
|||
|
}
|
|||
|
console.log((0, _node.transliterate)(argv._[0], options));
|
|||
|
}
|