Skip to content

Allow repeatable subcommands? #454

@idanarye

Description

@idanarye

I 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 = , rotate = )
  • FileCommand(name = "B.pdf", count = 3, rotate = )
  • FileCommand(name = "C.pdf", count = 3, rotate = "left")
  • FileCommand(name = "D.pdf", count = , rotate = )
  • FileCommand(name = "E.pdf", count = , 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?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions