Skip to content

A peer-to-peer stack for code collaboration

Extract error messages for command parsing (#480)

Merle Breitkreuzb9a27f284c59
diff --git a/rad/prelude/cmd-parsing.rad b/rad/prelude/cmd-parsing.rad
index c08726a..1c6ef32 100644
--- a/rad/prelude/cmd-parsing.rad
+++ b/rad/prelude/cmd-parsing.rad
@@ -11,6 +11,9 @@
(import prelude/seq :unqualified)
(import prelude/strings :unqualified)
+(file-module! "prelude/error-messages.rad")
+(import prelude/error-messages :as 'error)
+
(def parse-failure-mode (ref {:stub #f}))
(def parse-failure
@@ -27,14 +30,6 @@
(fn [cmd opts]
(catch 'invalid-input (cmd opts) (fn [x] x))))
-(def missing-arg-error
- (fn [arg cmd]
- (string-append "Missing argument \"" (show arg) "\" for command \"" cmd "\"")))
-
-(def too-many-args-error
- (fn [cmd]
- (string-append "Too many arguments for command \"" cmd "\"")))
-
(def /cmd-opts
"Matches the command `cmd` and a combination of options `opt`. The options
need to be one of `:options` of the dicts in `cmd-opts`. In that list each
@@ -51,29 +46,27 @@
(def parse-arg
(fn [opt args]
(def opt-key (lookup :key opt))
- (def valid-args-help
- (string-append "Valid arguments: " (intercalate ", "(lookup :possible-values opt))))
+ (def valid-args
+ (lookup :possible-values opt))
(match args
(/cons 'val 'rest)
- (if (elem? val (lookup :possible-values opt))
+ (if (elem? val valid-args)
(modify-map opt-key (fn [x] (cons val x)) (parse-cli rest))
(do
(if (starts-with? val "-")
(parse-failure
- (string-append "Missing argument for option \"" (show opt-key) "\". " valid-args-help) help)
+ (error/missing-arg-for-opt (show opt-key) valid-args) help)
(parse-failure
- (string-append
- "Invalid argument \"" val "\" for option \"" (show opt-key) "\". "
- valid-args-help)
+ (error/invalid-arg-for-opt val (show opt-key) valid-args)
help))))
[] (parse-failure
- (string-append "Missing argument for option \"" (show opt-key) "\". " valid-args-help) help)
+ (error/missing-arg-for-opt (show opt-key) valid-args) help)
)))
(def parse-opt
(fn [opt rest]
(def option
(match (filter (fn [x] (elem? opt (lookup :options x))) cmd-opts)
- [] (parse-failure (string-append "Invalid option \"" opt "\" for command list.") help)
+ [] (parse-failure (error/invalid-opt-for-cmd opt "list") help)
'filt (first filt)))
(if (eq? (lookup :type option) :flag)
(insert (lookup :key option) #t (parse-cli rest))
@@ -118,9 +111,9 @@
(fn [value]
(match value
[cmd 'a-1 'a-2] (match-pat [arg-1 arg-2] [a-1 a-2])
- [cmd _] (parse-failure (missing-arg-error arg-2 cmd) help)
- [cmd] (parse-failure (missing-arg-error arg-1 cmd) help)
- (/cons cmd _) (parse-failure (too-many-args-error cmd) help)
+ [cmd _] (parse-failure (error/missing-arg (show arg-2) cmd) help)
+ [cmd] (parse-failure (error/missing-arg (show arg-1) cmd) help)
+ (/cons cmd _) (parse-failure (error/too-many-args cmd) help)
_ :nothing))))
(:test "/cmd-2"
@@ -142,8 +135,8 @@
(fn [value]
(match value
[cmd 'cmd-args] (match-pat cmd-args-pat cmd-args)
- [cmd] (parse-failure (missing-arg-error cmd-args-pat cmd) help)
- (/cons cmd _) (parse-failure (too-many-args-error cmd) help)
+ [cmd] (parse-failure (error/missing-arg (show cmd-args-pat) cmd) help)
+ (/cons cmd _) (parse-failure (error/too-many-args cmd) help)
_ :nothing))))
(:test "/cmd-1"
@@ -163,7 +156,7 @@
(fn [value]
(match value
[cmd] [:just {}]
- (/cons cmd _) (parse-failure (too-many-args-error cmd) help)
+ (/cons cmd _) (parse-failure (error/too-many-args cmd) help)
_ :nothing))))
(:test "/cmd-0"
diff --git a/rad/prelude/error-messages.rad b/rad/prelude/error-messages.rad
new file mode 100644
index 0000000..33a4fc5
--- /dev/null
+++ b/rad/prelude/error-messages.rad
@@ -0,0 +1,58 @@
+{:module 'prelude/error-messages
+ :doc "Functions for user facing error messages.
+ Functions should either have a descriptive name or additional comment so that
+ the text can be edited without knowledge of where they are used.
+ To verify changes, tests can be run with `stack exec -- radicle test/all.rad`"
+ :exports
+ '[missing-arg too-many-args missing-arg-for-opt invalid-arg-for-opt
+ invalid-opt-for-cmd]}
+
+(import prelude/strings :unqualified)
+
+;; parsing
+
+(def missing-arg
+ (fn [arg cmd]
+ (string-append "Missing argument \"" arg "\" for command \"" cmd "\"")))
+
+(:test "missing-arg"
+ [(missing-arg "foo" "bar") ==> "Missing argument \"foo\" for command \"bar\""])
+
+(def too-many-args
+ (fn [cmd]
+ (string-append "Too many arguments for command \"" cmd "\"")))
+
+(:test "too-many-args"
+ [(too-many-args "foobar") ==> "Too many arguments for command \"foobar\""])
+
+(def valid-args-help
+ (fn [args]
+ (string-append "Valid arguments: " (intercalate ", " args))))
+
+(:test "valid-args-help"
+ [(valid-args-help ["foo" "bar"]) ==> "Valid arguments: foo, bar"])
+
+(def missing-arg-for-opt
+ (fn [opt valid-args]
+ (string-append
+ "Missing argument for option \"" opt "\". "
+ (valid-args-help valid-args))))
+
+(:test "missing-arg-for-opt"
+ [(missing-arg-for-opt "-s" ["foo" "bar"]) ==> "Missing argument for option \"-s\". Valid arguments: foo, bar"])
+
+(def invalid-arg-for-opt
+ (fn [arg opt valid-args]
+ (string-append
+ "Invalid argument \"" arg "\" for option \"" opt "\". "
+ (valid-args-help valid-args))))
+
+(:test "invalid-arg-for-opt"
+ [(invalid-arg-for-opt "foobar" "-s" ["foo" "bar"]) ==> "Invalid argument \"foobar\" for option \"-s\". Valid arguments: foo, bar"])
+
+(def invalid-opt-for-cmd
+ (fn [opt cmd]
+ (string-append "Invalid option \"" opt "\" for command " cmd ".")))
+
+(:test "invalid-opt-for-cmd"
+ [(invalid-opt-for-cmd "foo" "list") ==> "Invalid option \"foo\" for command list."])