Skip to content

A peer-to-peer stack for code collaboration

Use new list filter syntax (#439)

Merle Breitkreuz6805d4d2faef
diff --git a/bin/rad-diff b/bin/rad-diff
index 03205d4..f05d3b2 100755
--- a/bin/rad-diff
+++ b/bin/rad-diff
@@ -5,16 +5,20 @@
(load! (find-module-file! "prelude/io-utils.rad"))
(load! (find-module-file! "monadic/project.rad"))
-(import prelude/cmd-parsing :unqualified)
(import prelude/validation :as 'validation)
(def machine-name (fn [] (get-rsm-of-type! :rad-diff)))
-(def valid-list-options
- ["--pending" "--retracted" "--accepted" "--rejected" "--fancy"])
+(def valid-states
+ ["pending" "retracted" "accepted" "rejected"])
+
+(def status-opts ["-s" "--state" "--filter-by-state" ])
(def list-opts-help
- (foldl (fn [a opt] (string-append a " [" opt "]")) "" valid-list-options))
+ (string-append
+ "[("
+ (string/intercalate " | " status-opts)
+ ") <state>] [--fancy]"))
(def help
(string-append
@@ -23,7 +27,7 @@
Usage:
rad-diff list " list-opts-help "
rad-diff propose <commit>
- rad-diff [retract|accept|reject|show] <diff-no>
+ rad-diff [retract | accept | reject | show] <diff-no>
rad-diff comment <diff-no> <comment>
rad-diff help
@@ -191,9 +195,9 @@
(fn [diff] [(pretty-headline diff) diff]))
(def filt
- (match (filter (fn [opt] (not (eq? opt :fancy))) options)
+ (match (map (fn [opt] (read (string-append ":" opt))) (lookup :state options))
[] (fn [x] #t)
- _ (fn [x] (elem? (lookup :state x) options))))
+ 'args (fn [x] (elem? (lookup :state x) args))))
(def items
(map mk-key
@@ -203,7 +207,7 @@
(def items-dict (dict-from-seq items))
(if (empty-seq? items)
(put-str! "No diffs!")
- (if (elem? :fancy options)
+ (if (lookup :fancy options)
(match (fzf-select-with-preview!
(map first items)
(fn [s] (pretty-diff (lookup s items-dict))))
@@ -271,16 +275,15 @@
(cmd-parse-failure "Diff numbers must be whole numbers!"))
(cmd-parse-failure "To create a comment, please provide the diff number."))))
-(def list-options
- (fn [os]
- (map (fn [o]
- (if (member? o valid-list-options)
- (read (string-append ":" (drop 2 o)))
- (cmd-parse-failure (string-append "Invalid list option: " o)))) os)))
+(def cmd-options
+ [
+ { :key :state :type :multi-opt :options status-opts :possible-values valid-states :default []}
+ { :key :fancy :type :flag :options ["--fancy"] :default #f }
+ ])
(def /list-cmd
(fn [opts]
- (/cmd-1-opts "list" opts list-options)))
+ (/cmd-opts "list" opts cmd-options help)))
(def args (get-args!))
(match args
diff --git a/bin/rad-issues b/bin/rad-issues
index 2040721..d78d0a9 100755
--- a/bin/rad-issues
+++ b/bin/rad-issues
@@ -7,15 +7,25 @@
(import prelude/validation :as 'validation)
(import prelude/machine :as 'machine)
-(import prelude/cmd-parsing :unqualified)
(def machine-name (fn [] (get-rsm-of-type! :rad-issues)))
+(def valid-states ["open" "closed"])
+
+(def status-opts ["--filter-by-state" "--state" "-s"])
+
+(def list-opts-help
+ (string-append
+ "[("
+ (string/intercalate " | " status-opts)
+ ") <state>] [--fancy]"))
+
(def help
+ (string-append
"rad-issues - Radicle issue CLI
Usage:
- rad-issues list [--closed] [--open]
+ rad-issues list " list-opts-help "
rad-issues new
rad-issues close <issue-number>
rad-issues make-key <nick>
@@ -26,7 +36,7 @@
new - Create a new issue in $EDITOR
close - Close an issue
help - Print this help and exit
- ")
+ "))
(def cmd-parse-failure
(fn [error]
@@ -101,11 +111,9 @@
[(string-append "Issue " (show n) " - " tit) i])))
(def filt
- (match [(lookup-default "open" #f options)
- (lookup-default "closed" #f options)]
- [#t #f] (fn [x] (eq? (lookup :state (lookup :issue x)) :open))
- [#f #t] (fn [x] (eq? (lookup :state (lookup :issue x)) :closed))
- _ (fn [x] #t)))
+ (match (map (fn [opt] (read (string-append ":" opt))) (lookup :state options))
+ [] (fn [x] #t)
+ 'args (fn [x] (elem? (lookup :state (lookup :issue x)) args))))
(def items
(map mk-key
@@ -115,28 +123,30 @@
(def items-dict (dict-from-seq items))
(if (empty-seq? items)
(put-str! "No issues!")
- (match (fzf-select-with-preview!
- (map first items)
- (fn [s] (pretty-issue (add-username-issue (lookup :issue (lookup s items-dict))))))
- (/just 's)
- (do (def selected (lookup s items-dict))
- (def issue-number (match selected {:issue {:number 'n}} n))
- (def pretty-item
- (add-comment-md
- (pretty-issue
- (add-username-issue
- (lookup :issue selected)))))
- (def edited-item (edit-in-editor! pretty-item))
- (def added-comment
- (string/unlines
- (take-while (fn [x] (not (eq? x comment-divider)))
- (string/lines edited-item))))
- (put-str!
- (if (eq? added-comment "")
- "Item was not updated."
- (do (simple-add-comment! machine issue-number added-comment)
- (string-append "Sent comment: " added-comment)))))
- _ (put-str! "No selection made.")))))
+ (if (lookup :fancy options)
+ (match (fzf-select-with-preview!
+ (map first items)
+ (fn [s] (pretty-issue (add-username-issue (lookup :issue (lookup s items-dict))))))
+ (/just 's)
+ (do (def selected (lookup s items-dict))
+ (def issue-number (match selected {:issue {:number 'n}} n))
+ (def pretty-item
+ (add-comment-md
+ (pretty-issue
+ (add-username-issue
+ (lookup :issue selected)))))
+ (def edited-item (edit-in-editor! pretty-item))
+ (def added-comment
+ (string/unlines
+ (take-while (fn [x] (not (eq? x comment-divider)))
+ (string/lines edited-item))))
+ (put-str!
+ (if (eq? added-comment "")
+ "Item was not updated."
+ (do (simple-add-comment! machine issue-number added-comment)
+ (string-append "Sent comment: " added-comment)))))
+ _ (put-str! "No selection made."))
+ (map (fn [d] (put-str! (first d))) items)))))
(def close-issue
(fn [issue-number machine-name]
@@ -159,21 +169,15 @@
(def i (read (edit-in-editor! template)))
(create-issue! machine (simple-issue i))))
-(def valid-list-options
- ["--open" "--closed"])
-
-(def list-options
- (fn [os]
- (foldr (fn [o acc]
- (if (member? o valid-list-options)
- (insert (drop 2 o) #t acc)
- (cmd-parse-failure (string-append "Invalid list option: " o))))
- {}
- os)))
+(def cmd-options
+ [
+ { :key :state :type :multi-opt :options ["-s" "--state" "--filter-by-state"] :possible-values valid-states :default []}
+ { :key :fancy :type :flag :options ["--fancy"] :default #f }
+ ])
(def /list-cmd
(fn [opts]
- (/cmd-1-opts "list" opts list-options)))
+ (/cmd-opts "list" opts cmd-options help)))
(def args (get-args!))
(match args
diff --git a/rad/prelude.rad b/rad/prelude.rad
index e24cff2..7f9ad96 100644
--- a/rad/prelude.rad
+++ b/rad/prelude.rad
@@ -42,3 +42,4 @@
(import prelude/io-utils :unqualified)
(import prelude/machine :as 'machine)
(import prelude/machine-remote :unqualified)
+(import prelude/cmd-parsing :unqualified)
diff --git a/rad/prelude/cmd-parsing.rad b/rad/prelude/cmd-parsing.rad
index 582aec6..c08726a 100644
--- a/rad/prelude/cmd-parsing.rad
+++ b/rad/prelude/cmd-parsing.rad
@@ -1,11 +1,15 @@
{:module 'prelude/cmd-parsing
:doc "Functions for parsing commands."
:exports
- '[parse-failure /cmd-0 /cmd-1 /cmd-2 /cmd-1-opts]}
+ '[parse-failure /cmd-0 /cmd-1 /cmd-2 /cmd-opts]}
+(import prelude/basic :unqualified)
+(import prelude/dict :unqualified)
(import prelude/validation :as 'validation)
(import prelude/patterns :unqualified)
(import prelude/lens :unqualified)
+(import prelude/seq :unqualified)
+(import prelude/strings :unqualified)
(def parse-failure-mode (ref {:stub #f}))
@@ -13,12 +17,16 @@
"Prints the passed error message and help text and exits."
(fn [error help]
(if (lookup :stub (read-ref parse-failure-mode))
- (string-append error " " help)
+ (throw 'invalid-input (string-append error " " help))
(do
(put-str! error)
(put-str! help)
(exit! 1)))))
+(def catch-for-testing
+ (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 "\"")))
@@ -27,32 +35,79 @@
(fn [cmd]
(string-append "Too many arguments for command \"" cmd "\"")))
-(def /cmd-1-opts
- "Matches a command `cmd` with one argument and an optional
- combination of options that are allowed by opts-map"
- (fn [cmd opts opts-map]
+(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
+ possible option is represented by the keys `:key`, `:default`, `:options` and
+ `:type`. Types can be `:flag` (boolean) or `:multi-opt` (can occur more than
+ once in a command, a list of valid options is passed with `:possible-values`).
+ If `opt` is empty, the default for each `cmd-opts` is returned. "
+ (fn [cmd opts cmd-opts help]
+ (def default
+ (dict-from-seq
+ (map (fn [opt] (list (lookup :key opt) (lookup :default opt))) cmd-opts)))
+ (def-rec parse-cli
+ (fn [args]
+ (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))))
+ (match args
+ (/cons 'val 'rest)
+ (if (elem? val (lookup :possible-values opt))
+ (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)
+ (parse-failure
+ (string-append
+ "Invalid argument \"" val "\" for option \"" (show opt-key) "\". "
+ valid-args-help)
+ help))))
+ [] (parse-failure
+ (string-append "Missing argument for option \"" (show opt-key) "\". " valid-args-help) 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)
+ 'filt (first filt)))
+ (if (eq? (lookup :type option) :flag)
+ (insert (lookup :key option) #t (parse-cli rest))
+ (parse-arg option rest))))
+ (match args
+ /nil default
+ (/cons 'opt 'rest) (parse-opt opt rest))))
(fn [v]
(match v
- [cmd] [:just {opts []}]
- (/cons cmd 'os) [:just {opts (opts-map os)}]
+ [cmd] [:just {opts default}]
+ (/cons cmd 'os) [:just {opts (parse-cli os)}]
_ :nothing))))
-(:test "/cmd-1-opts"
+(:test "/cmd-opts"
[:setup
(do
- (def opts-map
- (fn [opts]
- (map
- (fn [opt]
- (if (elem? opt ["--new" "--old"])
- (drop 2 opt)
- "unknown filter")) opts))))]
- [ ((/cmd-1-opts "list" 'opts opts-map) ["list"]) ==> [:just {opts []}]]
- [ ((/cmd-1-opts "list" 'opts opts-map) ["no-list"]) ==> :nothing]
- [ ((/cmd-1-opts "list" 'opts opts-map) ["list" "--old"]) ==> [:just {opts ["old"]}]]
- [ ((/cmd-1-opts "list" 'opts opts-map) ["list" "--old" "--new"]) ==> [:just {opts ["old" "new"]}]]
- [ ((/cmd-1-opts "list" 'opts opts-map) ["list" "--filter"]) ==> [:just {opts ["unknown filter"]}]]
- [ ((/cmd-1-opts "list" 'opts opts-map) ["list" "--old" "--current"]) ==> [:just {opts ["old" "unknown filter"]}]])
+ (set-ref parse-failure-mode (@ :stub) #t)
+ (def help "help")
+ (def cmd-opts
+ [
+ { :key :state :type :multi-opt :options ["-s" "--state" "--filter-by-state"] :possible-values ["acc" "prop"] :default []}
+ { :key :fancy :type :flag :options ["--fancy"] :default #f }
+ ])
+ (def /cmd (/cmd-opts "list" 'opts cmd-opts help)))]
+ [(/cmd ["list"] ) ==> [:just {opts {:state [] :fancy #f}}]]
+ [(/cmd ["list" "--fancy"] ) ==> [:just {opts {:state [] :fancy #t}}]]
+ [(/cmd ["list" "--state" "acc"] ) ==> [:just {opts {:state ["acc"] :fancy #f}}]]
+ [(/cmd ["list" "--fancy" "-s" "acc"] ) ==> [:just {opts {:state ["acc"] :fancy #t}}]]
+ [(/cmd ["list" "--state" "prop" "-s" "acc"] ) ==> [:just {opts {:state ["prop" "acc"] :fancy #f}}]]
+ [(catch-for-testing /cmd ["list" "--invalid" "prop"] ) ==> "Invalid option \"--invalid\" for command list. help"]
+ [(catch-for-testing /cmd ["list" "prop"] ) ==> "Invalid option \"prop\" for command list. help"]
+ [(catch-for-testing /cmd ["list" "--state" "invalid"] ) ==> "Invalid argument \"invalid\" for option \":state\". Valid arguments: acc, prop help"]
+ [(catch-for-testing /cmd ["list" "--state"] ) ==> "Missing argument for option \":state\". Valid arguments: acc, prop help"]
+ [(catch-for-testing /cmd ["list" "--state" "--fancy"] ) ==> "Missing argument for option \":state\". Valid arguments: acc, prop help"])
(def /cmd-2
"Matches a command `cmd` with exactly two arguments."
@@ -70,12 +125,14 @@
(:test "/cmd-2"
[:setup
- (do (set-ref parse-failure-mode (@ :stub) #t))]
- [ ((/cmd-2 "cmd-2" 'arg-1 'arg-2 "help") ["cmd-2" "arg1" "arg2"]) ==> [:just {arg-1 "arg1" arg-2 "arg2"}]]
- [ ((/cmd-2 "cmd-2" 'arg-1 'arg-2 "help") ["cmd-0"]) ==> :nothing]
- [ ((/cmd-2 "cmd-2" 'arg-1 'arg-2 "help") ["cmd-2"]) ==> "Missing argument \"arg-1\" for command \"cmd-2\" help"]
- [ ((/cmd-2 "cmd-2" 'arg-1 'arg-2 "help") ["cmd-2" 1]) ==> "Missing argument \"arg-2\" for command \"cmd-2\" help"]
- [ ((/cmd-2 "cmd-2" 'arg-1 'arg-2 "help") ["cmd-2" 1 2 3]) ==> "Too many arguments for command \"cmd-2\" help"])
+ (do
+ (set-ref parse-failure-mode (@ :stub) #t)
+ (def /cmd (/cmd-2 "cmd-2" 'arg-1 'arg-2 "help")))]
+ [ (/cmd ["cmd-2" "arg1" "arg2"]) ==> [:just {arg-1 "arg1" arg-2 "arg2"}]]
+ [ (/cmd ["cmd-0"]) ==> :nothing]
+ [ (catch-for-testing /cmd ["cmd-2"]) ==> "Missing argument \"arg-1\" for command \"cmd-2\" help"]
+ [ (catch-for-testing /cmd ["cmd-2" 1]) ==> "Missing argument \"arg-2\" for command \"cmd-2\" help"]
+ [ (catch-for-testing /cmd ["cmd-2" 1 2 3]) ==> "Too many arguments for command \"cmd-2\" help"])
(def /cmd-1
"Matches a command `cmd` with exactly one arguments."
@@ -91,11 +148,13 @@
(:test "/cmd-1"
[:setup
- (do (set-ref parse-failure-mode (@ :stub) #t))]
- [ ((/cmd-1 "cmd-1" 'arg "help") ["cmd-1" "arg"]) ==> [:just {arg "arg"}]]
- [ ((/cmd-1 "cmd-1" 'arg "help") ["cmd-0"]) ==> :nothing]
- [ ((/cmd-1 "cmd-1" 'arg "help") ["cmd-1"]) ==> "Missing argument \"arg\" for command \"cmd-1\" help"]
- [ ((/cmd-1 "cmd-1" 'arg "help") ["cmd-1" 1 2]) ==> "Too many arguments for command \"cmd-1\" help"])
+ (do
+ (set-ref parse-failure-mode (@ :stub) #t)
+ (def /cmd (/cmd-1 "cmd-1" 'arg "help")))]
+ [ (/cmd ["cmd-1" "arg"]) ==> [:just {arg "arg"}]]
+ [ (/cmd ["cmd-0"]) ==> :nothing]
+ [ (catch-for-testing /cmd ["cmd-1"]) ==> "Missing argument \"arg\" for command \"cmd-1\" help"]
+ [ (catch-for-testing /cmd ["cmd-1" 1 2]) ==> "Too many arguments for command \"cmd-1\" help"])
(def /cmd-0
"Matches a command `cmd` with no other arguments."
@@ -109,7 +168,9 @@
(:test "/cmd-0"
[:setup
- (do (set-ref parse-failure-mode (@ :stub) #t))]
- [ ((/cmd-0 "cmd-0" "help") ["cmd-0"]) ==> [:just {}]]
- [ ((/cmd-0 "cmd-0" "help") ["cmd-1"]) ==> :nothing]
- [ ((/cmd-0 "cmd-0" "help") ["cmd-0" 1]) ==> "Too many arguments for command \"cmd-0\" help"])
+ (do
+ (set-ref parse-failure-mode (@ :stub) #t)
+ (def /cmd (/cmd-0 "cmd-0" "help")))]
+ [ (/cmd ["cmd-0"]) ==> [:just {}]]
+ [ (/cmd ["cmd-1"]) ==> :nothing]
+ [ (catch-for-testing /cmd ["cmd-0" 1]) ==> "Too many arguments for command \"cmd-0\" help"])