|
1 | 1 | package org.utplsql.cli; |
2 | 2 |
|
3 | | -import com.beust.jcommander.JCommander; |
4 | | -import com.beust.jcommander.ParameterException; |
| 3 | +import picocli.CommandLine; |
| 4 | + |
| 5 | +import java.util.List; |
5 | 6 |
|
6 | 7 | public class Cli { |
7 | 8 |
|
8 | 9 | static final int DEFAULT_ERROR_CODE = 1; |
9 | 10 |
|
10 | | - static final String HELP_CMD = "-h"; |
11 | | - |
12 | 11 | public static void main(String[] args) { |
13 | 12 |
|
14 | | - int exitCode = runWithExitCode(args); |
| 13 | + int exitCode = runPicocliWithExitCode(args); |
15 | 14 |
|
16 | 15 | System.exit(exitCode); |
17 | 16 | } |
18 | 17 |
|
19 | | - static int runWithExitCode( String[] args ) { |
| 18 | + static int runPicocliWithExitCode(String[] args) { |
20 | 19 |
|
21 | 20 | LoggerConfiguration.configure(LoggerConfiguration.ConfigLevel.NONE); |
22 | 21 | LocaleInitializer.initLocale(); |
23 | 22 |
|
24 | | - JCommander jc = new JCommander(); |
25 | | - jc.setProgramName("utplsql"); |
26 | | - |
27 | | - CommandProvider cmdProvider = new CommandProvider(jc); |
28 | | - |
29 | | - cmdProvider.commands().forEach(cmd -> jc.addCommand(cmd.getCommand(), cmd)); |
| 23 | + CommandLine commandLine = new CommandLine(UtplsqlPicocliCommand.class); |
| 24 | + commandLine.setTrimQuotes(true); |
30 | 25 |
|
31 | 26 | int exitCode = DEFAULT_ERROR_CODE; |
32 | 27 |
|
33 | | - if ( args.length >= 1 && args[0].equals("-h") ) // Help? |
34 | | - { |
35 | | - exitCode = 0; |
36 | | - jc.usage(); |
37 | | - } |
38 | | - else { |
39 | | - try { |
40 | | - jc.parse(args); |
41 | | - |
42 | | - exitCode = cmdProvider.getCommand(jc.getParsedCommand()).run(); |
| 28 | + try { |
| 29 | + |
| 30 | + List<CommandLine> parsedLines = commandLine.parse(args); |
| 31 | + |
| 32 | + boolean commandWasRun = false; |
| 33 | + for (CommandLine parsedLine : parsedLines) { |
| 34 | + if (parsedLine.isUsageHelpRequested()) { |
| 35 | + parsedLine.usage(System.out); |
| 36 | + return 0; |
| 37 | + } else if (parsedLine.isVersionHelpRequested()) { |
| 38 | + parsedLine.printVersionHelp(System.out); |
| 39 | + return 0; |
| 40 | + } |
| 41 | + |
| 42 | + Object command = parsedLine.getCommand(); |
| 43 | + if (command instanceof ICommand) { |
| 44 | + exitCode = ((ICommand) command).run(); |
| 45 | + commandWasRun = true; |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
43 | 49 |
|
44 | | - } catch (ParameterException e) { |
45 | | - exitCode = new HelpCommand(jc, e.getMessage()).run(); |
46 | | - } catch (Exception e) { |
47 | | - e.printStackTrace(); |
| 50 | + if (!commandWasRun) { |
| 51 | + commandLine.usage(System.out); |
| 52 | + } |
| 53 | + } catch (CommandLine.ParameterException e) { |
| 54 | + System.err.println(e.getMessage()); |
| 55 | + if (!CommandLine.UnmatchedArgumentException.printSuggestions(e, System.err)) { |
| 56 | + e.getCommandLine().usage(System.err); |
48 | 57 | } |
| 58 | + } catch (Exception e) { |
| 59 | + e.printStackTrace(); |
49 | 60 | } |
50 | 61 |
|
51 | 62 | return exitCode; |
|
0 commit comments