Skip to content

Commit

Permalink
Add cljfmt options programmatically to CLI options
Browse files Browse the repository at this point in the history
This further reduces code duplication and automatically sorts the CLI
options by name.
  • Loading branch information
or committed May 1, 2022
1 parent bb08259 commit 3d7e866
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions cljfmt/src/cljfmt/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -161,40 +161,25 @@
(def default-paths ["src" "test" "project.clj"])

(def ^:private cli-options
[[nil "--help"]
[nil "--file-pattern FILE_PATTERN"
:default (:file-pattern default-options)
:parse-fn re-pattern]
[nil "--indents INDENTS_PATH"
:parse-fn cli-file-reader]
[nil "--alias-map ALIAS_MAP_PATH"
:parse-fn cli-file-reader]
[nil "--project-root PROJECT_ROOT"
:default (:project-root default-options)]
[nil "--[no-]ansi"
:default (:ansi? default-options)
:id :ansi?]
[nil "--[no-]indentation"
:default (:indentation? cljfmt/default-options)
:id :indentation?]
[nil "--[no-]remove-multiple-non-indenting-spaces"
:default (:remove-multiple-non-indenting-spaces? cljfmt/default-options)
:id :remove-multiple-non-indenting-spaces?]
[nil "--[no-]remove-surrounding-whitespace"
:default (:remove-surrounding-whitespace? cljfmt/default-options)
:id :remove-surrounding-whitespace?]
[nil "--[no-]remove-trailing-whitespace"
:default (:remove-trailing-whitespace? cljfmt/default-options)
:id :remove-trailing-whitespace?]
[nil "--[no-]insert-missing-whitespace"
:default (:insert-missing-whitespace? cljfmt/default-options)
:id :insert-missing-whitespace?]
[nil "--[no-]remove-consecutive-blank-lines"
:default (:remove-consecutive-blank-lines? cljfmt/default-options)
:id :remove-consecutive-blank-lines?]
[nil "--[no-]split-keypairs-over-multiple-lines"
:default (:split-keypairs-over-multiple-lines? cljfmt/default-options)
:id :split-keypairs-over-multiple-lines?]])
(into
[[nil "--help"]
[nil "--file-pattern FILE_PATTERN"
:default (:file-pattern default-options)
:parse-fn re-pattern]
[nil "--indents INDENTS_PATH"
:parse-fn cli-file-reader]
[nil "--alias-map ALIAS_MAP_PATH"
:parse-fn cli-file-reader]
[nil "--project-root PROJECT_ROOT"
:default (:project-root default-options)]
[nil "--[no-]ansi"
:default (:ansi? default-options)
:id :ansi?]]
(for [[option default] (sort-by (comp name first) cljfmt/default-options)]
[nil (str "--[no-]" (let [s (name option)]
(subs s 0 (-> s count dec))))
:default default
:id option])))

(defn- file-exists? [path]
(.exists (io/as-file path)))
Expand Down

0 comments on commit 3d7e866

Please sign in to comment.