Allow repeatable subcommands?
See original GitHub issueI came here looking for a solution for a problem similar to #358 - the need for a syntax for lists of multi-field entries - but had a different solution in mind. These solutions are not mutually exclusive, so I elected to open a new ticket for it.
My idea is to have repeatable subcommands. So - if we take the example from #358 - print
will be the main command and have, say, file
as a subcommand. We will write it like so:
print --paper A4 \
file A.pdf \
file B.pdf --count 3 \
file C.pdf --count 3 --rotate left \
file D.pdf \
file E.pdf --rotate right
This will create 6 CommandLine
objects:
- PrintCommand(paper = “A4”)
- FileCommand(name = “A.pdf”, count = <default>, rotate = <default>)
- FileCommand(name = “B.pdf”, count = 3, rotate = <default>)
- FileCommand(name = “C.pdf”, count = 3, rotate = “left”)
- FileCommand(name = “D.pdf”, count = <default>, rotate = <default>)
- FileCommand(name = “E.pdf”, count = <default>, rotate = “right”)
A bit more verbose than @kravemir’s original syntax (since you need to specify the options for each file) but much more readable IMHO.
I tried to do it by making file
a subcommand of itself:
CommandLine printCommand = new CommandLine(new PrintCommand());
CommandLine fileCommand = new CommandLine(new FileCommand());
printCommand.addSubcommand(fileCommand);
fileCommand.addSubcommand(fileCommand);
And it did parse that command line - but it was always using the same FileCommand
object so I only got E.pdf
’s parameters. Maybe if a CommandLine
could receive a factory for objects, and construct a new one for each subcommand it parses?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:30 (18 by maintainers)
@idanarye, @kravemir, @hanslovsky, @lakemove, All,
picocli 4.2.0 has been released, including this feature. Enjoy!
I’m already abusing them enough as is, so I’m not going to request any farther complications.