1 package org
.argeo
.cms
.cli
;
3 import java
.io
.StringWriter
;
4 import java
.util
.Arrays
;
6 import java
.util
.function
.Function
;
8 import org
.apache
.commons
.cli
.CommandLine
;
9 import org
.apache
.commons
.cli
.DefaultParser
;
10 import org
.apache
.commons
.cli
.Options
;
11 import org
.apache
.commons
.cli
.ParseException
;
13 /** A command that can be described. */
14 public interface DescribedCommand
<T
> extends Function
<List
<String
>, T
> {
15 default Options
getOptions() {
19 String
getDescription();
21 default String
getUsage() {
25 default String
getExamples() {
29 default CommandLine
toCommandLine(List
<String
> args
) {
31 DefaultParser parser
= new DefaultParser();
32 return parser
.parse(getOptions(), args
.toArray(new String
[args
.size()]));
33 } catch (ParseException e
) {
34 throw new CommandArgsException(e
);
38 /** In order to implement quickly a main method. */
39 public static void mainImpl(DescribedCommand
<?
> command
, String
[] args
) {
41 Object output
= command
.apply(Arrays
.asList(args
));
42 System
.out
.println(output
);
44 } catch (IllegalArgumentException e
) {
45 StringWriter out
= new StringWriter();
46 HelpCommand
.printHelp(command
, out
);
47 System
.err
.println(out
.toString());
49 } catch (Exception e
) {