qrcode 3.6 KB

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