]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.cli/src/org/argeo/api/cli/CommandsCli.java
Simplify CMS RCP
[lgpl/argeo-commons.git] / org.argeo.api.cli / src / org / argeo / api / cli / CommandsCli.java
1 package org.argeo.api.cli;
2
3 import java.io.StringWriter;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.TreeMap;
10 import java.util.function.Function;
11
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;
18
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<>();
23
24 protected final Options options = new Options();
25
26 public CommandsCli(String commandName) {
27 this.commandName = commandName;
28 }
29
30 @Override
31 public Object apply(List<String> args) {
32 String cmd = null;
33 List<String> newArgs = new ArrayList<>();
34 boolean isHelpOption = false;
35 try {
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())) {
41 isHelpOption = true;
42 // TODO break?
43 }
44
45 if (!arg.startsWith("-") && cmd == null) {
46 cmd = arg;
47 } else {
48 newArgs.add(arg);
49 }
50 }
51 } catch (ParseException e) {
52 CommandArgsException cae = new CommandArgsException(e);
53 throw cae;
54 }
55
56 Function<List<String>, ?> function = cmd != null ? getCommand(cmd) : getDefaultCommand();
57
58 // --help option
59 if (!(function instanceof CommandsCli))
60 if (function instanceof DescribedCommand<?> command)
61 if (isHelpOption) {
62 throw new PrintHelpRequestException(cmd, this);
63 // StringWriter out = new StringWriter();
64 // HelpCommand.printHelp(command, out);
65 // System.out.println(out.toString());
66 // return null;
67 }
68
69 if (function == null)
70 throw new IllegalArgumentException("Uknown command " + cmd);
71 try {
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);
78 }
79 throw e;
80 } catch (IllegalArgumentException e) {
81 CommandArgsException cae = new CommandArgsException(e);
82 cae.setCommandName(cmd);
83 throw cae;
84 }
85 }
86
87 @Override
88 public Options getOptions() {
89 return options;
90 }
91
92 protected void addCommand(String cmd, Function<List<String>, ?> function) {
93 commands.put(cmd, function);
94
95 }
96
97 @Override
98 public String getUsage() {
99 return "[command]";
100 }
101
102 protected void addCommandsCli(CommandsCli commandsCli) {
103 addCommand(commandsCli.getCommandName(), commandsCli);
104 commandsCli.addCommand(HelpCommand.HELP, new HelpCommand(this, commandsCli));
105 }
106
107 public String getCommandName() {
108 return commandName;
109 }
110
111 public Set<String> getSubCommands() {
112 return commands.keySet();
113 }
114
115 public Function<List<String>, ?> getCommand(String command) {
116 return commands.get(command);
117 }
118
119 public HelpCommand getHelpCommand() {
120 return (HelpCommand) getCommand(HelpCommand.HELP);
121 }
122
123 public Function<List<String>, String> getDefaultCommand() {
124 return getHelpCommand();
125 }
126
127 /** In order to implement quickly a main method. */
128 public static void mainImpl(CommandsCli cli, String[] args) {
129 try {
130 cli.addCommand(HelpCommand.HELP, new HelpCommand(null, cli));
131 Object output = cli.apply(Arrays.asList(args));
132 if (output != null)
133 System.out.println(output);
134 System.exit(0);
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))
143 e.printStackTrace();
144 if (e.getCommandName() != null) {
145 StringWriter out = new StringWriter();
146 HelpCommand.printHelp(e.getCommandsCli(), e.getCommandName(), out);
147 System.err.println(out.toString());
148 } else {
149 e.printStackTrace();
150 }
151 System.exit(1);
152 } catch (Exception e) {
153 e.printStackTrace();
154 System.exit(1);
155 }
156 }
157 }