38
38
import org .apache .commons .cli .Option ;
39
39
import org .apache .commons .cli .Options ;
40
40
import org .apache .commons .cli .ParseException ;
41
+ import org .apache .solr .client .api .util .SolrVersion ;
41
42
import org .apache .solr .client .solrj .SolrClient ;
42
43
import org .apache .solr .client .solrj .request .ContentStreamUpdateRequest ;
43
44
import org .apache .solr .common .util .ContentStreamBase ;
45
+ import org .apache .solr .common .util .EnvUtils ;
44
46
import org .apache .solr .common .util .NamedList ;
45
47
import org .apache .solr .util .configuration .SSLConfigurationsFactory ;
46
48
import org .slf4j .Logger ;
47
49
import org .slf4j .LoggerFactory ;
48
50
49
51
/** Command-line utility for working with Solr. */
52
+ @ picocli .CommandLine .Command (
53
+ name = "solr" ,
54
+ version = "Apache Solr version " + SolrVersion .LATEST_STRING ,
55
+ mixinStandardHelpOptions = true ,
56
+ commandListHeading = "\n Commands:\n " ,
57
+ descriptionHeading = "Global options:\n " ,
58
+ footer = {
59
+ "" ,
60
+ "SolrCloud example (embedded Zookeeper):" ,
61
+ "" ,
62
+ " ./solr start -c" ,
63
+ "" ,
64
+ "For more help on how to use Solr, head to https://solr.apache.org/"
65
+ },
66
+ usageHelpAutoWidth = true ,
67
+ defaultValueProvider = ZkTool .ZkHostDefaultValueProvider .class ,
68
+ subcommands = {StatusTool .class , HealthcheckTool .class , VersionTool .class , ZkTool .class })
50
69
public class SolrCLI implements CLIO {
51
70
52
71
private static final Logger log = LoggerFactory .getLogger (MethodHandles .lookup ().lookupClass ());
53
72
73
+ @ picocli .CommandLine .Option (
74
+ names = {"-v" , "--version" },
75
+ versionHelp = true ,
76
+ description = "Print version information and exit" )
77
+ boolean versionRequested ;
78
+
79
+ @ picocli .CommandLine .Option (
80
+ names = {"-h" , "--help" },
81
+ usageHelp = true ,
82
+ description = "Display this help message" )
83
+ boolean helpRequested ;
84
+
54
85
public static void exit (int exitStatus ) {
55
86
try {
56
87
System .exit (exitStatus );
@@ -62,6 +93,25 @@ public static void exit(int exitStatus) {
62
93
63
94
/** Runs a tool. */
64
95
public static void main (String [] args ) throws Exception {
96
+ if (EnvUtils .getPropertyAsBool ("solr.picocli" , true )) {
97
+ CLIO .err ("Using picocli" );
98
+ SSLConfigurationsFactory .current ().init ();
99
+ picocli .CommandLine commandLine = new picocli .CommandLine (new SolrCLI ());
100
+ exit (commandLine .execute (args ));
101
+ } else {
102
+ CLIO .err ("DEBUG: Using commons-cli" );
103
+ exit (parseWithCommonsCli (args ));
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Parses the command-line arguments passed by the user using Apache Commons CLI. This
109
+ *
110
+ * @param args the original command-line arguments
111
+ * @deprecated Please use picocli
112
+ */
113
+ @ Deprecated (since = "9.9" )
114
+ public static int parseWithCommonsCli (String [] args ) throws Exception {
65
115
final boolean hasNoCommand =
66
116
args == null || args .length == 0 || args [0 ] == null || args [0 ].trim ().length () == 0 ;
67
117
final boolean isHelpCommand = !hasNoCommand && Arrays .asList ("-h" , "--help" ).contains (args [0 ]);
@@ -117,15 +167,17 @@ public static void main(String[] args) throws Exception {
117
167
System .exit (1 );
118
168
}
119
169
CommandLine cli = parseCmdLine (tool , args );
120
- System . exit ( tool .runTool (cli ) );
170
+ return tool .runTool (cli );
121
171
}
122
172
173
+ @ Deprecated
123
174
public static Tool findTool (String [] args ) throws Exception {
124
175
String toolType = args [0 ].trim ().toLowerCase (Locale .ROOT );
125
176
return newTool (toolType );
126
177
}
127
178
128
- public static CommandLine parseCmdLine (Tool tool , String [] args ) {
179
+ @ Deprecated
180
+ public static org .apache .commons .cli .CommandLine parseCmdLine (Tool tool , String [] args ) {
129
181
// the parser doesn't like -D props
130
182
List <String > toolArgList = new ArrayList <>();
131
183
List <String > dashDList = new ArrayList <>();
@@ -140,7 +192,7 @@ public static CommandLine parseCmdLine(Tool tool, String[] args) {
140
192
String [] toolArgs = toolArgList .toArray (new String [0 ]);
141
193
142
194
// process command-line args to configure this application
143
- CommandLine cli = processCommandLineArgs (tool , toolArgs );
195
+ org . apache . commons . cli . CommandLine cli = processCommandLineArgs (tool , toolArgs );
144
196
145
197
List <String > argList = cli .getArgList ();
146
198
argList .addAll (dashDList );
@@ -179,6 +231,7 @@ protected static void checkSslStoreSysProp(String solrInstallDir, String key) {
179
231
}
180
232
181
233
// Creates an instance of the requested tool, using classpath scanning if necessary
234
+ @ Deprecated
182
235
private static Tool newTool (String toolType ) throws Exception {
183
236
if ("healthcheck" .equals (toolType )) return new HealthcheckTool ();
184
237
else if ("status" .equals (toolType )) return new StatusTool ();
@@ -232,7 +285,7 @@ private static Tool newTool(String toolType) throws Exception {
232
285
* CLI option.
233
286
*/
234
287
public static String getOptionWithDeprecatedAndDefault (
235
- CommandLine cli , Option opt , Option deprecated , String def ) {
288
+ org . apache . commons . cli . CommandLine cli , Option opt , Option deprecated , String def ) {
236
289
String val = cli .getOptionValue (opt );
237
290
if (val == null ) {
238
291
val = cli .getOptionValue (deprecated );
@@ -242,6 +295,7 @@ public static String getOptionWithDeprecatedAndDefault(
242
295
243
296
// TODO: SOLR-17429 - remove the custom logic when Commons CLI is upgraded and
244
297
// makes stderr the default, or makes Option.toDeprecatedString() public.
298
+ @ Deprecated
245
299
private static void deprecatedHandlerStdErr (Option o ) {
246
300
// Deprecated options without a description act as "stealth" options
247
301
if (o .isDeprecated () && !o .getDeprecated ().getDescription ().isBlank ()) {
@@ -256,10 +310,12 @@ private static void deprecatedHandlerStdErr(Option o) {
256
310
}
257
311
258
312
/** Parses the command-line arguments passed by the user. */
259
- public static CommandLine processCommandLineArgs (Tool tool , String [] args ) {
313
+ @ Deprecated
314
+ public static org .apache .commons .cli .CommandLine processCommandLineArgs (
315
+ Tool tool , String [] args ) {
260
316
Options options = tool .getOptions ();
261
317
262
- CommandLine cli = null ;
318
+ org . apache . commons . cli . CommandLine cli = null ;
263
319
try {
264
320
cli =
265
321
DefaultParser .builder ()
@@ -296,6 +352,7 @@ public static CommandLine processCommandLineArgs(Tool tool, String[] args) {
296
352
}
297
353
298
354
/** Prints tool help for a given tool */
355
+ @ Deprecated
299
356
public static void printToolHelp (Tool tool ) {
300
357
HelpFormatter formatter = getFormatter ();
301
358
Options nonDeprecatedOptions = new Options ();
@@ -314,13 +371,19 @@ public static void printToolHelp(Tool tool) {
314
371
autoGenerateUsage );
315
372
}
316
373
374
+ @ Deprecated
317
375
public static HelpFormatter getFormatter () {
318
376
HelpFormatter formatter = HelpFormatter .builder ().get ();
319
377
formatter .setWidth (120 );
320
378
return formatter ;
321
379
}
322
380
323
- /** Scans Jar files on the classpath for Tool implementations to activate. */
381
+ /**
382
+ * Scans Jar files on the classpath for Tool implementations to activate.
383
+ *
384
+ * @deprecated With Picocli we no longer need to scan the classpath for Tool implementations?
385
+ */
386
+ @ Deprecated
324
387
private static List <Class <? extends Tool >> findToolClassesInPackage (String packageName ) {
325
388
List <Class <? extends Tool >> toolClasses = new ArrayList <>();
326
389
try {
@@ -344,6 +407,7 @@ private static List<Class<? extends Tool>> findToolClassesInPackage(String packa
344
407
return toolClasses ;
345
408
}
346
409
410
+ @ Deprecated
347
411
private static Set <String > findClasses (String path , String packageName ) throws Exception {
348
412
Set <String > classes = new TreeSet <>();
349
413
if (path .startsWith ("file:" ) && path .contains ("!" )) {
@@ -402,6 +466,7 @@ public static String uptime(long uptimeMs) {
402
466
numSeconds );
403
467
}
404
468
469
+ @ Deprecated
405
470
private static void printHelp () {
406
471
407
472
print ("Usage: solr COMMAND OPTIONS" );
0 commit comments