2 var yargs = require('yargs')
3 var qr = require('../lib')
5 function save (file, text, options) {
6 qr.toFile(file, text, options, function (err, data) {
8 console.error('Error:', err.message)
12 console.log('saved qrcode to: ' + file + '\n')
16 function print (text, options) {
17 options.type = 'terminal'
18 qr.toString(text, options, function (err, text) {
20 console.error('Error:', err.message)
28 function parseOptions (args) {
30 version: args.qversion,
31 errorCorrectionLevel: args.error,
33 maskPattern: args.mask,
38 light: args.lightcolor,
44 function processInputs (text, opts) {
51 save(opts.output, text, parseOptions(opts))
53 print(text, parseOptions(opts))
59 .usage('Usage: $0 [options] <input string>')
62 description: 'QR Code symbol version (1 - 40)',
63 group: 'QR Code options:',
68 description: 'Error correction level',
69 choices: ['L', 'M', 'Q', 'H'],
70 group: 'QR Code options:'
74 description: 'Mask pattern (0 - 7)',
75 group: 'QR Code options:',
80 description: 'Output type',
81 choices: ['png', 'svg', 'utf8'],
83 group: 'Renderer options:'
87 description: 'Image width (px)',
89 group: 'Renderer options:',
94 description: 'Scale factor',
96 group: 'Renderer options:',
101 description: 'Quiet zone size',
102 group: 'Renderer options:',
107 description: 'Light RGBA hex color',
108 group: 'Renderer options:'
112 description: 'Dark RGBA hex color',
113 group: 'Renderer options:'
117 description: 'Output file'
122 .example('$0 "some text"', 'Draw in terminal window')
123 .example('$0 -o out.png "some text"', 'Save as png image')
124 .example('$0 -d F00 -o out.png "some text"', 'Use red as foreground color')
125 .parserConfiguration({'parse-numbers': false})
128 if (process.stdin.isTTY) {
129 processInputs(argv._.join(' '), argv)
132 process.stdin.setEncoding('utf8')
133 process.stdin.on('readable', function () {
134 var chunk = process.stdin.read()
135 if (chunk !== null) {
140 process.stdin.on('end', function () {
141 // this process can be run as a command outside of a tty so if there was no
142 // data on stdin read from argv
143 processInputs(text.length?text:argv._.join(' '), argv)