10
10
package org .truffleruby .launcher ;
11
11
12
12
import java .io .PrintStream ;
13
+ import java .io .IOException ;
13
14
import java .util .ArrayList ;
14
15
import java .util .Arrays ;
15
16
import java .util .Collections ;
16
17
import java .util .List ;
17
18
import java .util .Map ;
18
19
import java .util .Set ;
20
+ import java .lang .ProcessBuilder .Redirect ;
19
21
20
22
import org .graalvm .launcher .AbstractLanguageLauncher ;
21
23
import org .graalvm .nativeimage .ProcessProperties ;
@@ -33,6 +35,7 @@ public class RubyLauncher extends AbstractLanguageLauncher {
33
35
34
36
private CommandLineOptions config ;
35
37
private String implementationName = null ;
38
+ private boolean helpOptionUsed ;
36
39
37
40
public static void main (String [] args ) {
38
41
new RubyLauncher ().launch (args );
@@ -163,7 +166,7 @@ protected void collectArguments(Set<String> options) {
163
166
164
167
@ Override
165
168
protected void printHelp (OptionCategory maxCategory ) {
166
- printHelp (System . out );
169
+ printHelp (getOutput () );
167
170
}
168
171
169
172
@ Override
@@ -173,6 +176,47 @@ protected AbortException abortUnrecognizedArgument(String argument) {
173
176
"truffleruby: invalid option " + argument + " (Use --help for usage instructions.)" );
174
177
}
175
178
179
+ @ Override
180
+ protected boolean parseCommonOption (String defaultOptionPrefix , Map <String , String > polyglotOptions ,
181
+ boolean experimentalOptions , String arg ) {
182
+ if (arg .startsWith ("--help" )) {
183
+ helpOptionUsed = true ;
184
+ }
185
+
186
+ return super .parseCommonOption (defaultOptionPrefix , polyglotOptions , experimentalOptions , arg );
187
+ }
188
+
189
+ @ Override
190
+ protected boolean runLauncherAction () {
191
+ if (!helpOptionUsed || System .console () == null ) {
192
+ return super .runLauncherAction ();
193
+ }
194
+
195
+ String pager = getPagerFromEnv ();
196
+ if (pager == null || pager .length () == 0 ) {
197
+ return super .runLauncherAction ();
198
+ }
199
+
200
+ try {
201
+ Process process = new ProcessBuilder (pager )
202
+ .redirectOutput (Redirect .INHERIT )
203
+ .redirectErrorStream (true )
204
+ .start ();
205
+ PrintStream out = new PrintStream (process .getOutputStream ());
206
+
207
+ setOutput (out );
208
+ boolean code = super .runLauncherAction ();
209
+
210
+ out .flush ();
211
+ out .close ();
212
+ process .waitFor ();
213
+
214
+ return code ;
215
+ } catch (IOException | InterruptedException e ) {
216
+ throw abort (e );
217
+ }
218
+ }
219
+
176
220
private int runRubyMain (Context .Builder contextBuilder , CommandLineOptions config ) {
177
221
if (config .executionAction == ExecutionAction .UNSET ) {
178
222
switch (config .defaultExecutionAction ) {
@@ -299,6 +343,20 @@ private static List<String> getPathListFromEnvVariable(String name) {
299
343
return Collections .emptyList ();
300
344
}
301
345
346
+ private static String getPagerFromEnv () {
347
+ String pager = System .getenv ("RUBY_PAGER" );
348
+ if (pager != null ) {
349
+ return pager .strip ();
350
+ }
351
+
352
+ pager = System .getenv ("PAGER" );
353
+ if (pager != null ) {
354
+ return pager .strip ();
355
+ }
356
+
357
+ return null ;
358
+ }
359
+
302
360
private void printPreRunInformation (CommandLineOptions config ) {
303
361
if (config .showVersion ) {
304
362
System .out .println (TruffleRuby .getVersionString (getImplementationNameFromEngine ()));
0 commit comments