l3nz / cli-matic

Compact, hands-free [sub]command line parsing library for Clojure.
Eclipse Public License 2.0
361 stars 29 forks source link

Support for STDIN #28

Closed gaberger closed 2 years ago

gaberger commented 6 years ago

I hacked something together that looks like

 (let
   [bf (java.io.BufferedReader. *in*)]
    (if
     (.ready bf)
      (let
       [conf (slurp *in*) vargs (into [] args)]
        (run-cmd (conj vargs "--stdin" conf) STDIN_INPUT))
      (run-cmd args FILE_INPUT))))

where STDIN_INPUT

(def
  STDIN_INPUT
  {:app
   {:command "nparser",
    :description "A command-line configuration generator",
    :version "0.1.2"},
   :global-opts [],
   :commands
   [{:command "to-json",
     :description "Generate JSON from a config",
     :opts
     [{:option "stdin",
       :as "Config input file",
       :type :string,
       :default :present}
      {:option "grammar",
       :as "Grammar file",
       :type :string,
       :default :present}],
     :runs gen-json}
    {:command "to-config",
     :description "Generate config from an input file",
     :opts
     [{:option "stdin",
       :as "JSON input file",
       :type :string,
       :default :present}],
     :runs gen-config}]})

not very idiomatic, but would be great to incorporate this into cli-matic..

The primary use-case is now that we have GRAALVM we can native-compile CLJ code into binary artifacts to build command-line apps..

l3nz commented 6 years ago

Hi, thanks for your comment. I am not sure how that is supposed to work - like, how would you use it? In general, if you have a type of :jsonfile, Cli-matic will read the file and convert it to a Clojue data structure.

As for stdin, you could use /dev/stdinbut if you have something that is easier to use I'd love to add it.

As a side note, Graal is definitely a game changer - though I personally have not had much success playing with it. But I only tried once on some VMs and noticed it was taking forever.

l3nz commented 6 years ago

BTW, as for GraalVM, have a look here: https://github.com/l3nz/cli-matic/issues/29 💡

l3nz commented 6 years ago

Apropos of GraalVM - see https://www.astrecipes.net/blog/2018/07/20/cmd-line-apps-with-clojure-and-graalvm/

l3nz commented 6 years ago

I am not sure about stdin, as stdin is a file, so you can use it as /dev/stdin as a normal file (and if you want, it could be the default for a parameter, so you do not actually specify it).

Maybe we could use "-", so that it gets expanded to /dev/stdin or whatever it is when on Windows?

TimoKramer commented 3 years ago

Hi there, do you think it is sufficient to add stdin support for :slurp? I need the feature for a project linked above. What I added so far is that one can use - for placeholder when requiring stdin.

What do you think?

l3nz commented 2 years ago

Merged