Update the CLI to have separated actions and an option initializer.

main
KKlochko 2 years ago
parent 6f15eedacd
commit 9e4c46814c

@ -88,29 +88,65 @@
(println exit-message) (println exit-message)
(System/exit (if status 0 1))) (System/exit (if status 0 1)))
(defn get-message (defn get-or-load-option
"Return the message" "Return the option value or load the value from a file."
[options arguments] [options arguments option]
(cond (let [file-option (keyword (str (name option) "-file"))]
(contains? options :message) (cond
(:message options) (contains? options option)
(option options)
(contains? options :message-file)
(file/read-file (:message-file options)))) (contains? options file-option)
(file/read-file (file-option options)))))
(defn set-option
"Set a option in the option map."
[options option value]
(-> options
(assoc option value)))
(defn load-and-set-option
"Load the option and set it."
[options option]
(->> (get-or-load-option options nil option)
(set-option options option)))
(defn load-all-options
"Load all options."
[options]
(-> options
(load-and-set-option :message)))
(defn cracking-actions
[options arguments action-type]
(let [message (:message options)
symbols (:symbols options)
frequencies symbol-frequences/english-letter-frequences]
(cond
(= action-type :cracking)
(caesar-analyzers/get-plaintext message symbols frequencies)
)))
(defn actions (defn crypt-actions
[options arguments action-type] [options arguments action-type]
(let [message (get-message options arguments) (let [message (:message options)
key (Integer/parseInt (:key options)) key (Integer/parseInt (:key options))
symbols (:symbols options)] symbols (:symbols options)]
(cond
(= action-type :encrypt)
(caesar/encrypt-message message key symbols)
(= action-type :decrypt)
(caesar/decrypt-message message key symbols))))
(defn actions
[options arguments action-type]
(let [options (load-all-options options)]
(println (println
(cond (cond
(= action-type :cracking) (contains? #{:encrypt :decrypt} action-type)
(caesar-analyzers/get-plaintext message symbols symbol-frequences/english-letter-frequences) (crypt-actions options arguments action-type)
(= action-type :encrypt)
(caesar/encrypt-message message key symbols)
(= action-type :decrypt) (= action-type :cracking)
(caesar/decrypt-message message key symbols))))) (cracking-actions options arguments action-type)))))

Loading…
Cancel
Save