QR code command line tool

A colleague needed to make QR codes so I made a command line tool.

I installed simple-qr with raco package install,
then I searched for and installed the drracket-cmdline-args DrRacket plugin using graphical package manager in DrRacket so I could test it.
Once complete I used 'Create Executable' in DrRacket to create a version my colleague could use on their machine

#lang racket/base
(require simple-qr)
(define (main)
  (define (my-error text)
    (fprintf (current-error-port) "Error: ~a\n" text))
  (define args (current-command-line-arguments))
  (when (not (= (vector-length args) 2))
    (my-error "Usage: qr <url> <filename>")
    (exit 1))
  (define url-str (vector-ref args 0))
  (define filename (vector-ref args 1))
  (with-handlers ([exn:fail?
                   (lambda (e)
                     (fprintf (current-error-port)
                              "Error: ~a\n"
                              (exn-message e))
                     (exit 1))])
    (qr-write url-str filename)) ;(qr-write "https://www.something.com/" "myqr.png")
  (displayln (string-append "QR code for " url-str " to " filename " as PNG")))
(main)

Could not have done this without the Racket community :heart_eyes:

Nice. (I have used the QR package a lot, so yes, thanks to the creator.)

May I suggest a second look:

#! /bin/sh
#|
exec racket -qu "$0" ${1+"$@"}
|#
#lang racket/base

;; ./qr URL FileName creates a QR for `URL` and deposits it in `FileName`

(provide main)

(require simple-qr)

;; ---------------------------------------------------------------------------------------------------
#; {String String -> Void}
(define main
  (case-lambda
    [(url-str filename) (make-qr url-str filename)]
    [_ (use-as)]))

#; {String String -> Void}
(define (make-qr url-str filename)
  (with-handlers ([exn:fail? (lambda (e) (eprintf "Error: ~a\n" (exn-message e)) (exit 1))])
    (qr-write url-str filename))
  (displayln (string-append "QR code for " url-str " to " filename " as PNG")))

(define (use-as)
  (eprintf "expected usage: ./qr URL FileName\n")
  (exit 1))
1 Like