Created
November 11, 2011 17:00
-
-
Save hhutch/1358542 to your computer and use it in GitHub Desktop.
Clojurescript + NodeJS + Commander CLI + Core.Match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; You do have Clojurescript, right? | |
;; First Install NodeJS | |
;; Then install npm (node package manager) | |
;; Then install Commander.js https://github.com/visionmedia/commander.js | |
$ cljsc src/cljs-demo/pizza.cljs '{:optimizations :simple :pretty-print true :target :nodejs}' > pizza.js | |
$ ./pizza.js --help | |
Usage: pizza.js [options] | |
Options: | |
-h, --help output usage information | |
-V, --version output the version number | |
-p, --peppers add peppers | |
-P, --pinapple add pinapple | |
-e, --pepperoni add pepperoni | |
-s, --sardines add sardines | |
$ ./pizza.js -p -P | |
SWEET! | |
$ ./pizza.js -p | |
must have fishies!!! | |
$ ./pizza.js -p -s | |
$ ./pizza.js -s -P | |
REALLY?! are you crazy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns cljs-demo.pizza | |
(:use-macros [clojure.core.match.js :only [match]]) | |
(:require [cljs.nodejs :as node])) | |
(def commander (node/require "commander")) | |
(defn has? | |
[what] | |
(= true (aget commander (name what)))) | |
(doto commander | |
(.version "0.0.1") | |
(.option "-p, --peppers" "add peppers") | |
(.option "-P, --pinapple" "add pinapple") | |
(.option "-e, --pepperoni" "add pepperoni") | |
(.option "-s, --sardines" "add sardines") | |
(.parse process.argv)) | |
(defn start [& _] | |
(let [p (has? :peppers) | |
P (has? :pinapple) | |
e (has? :pepperoni) | |
s (has? :sardines)] | |
(match [p P e s ] | |
[true true _ _ ] (println "SWEET!") | |
[_ _ _ false] (println "must have fishies!!!") | |
[_ true _ true ] (println "REALLY?! are you crazy") ))) | |
(set! *main-cli-fn* start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment