Selaa lähdekoodia

enhance(cli): Add -h/--help to commands

Gabriel Horner 1 viikko sitten
vanhempi
sitoutus
ee97b8cda6
2 muutettua tiedostoa jossa 44 lisäystä ja 28 poistoa
  1. 1 0
      deps/cli/CHANGELOG.md
  2. 43 28
      deps/cli/src/logseq/cli.cljs

+ 1 - 0
deps/cli/CHANGELOG.md

@@ -1,4 +1,5 @@
 ## 0.4.2
 ## 0.4.2
+* Add `--help` to all commands as an alternative to `help [command]`
 * Add `--validate` option to `export-edn` command
 * Add `--validate` option to `export-edn` command
 * Fix cli can't run in CI environments
 * Fix cli can't run in CI environments
 * Fix `import-edn` and `mcp-server` commands not building refs for new or edited nodes
 * Fix `import-edn` and `mcp-server` commands not building refs for new or edited nodes

+ 43 - 28
deps/cli/src/logseq/cli.cljs

@@ -24,7 +24,7 @@
              :desc "Print version"}})
              :desc "Print version"}})
 
 
 (declare table)
 (declare table)
-(defn- help [_m]
+(defn- print-general-help [_m]
   (println (str "Usage: logseq [command] [options]\n\nOptions:\n"
   (println (str "Usage: logseq [command] [options]\n\nOptions:\n"
                 (cli/format-opts {:spec default-spec})))
                 (cli/format-opts {:spec default-spec})))
   (println (str "\nCommands:\n" (format-commands {:table table}))))
   (println (str "\nCommands:\n" (format-commands {:table table}))))
@@ -37,7 +37,7 @@
         (println (-> (fs/readFileSync package-json)
         (println (-> (fs/readFileSync package-json)
                      js/JSON.parse
                      js/JSON.parse
                      (aget "version")))))
                      (aget "version")))))
-    (help m)))
+    (print-general-help m)))
 
 
 (defn- print-command-help [command cmd-map]
 (defn- print-command-help [command cmd-map]
   (println (str "Usage: logseq " command
   (println (str "Usage: logseq " command
@@ -50,24 +50,30 @@
                 (when (:description cmd-map)
                 (when (:description cmd-map)
                   (str "\n\nDescription:\n" (cli-text-util/wrap-text (:description cmd-map) 80))))))
                   (str "\n\nDescription:\n" (cli-text-util/wrap-text (:description cmd-map) 80))))))
 
 
