This repository was archived by the owner on May 11, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathCli.java
More file actions
126 lines (116 loc) · 3.78 KB
/
Copy pathCli.java
File metadata and controls
126 lines (116 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.cli;
import io.jooby.internal.cli.CommandContextImpl;
import io.jooby.internal.cli.JLineCompleter;
import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.ParsedLine;
import org.jline.reader.UserInterruptException;
import org.jline.reader.impl.DefaultParser;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import picocli.CommandLine;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* Application console.
*
* Usage:
* <pre>{@code
* jooby> --help
* Usage: jooby [-hV] [COMMAND]
* -h, --help Show this help message and exit.
* -V, --version Print version information and exit.
* Commands:
* create Creates a new application
* exit Exit console
* }</pre>
*
* @since 2.0.6
*/
@CommandLine.Command(
name = "jooby",
versionProvider = Version.class,
mixinStandardHelpOptions = true,
version = "Print version information"
)
public class Cli extends Cmd {
/** Command line specification. */
private @CommandLine.Spec CommandLine.Model.CommandSpec spec;
/** Unmatched command line arguments. */
private @CommandLine.Unmatched List<String> args;
@Override public void run(@Nonnull Context ctx) {
List<String> args = this.args.stream()
.filter(Objects::nonNull)
.map(String::trim)
.filter(it -> it.length() > 0)
.collect(Collectors.toList());
if (args.size() > 0) {
String arg = args.get(0);
if ("-h".equals(arg) || "--help".equals(arg)) {
ctx.println(spec.commandLine().getUsageMessage());
} else if ("-V".equalsIgnoreCase(arg) || "--version".equals(arg)) {
ctx.println(ctx.getVersion());
} else {
ctx.println(
"Unknown command or option(s): " + args.stream().collect(Collectors.joining(" ")));
ctx.println(spec.commandLine().getUsageMessage());
}
} else {
ctx.println(spec.commandLine().getUsageMessage());
}
}
/**
* Start a jooby console or execute given arguments and exits.
*
* @param args Command line arguments.
* @throws IOException If something goes wrong.
*/
public static void main(String[] args) throws IOException {
// set up the completion
Cli jooby = new Cli();
CommandLine cmd = new CommandLine(jooby)
.addSubcommand(new CreateCmd())
.addSubcommand(new ExitCmd())
.addSubcommand(new SetCmd());
Terminal terminal = TerminalBuilder.builder().build();
LineReader reader = LineReaderBuilder.builder()
.terminal(terminal)
.completer(new JLineCompleter(cmd.getCommandSpec()))
.parser(new DefaultParser())
.build();
CommandContextImpl context = new CommandContextImpl(reader, Version.VERSION);
jooby.setContext(context);
cmd.getSubcommands().values().stream()
.map(CommandLine::getCommand)
.filter(Cmd.class::isInstance)
.map(Cmd.class::cast)
.forEach(command -> command.setContext(context));
if (args.length > 0) {
cmd.execute(args);
} else {
String prompt = "jooby> ";
// start the shell and process input until the user quits with Ctl-D
while (true) {
try {
String line = reader.readLine(prompt);
ParsedLine pl = reader.getParser().parse(line, 0);
String[] arguments = pl.words().toArray(new String[0]);
cmd.execute(arguments);
} catch (UserInterruptException e) {
System.exit(0);
} catch (EndOfFileException e) {
return;
}
}
}
}
}