Closed l3nz closed 4 years ago
The trick may be changing the subcommmand from nil or a string, to a vector of 0 or more strings.
And making the main parser recursive: https://github.com/l3nz/cli-matic/blob/23e52bbcc14695bc74bff73bc07624d119004694/src/cli_matic/core.cljc#L356
Actually, we could get away with :global-options
and :commands,
nesting them into a new recursive structure that contains options and commands:
{:command "toycalc"
:description "A command-line toy calculator"
:version "0.0.1"
:opts [{:option "base"
:as "The number base for output"
:type :int
:default 10}]
:commands [
{:command "add"
:description "Adds two numbers together"
:opts [{:option "a" :as "Addendum 1" :type :int}
{:option "b" :as "Addendum 2" :type :int :default 0}]
:commands [{:command "plain"
:description "Adds two numbers"
:runs add_plain}
{:command "double"
:description "Adds two numbers twice"
:runs add_twice}]}
{:command "sub"
:description "Subtracts parameter B from A"
:opts [{:option "a" :as "Parameter A" :type :int :default 0}
{:option "b" :as "Parameter B" :type :int :default 0}]
:runs subtract_numbers}
]})
}
It would be relatively simple to transform today's globals and commands into this new data structure, where you can run:
cmd -?
cmd add -?
cmd add -a 1 -b 2 plain
cmd add -a 1 -b 2 double
cmd sub -?
All attributes going UP the tree are globals for your command; leaf trees have a :runs
function, while branches have a :commands
list
Hi,
Do you have an ETA for this feature? I'm new to clojure and wanted to port a CLI app to clojure and compile it to native. Found cli-matic and I like it, however my CLI makes use of sub-commands. For reference, it's written in TypeScript with https://oclif.io/ .
Thanks, Eugen
@l3nz : let me know if I can help out with testing and/or maybe developing. If you are open to a PR let me know if you have any other constrains besides the ones in the CONTRIBUTING guide.
Making some progress - see https://github.com/l3nz/cli-matic/blob/b69_nested/TODO.md
First snapshot out - most likely not working, but in theory is should work with current configuration. https://clojars.org/cli-matic/versions/0.4.0.1-SNAPSHOT
ATM, as I had no feedback and the first version was basically working for me, I'm publishing v0.4.0. I have not yet documented the new format, but the old one will continue to be supported unless you want to use the nested format. So you got a new engine under the same old hood.
There are surely some issues, but until this goes in front of real people they will not be found, so let's click on "I'm feeling lucky".
Awesome. I'm still catching up on things but I will give it a try.
At the moment we can have
but it would be cool if you could nest sub-commands,
So that calling "run" would print out the subcommands for "run" and "run alpha" or "run beta" would be actual sub-commmands.
This means that either you have a definition for :runs as a fn (for a terminal command) or you would have another :commands containing a list of command definitions, like in: