qrcode 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. maskPattern: args.mask,
  29. margin: args.qzone,
  30. width: args.width,
  31. scale: args.scale,
  32. color: {
  33. light: args.lightcolor,
  34. dark: args.darkcolor
  35. }
  36. }
  37. }
  38. function processInputs (text, opts) {
  39. if (!text.length) {
  40. yargs.showHelp()
  41. process.exit(1)
  42. }
  43. if (opts.output) {
  44. save(opts.output, text, parseOptions(opts))
  45. } else {
  46. print(text, parseOptions(opts))
  47. }
  48. }
  49. var argv = yargs
  50. .detectLocale(false)
  51. .usage('Usage: $0 [options] <input string>')
  52. .option('v', {
  53. alias: 'qversion',
  54. description: 'QR Code symbol version (1 - 40)',
  55. group: 'QR Code options:',
  56. type: 'number'
  57. })
  58. .option('e', {
  59. alias: 'error',
  60. description: 'Error correction level',
  61. choices: ['L', 'M', 'Q', 'H'],
  62. group: 'QR Code options:'
  63. })
  64. .option('m', {
  65. alias: 'mask',
  66. description: 'Mask pattern (0 - 7)',
  67. group: 'QR Code options:',
  68. type: 'number'
  69. })
  70. .option('t', {
  71. alias: 'type',
  72. description: 'Output type',
  73. choices: ['png', 'svg', 'utf8'],
  74. implies: 'output',
  75. group: 'Renderer options:'
  76. })
  77. .option('w', {
  78. alias: 'width',
  79. description: 'Image width (px)',
  80. conflicts: 'scale',
  81. group: 'Renderer options:',
  82. type: 'number'
  83. })
  84. .option('s', {
  85. alias: 'scale',
  86. description: 'Scale factor',
  87. conflicts: 'width',
  88. group: 'Renderer options:',
  89. type: 'number'
  90. })
  91. .option('q', {
  92. alias: 'qzone',
  93. description: 'Quiet zone size',
  94. group: 'Renderer options:',
  95. type: 'number'
  96. })
  97. .option('l', {
  98. alias: 'lightcolor',
  99. description: 'Light RGBA hex color',
  100. group: 'Renderer options:'
  101. })
  102. .option('d', {
  103. alias: 'darkcolor',
  104. description: 'Dark RGBA hex color',
  105. group: 'Renderer options:'
  106. })
  107. .option('o', {
  108. alias: 'output',
  109. description: 'Output file'
  110. })
  111. .help('h')
  112. .alias('h', 'help')
  113. .version()
  114. .example('$0 "some text"', 'Draw in terminal window')
  115. .example('$0 -o out.png "some text"', 'Save as png image')
  116. .example('$0 -d F00 -o out.png "some text"', 'Use red as foreground color')
  117. .parserConfiguration({'parse-numbers': false})
  118. .argv
  119. if (process.stdin.isTTY) {
  120. processInputs(argv._.join(' '), argv)
  121. } else {
  122. var text = ''
  123. process.stdin.setEncoding('utf8')
  124. process.stdin.on('readable', function () {
  125. var chunk = process.stdin.read()
  126. if (chunk !== null) {
  127. text += chunk
  128. }
  129. })
  130. process.stdin.on('end', function () {
  131. // this process can be run as a command outside of a tty so if there was no
  132. // data on stdin read from argv
  133. processInputs(text.length?text:argv._.join(' '), argv)
  134. })
  135. }