@@ -54,6 +54,7 @@ public static void main(String[] argsArray) throws Exception {
5454 String className = args .remove (0 );
5555
5656 boolean printLaunchCommand ;
57+ boolean printUsage ;
5758 AbstractCommandBuilder builder ;
5859 try {
5960 if (className .equals ("org.apache.spark.deploy.SparkSubmit" )) {
@@ -62,9 +63,11 @@ public static void main(String[] argsArray) throws Exception {
6263 builder = new SparkClassCommandBuilder (className , args );
6364 }
6465 printLaunchCommand = !isEmpty (System .getenv ("SPARK_PRINT_LAUNCH_COMMAND" ));
66+ printUsage = false ;
6567 } catch (IllegalArgumentException e ) {
66- builder = new UsageLauncher ( );
68+ builder = new UsageCommandBuilder ( e . getMessage () );
6769 printLaunchCommand = false ;
70+ printUsage = true ;
6871 }
6972
7073 Map <String , String > env = new HashMap <String , String >();
@@ -75,7 +78,13 @@ public static void main(String[] argsArray) throws Exception {
7578 }
7679
7780 if (isWindows ()) {
78- System .out .println (prepareWindowsCommand (cmd , env ));
81+ // When printing the usage message, we can't use "cmd /v" since that prevents the env
82+ // variable from being seen in the caller script. So do not call prepareWindowsCommand().
83+ if (printUsage ) {
84+ System .out .println (join (" " , cmd ));
85+ } else {
86+ System .out .println (prepareWindowsCommand (cmd , env ));
87+ }
7988 } else {
8089 // In bash, use NULL as the arg separator since it cannot be used in an argument.
8190 List <String > bashCmd = prepareBashCommand (cmd , env );
@@ -133,23 +142,29 @@ private static List<String> prepareBashCommand(List<String> cmd, Map<String, Str
133142 * Internal launcher used when command line parsing fails. This will behave differently depending
134143 * on the platform:
135144 *
136- * - On Unix-like systems, it will print a call to the "usage" function with argument "1". The
137- * function is expected to print the command's usage and exit with the provided exit code.
138- * The script should use "export -f usage" after declaring a function called "usage", so that
139- * the function is available to downstream scripts.
145+ * - On Unix-like systems, it will print a call to the "usage" function with two arguments: the
146+ * the error string, and the exit code to use. The function is expected to print the command's
147+ * usage and exit with the provided exit code. The script should use "export -f usage" after
148+ * declaring a function called "usage", so that the function is available to downstream scripts.
140149 *
141- * - On Windows it will set the variable "SPARK_LAUNCHER_USAGE_ERROR" to "1". The batch script
142- * should check for this variable and print its usage, since batch scripts don't really support
143- * the "export -f" functionality used in bash.
150+ * - On Windows it will set the variable "SPARK_LAUNCHER_USAGE_ERROR" to the usage error message.
151+ * The batch script should check for this variable and print its usage, since batch scripts
152+ * don't really support the "export -f" functionality used in bash.
144153 */
145- private static class UsageLauncher extends AbstractCommandBuilder {
154+ private static class UsageCommandBuilder extends AbstractCommandBuilder {
155+
156+ private final String message ;
157+
158+ UsageCommandBuilder (String message ) {
159+ this .message = message ;
160+ }
146161
147162 @ Override
148163 public List <String > buildCommand (Map <String , String > env ) {
149164 if (isWindows ()) {
150- return Arrays .asList ("set" , "SPARK_LAUNCHER_USAGE_ERROR=1" );
165+ return Arrays .asList ("set" , "SPARK_LAUNCHER_USAGE_ERROR=" + message );
151166 } else {
152- return Arrays .asList ("usage" , "1" );
167+ return Arrays .asList ("usage" , message , "1" );
153168 }
154169 }
155170
0 commit comments