Skip to content

Commit b2b8a85

Browse files
committed
Simplify option parsing and use MRI-like names
PullRequest: truffleruby/668
2 parents 1c1fa9f + aaafe3f commit b2b8a85

File tree

11 files changed

+74
-98
lines changed

11 files changed

+74
-98
lines changed

spec/truffle/launcher_spec.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ def should_print_full_java_command(options, env: {})
8989
end
9090

9191
it "takes --option options from TRUFFLERUBYOPT" do
92-
out = ruby_exe("puts $VERBOSE", env: { "TRUFFLERUBYOPT" => "--verbosity=true" })
92+
out = ruby_exe("puts $VERBOSE", env: { "TRUFFLERUBYOPT" => "--verbose=true" })
9393
$?.success?.should == true
9494
out.should == "true\n"
9595
end
9696

9797
it "takes --ruby.option options from TRUFFLERUBYOPT" do
98-
out = ruby_exe("puts $VERBOSE", env: { "TRUFFLERUBYOPT" => "--ruby.verbosity=true" })
98+
out = ruby_exe("puts $VERBOSE", env: { "TRUFFLERUBYOPT" => "--ruby.verbose=true" })
9999
$?.success?.should == true
100100
out.should == "true\n"
101101
end
@@ -107,36 +107,36 @@ def should_print_full_java_command(options, env: {})
107107
end
108108

109109
it "takes --option options from RUBYOPT" do
110-
out = ruby_exe("puts $VERBOSE", env: { "RUBYOPT" => "--verbosity=true" })
110+
out = ruby_exe("puts $VERBOSE", env: { "RUBYOPT" => "--verbose=true" })
111111
$?.success?.should == true
112112
out.should == "true\n"
113113
end
114114

115115
it "takes --ruby.option options from RUBYOPT" do
116-
out = ruby_exe("puts $VERBOSE", env: { "RUBYOPT" => "--ruby.verbosity=true" })
116+
out = ruby_exe("puts $VERBOSE", env: { "RUBYOPT" => "--ruby.verbose=true" })
117117
$?.success?.should == true
118118
out.should == "true\n"
119119
end
120120

121121
guard -> { !TruffleRuby.native? } do
122122
it "takes options from system properties set on the command line using --jvm" do
123-
out = ruby_exe("puts $VERBOSE", options: "--jvm.Dpolyglot.ruby.verbosity=true")
123+
out = ruby_exe("puts $VERBOSE", options: "--jvm.Dpolyglot.ruby.verbose=true")
124124
$?.success?.should == true
125125
out.should == "true\n"
126126
end
127127
end
128128

129129
guard -> { TruffleRuby.native? } do
130130
it "takes options from system properties set on the command line using --native" do
131-
out = ruby_exe("puts $VERBOSE", options: "--native.Dpolyglot.ruby.verbosity=true")
131+
out = ruby_exe("puts $VERBOSE", options: "--native.Dpolyglot.ruby.verbose=true")
132132
$?.success?.should == true
133133
out.should == "true\n"
134134
end
135135
end
136136

137137
it "prioritises options on the command line over system properties" do
138138
prefix = TruffleRuby.native? ? 'native' : 'jvm'
139-
out = ruby_exe("puts $VERBOSE", options: "--#{prefix}.Dpolyglot.ruby.verbosity=nil -W2")
139+
out = ruby_exe("puts $VERBOSE", options: "--#{prefix}.Dpolyglot.ruby.verbose=nil -W2")
140140
$?.success?.should == true
141141
out.should == "true\n"
142142
end
@@ -158,7 +158,7 @@ def should_print_full_java_command(options, env: {})
158158
it "prints available user options for --help:languages" do
159159
out = ruby_exe(nil, options: "--help:languages")
160160
$?.success?.should == true
161-
out.should include("--ruby.verbosity")
161+
out.should include("--ruby.verbose")
162162
end
163163

