1 package org
.argeo
.api
.cli
;
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
.MissingOptionException
;
16 import org
.apache
.commons
.cli
.Options
;
17 import org
.apache
.commons
.cli
.ParseException
;
19 /** Base class for a CLI managing sub commands. */
20 public abstract class CommandsCli
implements DescribedCommand
<Object
> {
21 private final String commandName
;
22 private Map
<String
, Function
<List
<String
>, ?
>> commands
= new TreeMap
<>();
24 protected final Options options
= new Options();
26 public CommandsCli(String commandName
) {
27 this.commandName
= commandName
;
31 public Object
apply(List
<String
> args
) {
33 List
<String
> newArgs
= new ArrayList
<>();
34 boolean isHelpOption
= false;
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
.equals("--" + HelpCommand
.HELP_OPTION
.getLongOpt())) {
45 if (!arg
.startsWith("-") && cmd
== null) {
51 } catch (ParseException e
) {
52 CommandArgsException cae
= new CommandArgsException(e
);
56 Function
<List
<String
>, ?
> function
= cmd
!= null ?
getCommand(cmd
) : getDefaultCommand();
59 if (!(function
instanceof CommandsCli
))
60 if (function
instanceof DescribedCommand
<?
> command
)
62 throw new PrintHelpRequestException(cmd
, this);
63 // StringWriter out = new StringWriter();
64 // HelpCommand.printHelp(command, out);
65 // System.out.println(out.toString());
70 throw new IllegalArgumentException("Uknown command " + cmd
);
72 Object value
= function
.apply(newArgs
);
73 return value
!= null ? value
.toString() : null;
74 } catch (CommandArgsException e
) {
75 if (e
.getCommandName() == null) {
76 e
.setCommandName(cmd
);
77 e
.setCommandsCli(this);
80 } catch (IllegalArgumentException e
) {
81 CommandArgsException cae
= new CommandArgsException(e
);
82 cae
.setCommandName(cmd
);
88 public Options
getOptions() {
92 protected void addCommand(String cmd
, Function
<List
<String
>, ?
> function
) {
93 commands
.put(cmd
, function
);
98 public String
getUsage() {
102 protected void addCommandsCli(CommandsCli commandsCli
) {
103 addCommand(commandsCli
.getCommandName(), commandsCli
);
104 commandsCli
.addCommand(HelpCommand
.HELP
, new HelpCommand(this, commandsCli
));
107 public String
getCommandName() {
111 public Set
<String
> getSubCommands() {
112 return commands
.keySet();
115 public Function
<List
<String
>, ?
> getCommand(String command
) {
116 return commands
.get(command
);
119 public HelpCommand
getHelpCommand() {
120 return (HelpCommand
) getCommand(HelpCommand
.HELP
);
123 public Function
<List
<String
>, String
> getDefaultCommand() {
124 return getHelpCommand();
127 /** In order to implement quickly a main method. */
128 public static void mainImpl(CommandsCli cli
, String
[] args
) {
130 cli
.addCommand(HelpCommand
.HELP
, new HelpCommand(null, cli
));
131 Object output
= cli
.apply(Arrays
.asList(args
));
133 System
.out
.println(output
);
135 } catch (PrintHelpRequestException e
) {
136 StringWriter out
= new StringWriter();
137 HelpCommand
.printHelp(e
.getCommandsCli(), e
.getCommandName(), out
);
138 System
.out
.println(out
.toString());
139 } catch (CommandArgsException e
) {
140 System
.err
.println("Wrong arguments " + Arrays
.toString(args
) + ": " + e
.getMessage());
141 Throwable cause
= e
.getCause();
142 if (!(cause
instanceof MissingOptionException
))
144 if (e
.getCommandName() != null) {
145 StringWriter out
= new StringWriter();
146 HelpCommand
.printHelp(e
.getCommandsCli(), e
.getCommandName(), out
);
147 System
.err
.println(out
.toString());
152 } catch (Exception e
) {