[Service] Integrate DeviceHome and SignalingServer
[platform/framework/web/wrtjs.git] / device_home / node_modules / qrcode / bin / qrcode
1 #!/usr/bin/env node
2 var yargs = require('yargs')
3 var qr = require('../lib')
4
5 function save (file, text, options) {
6   qr.toFile(file, text, options, function (err, data) {
7     if (err) {
8       console.error('Error:', err.message)
9       process.exit(1)
10     }
11
12     console.log('saved qrcode to: ' + file + '\n')
13   })
14 }
15
16 function print (text, options) {
17   options.type = 'terminal'
18   qr.toString(text, options, function (err, text) {
19     if (err) {
20       console.error('Error:', err.message)
21       process.exit(1)
22     }
23
24     console.log(text)
25   })
26 }
27
28 function parseOptions (args) {
29   return {
30     version: args.qversion,
31     errorCorrectionLevel: args.error,
32     type: args.type,
33     maskPattern: args.mask,
34     margin: args.qzone,
35     width: args.width,
36     scale: args.scale,
37     color: {
38       light: args.lightcolor,
39       dark: args.darkcolor
40     }
41   }
42 }
43
44 function processInputs (text, opts) {
45   if (!text.length) {
46     yargs.showHelp()
47     process.exit(1)
48   }
49
50   if (opts.output) {
51     save(opts.output, text, parseOptions(opts))
52   } else {
53     print(text, parseOptions(opts))
54   }
55 }
56
57 var argv = yargs
58   .detectLocale(false)
59   .usage('Usage: $0 [options] <input string>')
60   .option('v', {
61     alias: 'qversion',
62     description: 'QR Code symbol version (1 - 40)',
63     group: 'QR Code options:',
64     type: 'number'
65   })
66   .option('e', {
67     alias: 'error',
68     description: 'Error correction level',
69     choices: ['L', 'M', 'Q', 'H'],
70     group: 'QR Code options:'
71   })
72   .option('m', {
73     alias: 'mask',
74     description: 'Mask pattern (0 - 7)',
75     group: 'QR Code options:',
76     type: 'number'
77   })
78   .option('t', {
79     alias: 'type',
80     description: 'Output type',
81     choices: ['png', 'svg', 'utf8'],
82     implies: 'output',
83     group: 'Renderer options:'
84   })
85   .option('w', {
86     alias: 'width',
87     description: 'Image width (px)',
88     conflicts: 'scale',
89     group: 'Renderer options:',
90     type: 'number'
91   })
92   .option('s', {
93     alias: 'scale',
94     description: 'Scale factor',
95     conflicts: 'width',
96     group: 'Renderer options:',
97     type: 'number'
98   })
99   .option('q', {
100     alias: 'qzone',
101     description: 'Quiet zone size',
102     group: 'Renderer options:',
103     type: 'number'
104   })
105   .option('l', {
106     alias: 'lightcolor',
107     description: 'Light RGBA hex color',
108     group: 'Renderer options:'
109   })
110   .option('d', {
111     alias: 'darkcolor',
112     description: 'Dark RGBA hex color',
113     group: 'Renderer options:'
114   })
115   .option('o', {
116     alias: 'output',
117     description: 'Output file'
118   })
119   .help('h')
120   .alias('h', 'help')
121   .version()
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})
126   .argv
127
128 if (process.stdin.isTTY) {
129   processInputs(argv._.join(' '), argv)
130 } else {
131   var text = ''
132   process.stdin.setEncoding('utf8')
133   process.stdin.on('readable', function () {
134     var chunk = process.stdin.read()
135     if (chunk !== null) {
136       text += chunk
137     }
138   })
139
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)
144   })
145 }