164164
it "prints available expert options for --help:languages --help:expert" do
@@ -188,7 +188,7 @@ def should_print_full_java_command(options, env: {})
188188
$?.success?.should == false
189189
out.should include("invalid option --ruby.unknown=value")
190190
end
191-
191+
192192
it "sets the log level using --log.level=" do
193193
out = ruby_exe("14", options: "--options.log --log.level=CONFIG", args: "2>&1")
194194
$?.success?.should == true
@@ -210,15 +210,15 @@ def should_print_full_java_command(options, env: {})
210210
end
211211

212212
it 'parses ,' do
213-
out = ruby_exe("puts $LOAD_PATH", options: "--load_paths=a,b")
213+
out = ruby_exe("puts $LOAD_PATH", options: "--load-paths=a,b")
214214
$?.success?.should == true
215215
out.lines[0].should == "#{Dir.pwd}/a\n"
216216
out.lines[1].should == "#{Dir.pwd}/b\n"
217217
end
218218

219219
it 'parses , respecting escaping' do
220220
# \\\\ translates to one \
221-
out = ruby_exe("puts $LOAD_PATH", options: "--load_paths=a\\\\,b,,\\\\c")
221+
out = ruby_exe("puts $LOAD_PATH", options: "--load-paths=a\\\\,b,,\\\\c")
222222
$?.success?.should == true
223223
out.lines[0].should == "#{Dir.pwd}/a,b\n"
224224
out.lines[1].should == "#{Dir.pwd}\n"
@@ -254,7 +254,7 @@ def should_print_full_java_command(options, env: {})
254254
$?.success?.should == true
255255
out.should =~ /language options/i
256256
out.should include("Ruby:")
257-
out.should include("--ruby.load_paths=")
257+
out.should include("--ruby.load-paths=")
258258
end
259259

