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)
(System/exit (if status 0 1)))
(defn get-message
"Return the message"
[options arguments]
(defn get-or-load-option
"Return the option value or load the value from a file."
[options arguments option]
(let [file-option (keyword (str (name option) "-file"))]
(cond
(contains? options :message)
(:message options)
(contains? options :message-file)
(file/read-file (:message-file options))))
(contains? options option)
(option 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]
(let [message (get-message options arguments)
(let [message (:message options)
key (Integer/parseInt (:key options))
symbols (:symbols options)]
(println
(cond
(= action-type :cracking)
(caesar-analyzers/get-plaintext message symbols symbol-frequences/english-letter-frequences)
(= action-type :encrypt)
(caesar/encrypt-message message key symbols)
(= action-type :decrypt)
(caesar/decrypt-message message key symbols)))))
(caesar/decrypt-message message key symbols))))
(defn actions
[options arguments action-type]
(let [options (load-all-options options)]
(println
(cond
(contains? #{:encrypt :decrypt} action-type)
(crypt-actions options arguments action-type)
(= action-type :cracking)
(cracking-actions options arguments action-type)))))

Loading…
Cancel
Save