Spaces:
Sleeping
Sleeping
File size: 3,640 Bytes
a23aa7d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | #!/usr/bin/env node
var yargs = require('yargs')
var qr = require('../lib')
function save (file, text, options) {
qr.toFile(file, text, options, function (err, data) {
if (err) {
console.error('Error:', err.message)
process.exit(1)
}
console.log('saved qrcode to: ' + file + '\n')
})
}
function print (text, options) {
options.type = 'terminal'
qr.toString(text, options, function (err, text) {
if (err) {
console.error('Error:', err.message)
process.exit(1)
}
console.log(text)
})
}
function parseOptions (args) {
return {
version: args.qversion,
errorCorrectionLevel: args.error,
type: args.type,
small: !!args.small,
inverse: !!args.inverse,
maskPattern: args.mask,
margin: args.qzone,
width: args.width,
scale: args.scale,
color: {
light: args.lightcolor,
dark: args.darkcolor
}
}
}
function processInputs (text, opts) {
if (!text.length) {
yargs.showHelp()
process.exit(1)
}
if (opts.output) {
save(opts.output, text, parseOptions(opts))
} else {
print(text, parseOptions(opts))
}
}
var argv = yargs
.detectLocale(false)
.usage('Usage: $0 [options] <input string>')
.option('v', {
alias: 'qversion',
description: 'QR Code symbol version (1 - 40)',
group: 'QR Code options:',
type: 'number'
})
.option('e', {
alias: 'error',
description: 'Error correction level',
choices: ['L', 'M', 'Q', 'H'],
group: 'QR Code options:'
})
.option('m', {
alias: 'mask',
description: 'Mask pattern (0 - 7)',
group: 'QR Code options:',
type: 'number'
})
.option('t', {
alias: 'type',
description: 'Output type',
choices: ['png', 'svg', 'utf8'],
implies: 'output',
group: 'Renderer options:'
})
.option('i', {
alias: 'inverse',
type: 'boolean',
description: 'Invert colors',
group: 'Renderer options:'
})
.option('w', {
alias: 'width',
description: 'Image width (px)',
conflicts: 'scale',
group: 'Renderer options:',
type: 'number'
})
.option('s', {
alias: 'scale',
description: 'Scale factor',
conflicts: 'width',
group: 'Renderer options:',
type: 'number'
})
.option('q', {
alias: 'qzone',
description: 'Quiet zone size',
group: 'Renderer options:',
type: 'number'
})
.option('l', {
alias: 'lightcolor',
description: 'Light RGBA hex color',
group: 'Renderer options:'
})
.option('d', {
alias: 'darkcolor',
description: 'Dark RGBA hex color',
group: 'Renderer options:'
})
.option('small', {
type: 'boolean',
description: 'Output smaller QR code to terminal',
conflicts: 'type',
group: 'Renderer options:'
})
.option('o', {
alias: 'output',
description: 'Output file'
})
.help('h')
.alias('h', 'help')
.version()
.example('$0 "some text"', 'Draw in terminal window')
.example('$0 -o out.png "some text"', 'Save as png image')
.example('$0 -d F00 -o out.png "some text"', 'Use red as foreground color')
.parserConfiguration({'parse-numbers': false})
.argv
if (process.stdin.isTTY) {
processInputs(argv._.join(' '), argv)
} else {
var text = ''
process.stdin.setEncoding('utf8')
process.stdin.on('readable', function () {
var chunk = process.stdin.read()
if (chunk !== null) {
text += chunk
}
})
process.stdin.on('end', function () {
// this process can be run as a command outside of a tty so if there was no
// data on stdin read from argv
processInputs(text.length?text:argv._.join(' '), argv)
})
}
|