260260
guard -> { TruffleRuby.sulong? } do
@@ -318,7 +318,7 @@ def should_print_full_java_command(options, env: {})
318318
out.should include("14")
319319
end
320320
end
321-
321+
322322
it "warns on ignored options" do
323323
[
324324
"-y",

spec/truffle/options/parsing_spec.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
end
2525

2626
it "enum values" do
27-
ruby_exe("p Truffle::Boot.get_option('verbosity')").should_not == ":NIL\n"
28-
ruby_exe("p Truffle::Boot.get_option('verbosity')", options: "--verbosity=NIL").should == ":NIL\n"
27+
ruby_exe("p Truffle::Boot.get_option('verbose')").should_not == ":NIL\n"
28+
ruby_exe("p Truffle::Boot.get_option('verbose')", options: "--verbose=NIL").should == ":NIL\n"
29+
ruby_exe("p Truffle::Boot.get_option('verbose')", options: "--verbose").should == ":TRUE\n"
2930
end
3031

3132
it "strings" do
@@ -34,9 +35,9 @@
3435
end
3536

3637
it "arrays of strings" do
37-
ruby_exe("p Truffle::Boot.get_option('load_paths')", options: "--load_paths=ruby_spec_test_value").should == "[\"ruby_spec_test_value\"]\n"
38-
ruby_exe("p Truffle::Boot.get_option('load_paths')", options: "--load_paths=a,b,c").should == "[\"a\", \"b\", \"c\"]\n"
39-
ruby_exe("p Truffle::Boot.get_option('load_paths')", options: "--load_paths=a\\\\,b,c").should == "[\"a,b\", \"c\"]\n"
38+
ruby_exe("p Truffle::Boot.get_option('load-paths')", options: "--load-paths=ruby_spec_test_value").should == "[\"ruby_spec_test_value\"]\n"
39+
ruby_exe("p Truffle::Boot.get_option('load-paths')", options: "--load-paths=a,b,c").should == "[\"a\", \"b\", \"c\"]\n"
40+
ruby_exe("p Truffle::Boot.get_option('load-paths')", options: "--load-paths=a\\\\,b,c").should == "[\"a,b\", \"c\"]\n"
4041
end
4142

4243
end
@@ -52,7 +53,7 @@
5253
end
5354

5455
it "enum values" do
55-
ruby_exe("14", options: "--verbosity=foo", args: "2>&1").should include("Invalid argument --verbosity=foo specified. No enum constant org.truffleruby.shared.options.Verbosity.FOO'")
56+
ruby_exe("14", options: "--verbose=foo", args: "2>&1").should include("Invalid argument --verbose=foo specified. No enum constant org.truffleruby.shared.options.Verbosity.FOO'")
5657
end
5758

5859
end

src/launcher/java/org/truffleruby/launcher/CommandLineParser.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -398,26 +398,12 @@ private void processArgument() throws CommandLineException {
398398
config.setOption(OptionsCatalog.INTERNAL_ENCODING, encodingNames.substring(index + 1));
399399
}
400400
break FOR;
401-
} else if (argument.startsWith("--external-encoding")) {
402-
if (argument.equals("--external-encoding")) {
403-
characterIndex = argument.length();
404-
String feature = grabValue(getArgumentError("missing argument for " + argument), false);
405-
argument = argument + "=" + feature;
406-
}
407-
final String encodingName = argument.substring(argument.indexOf('=') + 1);
408-
config.setOption(OptionsCatalog.EXTERNAL_ENCODING, encodingName);
409-
break FOR;
410-
} else if (argument.startsWith("--internal-encoding")) {
411-
if (argument.equals("--internal-encoding")) {
412-
characterIndex = argument.length();
413-
String feature = grabValue(getArgumentError("missing argument for " + argument), false);
414-
argument = argument + "=" + feature;
415-
}
416-
final String encodingName = argument.substring(argument.indexOf('=') + 1);
417-
config.setOption(OptionsCatalog.INTERNAL_ENCODING, encodingName);
401+
} else if (argument.equals("--external-encoding") || argument.equals("--internal-encoding")) {
402+
// Just translate to option=value form and let the Launcher handle it
403+
characterIndex = argument.length();
404+
String feature = grabValue(getArgumentError("missing argument for " + argument), false);
405+
config.getUnknownArguments().add(argument + "=" + feature);
418406
break FOR;
419-
} else if (argument.equals("--debug")) {
420-
throw notImplemented("--debug");
421407
} else if (argument.equals("--yydebug")) {
422408
disallowedInRubyOpts(argument);
423409
warnInternalDebugTool(argument);
@@ -431,8 +417,6 @@ private void processArgument() throws CommandLineException {
431417
// cancel other execution actions
432418
config.setOption(OptionsCatalog.EXECUTION_ACTION, ExecutionAction.NONE);
433419
break FOR;
434-
} else if (argument.startsWith("--profile")) {
435-
throw notImplemented("--profile");
436420
} else if (argument.equals("--debug-frozen-string-literal")) {
437421
warnInternalDebugTool(argument);
438422
break FOR;
@@ -458,9 +442,6 @@ private void processArgument() throws CommandLineException {
458442
enableDisableFeature(enable, true);
459443
}
460444
break FOR;
461-
} else if (argument.equals("--verbose")) {
462-
config.setOption(OptionsCatalog.VERBOSITY, Verbosity.TRUE);
463-
break FOR;
464445
} else if (argument.startsWith("--dump=")) {
465446
warnInternalDebugTool(argument);
466447
break FOR;

src/launcher/java/org/truffleruby/launcher/RubyLauncher.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,21 @@ protected void launch(Context.Builder contextBuilder) {
132132
System.exit(exitValue);
133133
}
134134

135+
/**
136+
* This is only used to provide suggestions when an option is misspelled.
137+
* It should only list options which are parsed directly by the CommandLineParser.
138+
* Normal SDK options are already handled by the common Launcher code.
139+
*/
135140
@Override
136141
protected void collectArguments(Set<String> options) {
137142
options.addAll(Arrays.asList(
138143
"-0",
139144
"-a",
140145
"-c",
141146
"-C",
142-
"-d", "--debug",
147+
"-d",
143148
"-e",
144-
"-E", "--encoding",
149+
"-E",
145150
"-F",
146151
"-i",
147152
"-I",
@@ -152,25 +157,15 @@ protected void collectArguments(Set<String> options) {
152157
"-s",
153158
"-S",
154159
"-T",
155-
"-v", "--verbose",
160+
"-v",
156161
"-w",
157162
"-W",
158163
"-x",
159164
"--copyright",
160-
"--enable", "--disable",
161-
"--external-encoding", "--internal-encoding",
162-
"--yydebug",
163-
"--debug-frozen-string-literal",
164-
"--version",
165-
"--help",
166-
"--jit",
167-
"--jit-warnings",
168-
"--jit-debug",
169-
"--jit-wait",
170-
"--jit-save-temps",
171-
"--jit-verbose",
172-
"--jit-max-cache",
173-
"--jit-min-calls"));
165+
"--disable",
166+
"--enable",
167+
"--encoding",
168+
"--version"));
174169
}
175170

176171
@Override

src/main/java/org/truffleruby/options/Options.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,21 +291,21 @@ public class Options {
291291

292292
public Object fromDescription(OptionDescription<?> description) {
293293
switch (description.getName()) {
294-
case "ruby.load_paths":
294+
case "ruby.load-paths":
295295
return LOAD_PATHS;
296-
case "ruby.required_libraries":
296+
case "ruby.required-libraries":
297297
return REQUIRED_LIBRARIES;
298-
case "ruby.working_directory":
298+
case "ruby.working-directory":
299299
return WORKING_DIRECTORY;
300300
case "ruby.debug":
301301
return DEBUG;
302-
case "ruby.verbosity":
302+
case "ruby.verbose":
303303
return VERBOSITY;
304-
case "ruby.source_encoding":
304+
case "ruby.source-encoding":
305305
return SOURCE_ENCODING;
306-
case "ruby.internal_encoding":
306+
case "ruby.internal-encoding":
307307
return INTERNAL_ENCODING;
308-
case "ruby.external_encoding":
308+
case "ruby.external-encoding":
309309
return EXTERNAL_ENCODING;
310310
case "ruby.home":
311311
return HOME;

src/main/ruby/core/truffle/kernel_operations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def self.define_read_only_global(name, getter)
8282

8383
Truffle::Boot.redo do
8484
$DEBUG = Truffle::Boot.get_option 'debug'
85-
$VERBOSE = case Truffle::Boot.get_option 'verbosity'
85+
$VERBOSE = case Truffle::Boot.get_option 'verbose'
8686
when :TRUE
8787
true
8888
when :FALSE

src/main/ruby/post-boot/post-boot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# GNU Lesser General Public License version 2.1.
1010

1111
Truffle::Boot.delay do
12-
wd = Truffle::Boot.get_option('working_directory')
12+
wd = Truffle::Boot.get_option('working-directory')
1313
Dir.chdir(wd) unless wd.empty?
1414
end
1515

src/shared/java/org/truffleruby/shared/options/OptionsCatalog.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,49 @@ public class OptionsCatalog {
2020

2121
public static final StringArrayOptionDescription LOAD_PATHS = new StringArrayOptionDescription(
2222
OptionCategory.USER,
23-
"ruby.load_paths",
23+
"ruby.load-paths",
2424
"Load paths (configured by the -I Ruby option)",
2525
new String[]{"-I"},
2626
new String[]{});
2727
public static final StringArrayOptionDescription REQUIRED_LIBRARIES = new StringArrayOptionDescription(
2828
OptionCategory.USER,
29-
"ruby.required_libraries",
29+
"ruby.required-libraries",
3030
"Required libraries (configured by the -r Ruby option)",
3131
new String[]{"-r"},
3232
new String[]{});
3333
public static final StringOptionDescription WORKING_DIRECTORY = new StringOptionDescription(
3434
OptionCategory.USER,
35-
"ruby.working_directory",
35+
"ruby.working-directory",
3636
"Interpreter will switch to this directory (configured by the -C Ruby option)",
3737
new String[]{"-C"},
3838
"");
3939
public static final BooleanOptionDescription DEBUG = new BooleanOptionDescription(
4040
OptionCategory.USER,
4141
"ruby.debug",
42-
"Debug (configured by the -d Ruby option)",
42+
"Sets $DEBUG to this value (configured by the -d Ruby option)",
4343
new String[]{"-d"},
4444
false);
4545
public static final VerbosityOptionDescription VERBOSITY = new VerbosityOptionDescription(
4646
OptionCategory.USER,
47-
"ruby.verbosity",
48-
"Verbosity (configured by the -v, -w, -W, --verbose Ruby options)",
49-
new String[]{"-v", "-w", "-W", "--verbose"},
47+
"ruby.verbose",
48+
"Sets $VERBOSE to this value (configured by the -v, -w, -W Ruby options)",
49+
new String[]{"-v", "-w", "-W"},
5050
Verbosity.FALSE);
5151
public static final StringOptionDescription SOURCE_ENCODING = new StringOptionDescription(
5252
OptionCategory.USER,
53-
"ruby.source_encoding",
53+
"ruby.source-encoding",
5454
"Source encoding (configured by the -K Ruby option)",
5555
new String[]{"-K"},
5656
"");
5757
public static final StringOptionDescription INTERNAL_ENCODING = new StringOptionDescription(
5858
OptionCategory.USER,
59-
"ruby.internal_encoding",
59+
"ruby.internal-encoding",
6060
"Internal encoding (configured by the -E, -U Ruby options)",
6161
new String[]{"-E", "-U"},
6262
"");
6363
public static final StringOptionDescription EXTERNAL_ENCODING = new StringOptionDescription(
6464
OptionCategory.USER,
65-
"ruby.external_encoding",
65+
"ruby.external-encoding",
6666
"External encoding (configured by the -E Ruby option)",
6767
new String[]{"-E"},
6868
"");
@@ -801,21 +801,21 @@ public class OptionsCatalog {
801801

802802
public static OptionDescription<?> fromName(String name) {
803803
switch (name) {
804-
case "ruby.load_paths":
804+
case "ruby.load-paths":
805805
return LOAD_PATHS;
806-
case "ruby.required_libraries":
806+
case "ruby.required-libraries":
807807
return REQUIRED_LIBRARIES;
808-
case "ruby.working_directory":
808+
case "ruby.working-directory":
809809
return WORKING_DIRECTORY;
810810
case "ruby.debug":
811811
return DEBUG;
812-
case "ruby.verbosity":
812+
case "ruby.verbose":
813813
return VERBOSITY;
814-
case "ruby.source_encoding":
814+
case "ruby.source-encoding":
815815
return SOURCE_ENCODING;
816-
case "ruby.internal_encoding":
816+
case "ruby.internal-encoding":
817817
return INTERNAL_ENCODING;
818-
case "ruby.external_encoding":
818+
case "ruby.external-encoding":
819819
return EXTERNAL_ENCODING;
820820
case "ruby.home":
821821
return HOME;

tool/generate-options.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ def parse_reference_defaults(default)
4747
boxed_type = 'Integer'
4848
default = default.to_s
4949
when /^enum\/(\w*)/
50-
name = $1
51-
type = camelize name
50+
type = camelize $1
5251
boxed_type = type
5352
default = "#{type}.#{default.to_s.upcase}"
5453
when 'string'

0 commit comments

Comments
 (0)