-(defn- help-command [{{:keys [command]} :opts}]
+(defn- help-command [{{:keys [command help]} :opts}]
   (if-let [cmd-map (and command (some #(when (= command (first (:cmds %))) %) table))]
   (if-let [cmd-map (and command (some #(when (= command (first (:cmds %))) %) table))]
     (print-command-help command cmd-map)
     (print-command-help command cmd-map)
-    (println "Command" (pr-str command) "does not exist")))
+    ;; handle help --help
+    (if-let [cmd-map (and help (some #(when (= "help" (first (:cmds %))) %) table))]
+      (print-command-help "help" cmd-map)
+      (println "Command" (pr-str command) "does not exist"))))
 
 
 (defn- lazy-load-fn
 (defn- lazy-load-fn
-  "Lazy load fn to speed up start time. After nbb requires ~30 namespaces, start time gets close to 1s"
+  "Lazy load fn to speed up start time. After nbb requires ~30 namespaces, start time gets close to 1s.
+   Also handles --help on all commands"
   [fn-sym]
   [fn-sym]
   (fn [& args]
   (fn [& args]
-    (-> (p/let [_ (require (symbol (namespace fn-sym)))]
-          (apply (resolve fn-sym) args))
-        (p/catch (fn [err]
-                   (if (= :sci/error (:type (ex-data err)))
-                     (nbb.error/print-error-report err)
-                     (js/console.error "Error:" err))
-                   (js/process.exit 1))))))
+    (if (get-in (first args) [:opts :help])
+      (help-command {:opts {:command (-> args first :dispatch first)}})
+      (-> (p/let [_ (require (symbol (namespace fn-sym)))]
+            (apply (resolve fn-sym) args))
+          (p/catch (fn [err]
+                     (if (= :sci/error (:type (ex-data err)))
+                       (nbb.error/print-error-report err)
+                       (js/console.error "Error:" err))
+                     (js/process.exit 1)))))))
 
 
-(def ^:private table
+(def ^:private table*
   [{:cmds ["list"] :desc "List local graphs"
   [{:cmds ["list"] :desc "List local graphs"
     :fn (lazy-load-fn 'logseq.cli.commands.graph/list-graphs)}
     :fn (lazy-load-fn 'logseq.cli.commands.graph/list-graphs)}
    {:cmds ["show"] :desc "Show DB graph(s) info"
    {:cmds ["show"] :desc "Show DB graph(s) info"
@@ -116,30 +122,39 @@
     :spec default-spec
     :spec default-spec
     :fn default-command}])
     :fn default-command}])
 
 
+;; Spec shared with all commands
+(def ^:private shared-spec
+  {:help {:alias :h
+          :desc "Print help"}})
+
+(def ^:private table
+  (mapv (fn [m] (update m :spec #(merge % shared-spec))) table*))
+
 (defn- warn-if-db-version-not-installed
 (defn- warn-if-db-version-not-installed
   []
   []
   (when-not (fs/existsSync (cli-common-graph/get-db-graphs-dir))
   (when-not (fs/existsSync (cli-common-graph/get-db-graphs-dir))
     (println "[WARN] The database version's desktop app is not installed. Please install per https://github.com/logseq/logseq/#-database-version.")))
     (println "[WARN] The database version's desktop app is not installed. Please install per https://github.com/logseq/logseq/#-database-version.")))
 
 
 (defn ^:api -main [& args]
 (defn ^:api -main [& args]
-  (when-not (contains? #{nil "-h" "--help"} (first args))
-    (warn-if-db-version-not-installed))
+  (warn-if-db-version-not-installed)
   (try
   (try
     (cli/dispatch table
     (cli/dispatch table
                   args
                   args
-                  {:error-fn (fn [{:keys [cause msg option] type' :type :as data}]
-                               (if (and (= :org.babashka/cli type')
-                                        (= :require cause))
-                                 (do
-                                   (println "Error: Command missing required"
-                                            (if (get-in data [:spec option]) "option" "argument")
-                                            (pr-str (name option)))
-                                   (when-let [cmd-m (some #(when (= {:spec (:spec %)
-                                                                     :require (:require %)}
-                                                                    (select-keys data [:spec :require])) %) table)]
-                                     (print-command-help (-> cmd-m :cmds first) cmd-m)))
-                                 (throw (ex-info msg data)))
-                               (js/process.exit 1))})
+                  {:error-fn (fn [{:keys [cause msg option opts] type' :type :as data}]
+                               ;; Options aren't required when printing help
+                               (when-not (:help opts)
+                                 (if (and (= :org.babashka/cli type')
+                                          (= :require cause))
+                                   (do
+                                     (println "Error: Command missing required"
+                                              (if (get-in data [:spec option]) "option" "argument")
+                                              (pr-str (name option)))
+                                     (when-let [cmd-m (some #(when (= {:spec (:spec %)
+                                                                       :require (:require %)}
+                                                                      (select-keys data [:spec :require])) %) table)]
+                                       (print-command-help (-> cmd-m :cmds first) cmd-m)))
+                                   (throw (ex-info msg data)))
+                                 (js/process.exit 1)))})
     (catch ^:sci/error js/Error e
     (catch ^:sci/error js/Error e
       (nbb.error/print-error-report e)
       (nbb.error/print-error-report e)
       (js/process.exit 1))))
       (js/process.exit 1))))