Skip to content

Commit f4b528e

Browse files
committed
Support -- (end of options) to mruby command
#### Before this patch: ``` $ bin/mruby -e 'p ARGV' -- -x bin/mruby: invalid option -- (-h will show valid options) ``` #### After this patch: ``` $ bin/mruby -e 'p ARGV' -- -x ["-x"] ```
1 parent 190649e commit f4b528e

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

mrbgems/mruby-bin-mruby/bintest/mruby.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ def hoge
128128
assert_mruby("", /\A.*: Cannot open library file: .*\n\z/, false, %w[-r _no_exists_])
129129
end
130130

131+
assert('mruby --') do
132+
assert_mruby(%{["-x", "1"]\n}, "", true, %w[-e p(ARGV) -- -x 1])
133+
end
134+
131135
assert('mruby invalid short option') do
132136
assert_mruby("", /\A.*: invalid option -1 .*\n\z/, false, %w[-1])
133137
end

mrbgems/mruby-bin-mruby/tools/mruby/mruby.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,33 @@ options_init(struct options *opts, int argc, char **argv)
6464
static const char *
6565
options_opt(struct options *opts)
6666
{
67+
/* concatenated short options (e.g. `-cv`) */
6768
if (*opts->short_opt && *++opts->opt) {
6869
short_opt:
6970
opts->short_opt[0] = *opts->opt;
7071
opts->short_opt[1] = 0;
7172
return opts->short_opt;
7273
}
74+
7375
while (++opts->argv, --opts->argc) {
7476
opts->opt = *opts->argv;
77+
78+
/* empty || not start with `-` || `-` */
7579
if (!opts->opt[0] || opts->opt[0] != '-' || !opts->opt[1]) return NULL;
80+
7681
if (opts->opt[1] == '-') {
82+
/* `--` */
83+
if (!opts->opt[2]) {
84+
++opts->argv, --opts->argc;
85+
return NULL;
86+
}
87+
/* long option */
7788
opts->opt += 2;
7889
*opts->short_opt = 0;
7990
return opts->opt;
8091
}
8192
else {
93+
/* short option */
8294
++opts->opt;
8395
goto short_opt;
8496
}
@@ -90,6 +102,7 @@ static const char *
90102
options_arg(struct options *opts)
91103
{
92104
if (*opts->short_opt && opts->opt[1]) {
105+
/* concatenated short option and option argument (e.g. `-rLIBRARY`) */
93106
*opts->short_opt = 0;
94107
return opts->opt + 1;
95108
}

0 commit comments

Comments
 (0)