3 import java
.io
.StringWriter
;
4 import java
.util
.ArrayList
;
5 import java
.util
.Arrays
;
9 import java
.util
.TreeMap
;
10 import java
.util
.function
.Function
;
12 import org
.apache
.commons
.cli
.CommandLine
;
13 import org
.apache
.commons
.cli
.CommandLineParser
;
14 import org
.apache
.commons
.cli
.DefaultParser
;
15 import org
.apache
.commons
.cli
.Options
;
16 import org
.apache
.commons
.cli
.ParseException
;
18 /** Base class for a CLI managing sub commands. */
19 public abstract class CommandsCli
implements DescribedCommand
<Object
> {
20 public final static String HELP
= "help";
22 private final String commandName
;
23 private Map
<String
, Function
<List
<String
>, ?
>> commands
= new TreeMap
<>();
25 protected final Options options
= new Options();
27 public CommandsCli(String commandName
) {
28 this.commandName
= commandName
;
32 public Object
apply(List
<String
> args
) {
34 List
<String
> newArgs
= new ArrayList
<>();
36 CommandLineParser clParser
= new DefaultParser();
37 CommandLine commonCl
= clParser
.parse(getOptions(), args
.toArray(new String
[args
.size()]), true);
38 List
<String
> leftOvers
= commonCl
.getArgList();
39 for (String arg
: leftOvers
) {
40 if (!arg
.startsWith("-") && cmd
== null) {
46 } catch (ParseException e
) {
47 CommandArgsException cae
= new CommandArgsException(e
);
51 Function
<List
<String
>, ?
> function
= cmd
!= null ?
getCommand(cmd
) : getDefaultCommand();
53 throw new IllegalArgumentException("Uknown command " + cmd
);
55 return function
.apply(newArgs
).toString();
56 } catch (CommandArgsException e
) {
57 if (e
.getCommandName() == null) {
58 e
.setCommandName(cmd
);
59 e
.setCommandsCli(this);
62 } catch (IllegalArgumentException e
) {
63 CommandArgsException cae
= new CommandArgsException(e
);
64 cae
.setCommandName(cmd
);
70 public Options
getOptions() {
74 protected void addCommand(String cmd
, Function
<List
<String
>, ?
> function
) {
75 commands
.put(cmd
, function
);
80 public String
getUsage() {
84 protected void addCommandsCli(CommandsCli commandsCli
) {
85 addCommand(commandsCli
.getCommandName(), commandsCli
);
86 commandsCli
.addCommand(HELP
, new HelpCommand(this, commandsCli
));
89 public String
getCommandName() {
93 public Set
<String
> getSubCommands() {
94 return commands
.keySet();
97 public Function
<List
<String
>, ?
> getCommand(String command
) {
98 return commands
.get(command
);
101 public HelpCommand
getHelpCommand() {
102 return (HelpCommand
) getCommand(HELP
);
105 public Function
<List
<String
>, String
> getDefaultCommand() {
106 return getHelpCommand();
109 /** In order to implement quickly a main method. */
110 public static void mainImpl(CommandsCli cli
, String
[] args
) {
112 cli
.addCommand(CommandsCli
.HELP
, new HelpCommand(null, cli
));
113 Object output
= cli
.apply(Arrays
.asList(args
));
114 System
.out
.println(output
);
116 } catch (CommandArgsException e
) {
117 System
.err
.println("Wrong arguments " + Arrays
.toString(args
) + ": " + e
.getMessage());
118 if (e
.getCommandName() != null) {
119 StringWriter out
= new StringWriter();
120 HelpCommand
.printHelp(e
.getCommandsCli(), e
.getCommandName(), out
);
121 System
.err
.println(out
.toString());
126 } catch (Exception e
) {