[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / doc / ruby / options.md
blob143c8925f12fdef6774f67fb0c1979ede1c1c9a8
1 # Ruby Command-Line Options
3 ## About the Examples
5 Some examples here use command-line option `-e`,
6 which passes the Ruby code to be executed on the command line itself:
8 ```sh
9 $ ruby -e 'puts "Hello, World."'
10 ```
12 Some examples here assume that file `desiderata.txt` exists:
14 ```
15 $ cat desiderata.txt
16 Go placidly amid the noise and the haste,
17 and remember what peace there may be in silence.
18 As far as possible, without surrender,
19 be on good terms with all persons.
20 ```
22 ## Options
24 ### `-0`: \Set `$/` (Input Record Separator)
26 Option `-0` defines the input record separator `$/`
27 for the invoked Ruby program.
29 The optional argument to the option must be octal digits,
30 each in the range `0..7`;
31 these digits are prefixed with digit `0` to form an octal value.
33 If no argument is given, the input record separator is `0x00`.
35 If an argument is given, it must immediately follow the option
36 (no intervening whitespace or equal-sign character `'='`);
37 argument values:
39 - `0`: the input record separator is `''`;
40   see {Special Line Separator Values}[rdoc-ref:IO@Special+Line+Separator+Values].
41 - In range `(1..0377)`:
42   the input record separator `$/` is set to the character value of the argument.
43 - Any other octal value: the input record separator is `nil`.
45 Examples:
47 ```sh
48 $ ruby -0 -e 'p $/'
49 "\x00"
50 ruby -00 -e 'p $/'
52 $ ruby -012 -e 'p $/'
53 "\n"
54 $ ruby -015 -e 'p $/'
55 "\r"
56 $ ruby -0377 -e 'p $/'
57 "\xFF"
58 $ ruby -0400 -e 'p $/'
59 nil
60 ```
62 See also:
64 - {Option -a}[rdoc-ref:ruby/options.md@a-3A+Split+Input+Lines+into+Fields]:
65   Split input lines into fields.
66 - {Option -F}[rdoc-ref:ruby/options.md@F-3A+Set+Input+Field+Separator]:
67   \Set input field separator.
68 - {Option -l}[rdoc-ref:ruby/options.md@l-3A+Set+Output+Record+Separator-3B+Chop+Lines]:
69   \Set output record separator; chop lines.
70 - {Option -n}[rdoc-ref:ruby/options.md@n-3A+Run+Program+in+gets+Loop]:
71   Run program in `gets` loop.
72 - {Option -p}[rdoc-ref:ruby/options.md@p-3A+-n-2C+with+Printing]:
73   `-n`, with printing.
75 ### `-a`: Split Input Lines into Fields
77 Option `-a`, when given with either of options `-n` or `-p`,
78 splits the string at `$_` into an array of strings at `$F`:
80 ```sh
81 $ ruby -an -e 'p $F' desiderata.txt
82 ["Go", "placidly", "amid", "the", "noise", "and", "the", "haste,"]
83 ["and", "remember", "what", "peace", "there", "may", "be", "in", "silence."]
84 ["As", "far", "as", "possible,", "without", "surrender,"]
85 ["be", "on", "good", "terms", "with", "all", "persons."]
86 ```
88 For the splitting,
89 the default record separator is `$/`,
90 and the default field separator  is `$;`.
92 See also:
94 - {Option -0}[rdoc-ref:ruby/options.md@0-3A+Set+-24-2F+-28Input+Record+Separator-29]:
95   \Set `$/` (input record separator).
96 - {Option -F}[rdoc-ref:ruby/options.md@F-3A+Set+Input+Field+Separator]:
97   \Set input field separator.
98 - {Option -l}[rdoc-ref:ruby/options.md@l-3A+Set+Output+Record+Separator-3B+Chop+Lines]:
99   \Set output record separator; chop lines.
100 - {Option -n}[rdoc-ref:ruby/options.md@n-3A+Run+Program+in+gets+Loop]:
101   Run program in `gets` loop.
102 - {Option -p}[rdoc-ref:ruby/options.md@p-3A+-n-2C+with+Printing]:
103   `-n`, with printing.
105 ### `-c`: Check Syntax
107 Option `-c` specifies that the specified Ruby program
108 should be checked for syntax, but not actually executed:
111 $ ruby -e 'puts "Foo"'
113 $ ruby -c -e 'puts "Foo"'
114 Syntax OK
117 ### `-C`: \Set Working Directory
119 The argument to option `-C` specifies a working directory
120 for the invoked Ruby program;
121 does not change the working directory for the current process:
123 ```sh
124 $ basename `pwd`
125 ruby
126 $ ruby -C lib -e 'puts File.basename(Dir.pwd)'
128 $ basename `pwd`
129 ruby
132 Whitespace between the option and its argument may be omitted.
134 ### `-d`: \Set `$DEBUG` to `true`
136 Some code in (or called by) the Ruby program may include statements or blocks
137 conditioned by the global variable `$DEBUG` (e.g., `if $DEBUG`);
138 these commonly write to `$stdout` or `$stderr`.
140 The default value for `$DEBUG` is `false`;
141 option `-d` sets it to `true`:
143 ```sh
144 $ ruby -e 'p $DEBUG'
145 false
146 $ ruby -d -e 'p $DEBUG'
147 true
150 Option `--debug` is an alias for option `-d`.
152 ### `-e`: Execute Given Ruby Code
154 Option `-e` requires an argument, which is Ruby code to be executed;
155 the option may be given more than once:
158 $ ruby -e 'puts "Foo"' -e 'puts "Bar"'
163 Whitespace between the option and its argument may be omitted.
165 The command may include other options,
166 but should not include arguments (which, if given, are ignored).
168 ### `-E`: \Set Default Encodings
170 Option `-E` requires an argument, which specifies either the default external encoding,
171 or both the default external and internal encodings for the invoked Ruby program:
174 # No option -E.
175 $ ruby -e 'p [Encoding::default_external, Encoding::default_internal]'
176 [#<Encoding:UTF-8>, nil]
177 # Option -E with default external encoding.
178 $ ruby -E cesu-8 -e 'p [Encoding::default_external, Encoding::default_internal]'
179 [#<Encoding:CESU-8>, nil]
180 # Option -E with default external and internal encodings.
181 $ ruby -E utf-8:cesu-8 -e 'p [Encoding::default_external, Encoding::default_internal]'
182 [#<Encoding:UTF-8>, #<Encoding:CESU-8>]
185 Whitespace between the option and its argument may be omitted.
187 See also:
189 - {Option --external-encoding}[options_md.html#label--external-encoding-3A+Set+Default+External+Encoding]:
190   \Set default external encoding.
191 - {Option --internal-encoding}[options_md.html#label--internal-encoding-3A+Set+Default+Internal+Encoding]:
192   \Set default internal encoding.
194 Option `--encoding` is an alias for option `-E`.
196 ### `-F`: \Set Input Field Separator
198 Option `-F`, when given with option `-a`,
199 specifies that its argument is to be the input field separator to be used for splitting:
201 ```sh
202 $ ruby -an -Fs -e 'p $F' desiderata.txt
203 ["Go placidly amid the noi", "e and the ha", "te,\n"]
204 ["and remember what peace there may be in ", "ilence.\n"]
205 ["A", " far a", " po", "", "ible, without ", "urrender,\n"]
206 ["be on good term", " with all per", "on", ".\n"]
209 The argument may be a regular expression:
212 $ ruby -an -F'[.,]\s*' -e 'p $F' desiderata.txt
213 ["Go placidly amid the noise and the haste"]
214 ["and remember what peace there may be in silence"]
215 ["As far as possible", "without surrender"]
216 ["be on good terms with all persons"]
219 The argument must immediately follow the option
220 (no intervening whitespace or equal-sign character `'='`).
222 See also:
224 - {Option -0}[rdoc-ref:ruby/options.md@0-3A+Set+-24-2F+-28Input+Record+Separator-29]:
225   \Set `$/` (input record separator).
226 - {Option -a}[rdoc-ref:ruby/options.md@a-3A+Split+Input+Lines+into+Fields]:
227   Split input lines into fields.
228 - {Option -l}[rdoc-ref:ruby/options.md@l-3A+Set+Output+Record+Separator-3B+Chop+Lines]:
229   \Set output record separator; chop lines.
230 - {Option -n}[rdoc-ref:ruby/options.md@n-3A+Run+Program+in+gets+Loop]:
231   Run program in `gets` loop.
232 - {Option -p}[rdoc-ref:ruby/options.md@p-3A+-n-2C+with+Printing]:
233   `-n`, with printing.
235 ### `-h`: Print Short Help Message
237 Option `-h` prints a short help message
238 that includes single-hyphen options (e.g. `-I`),
239 and largely omits double-hyphen options (e.g., `--version`).
241 Arguments and additional options are ignored.
243 For a longer help message, use option `--help`.
245 ### `-i`: \Set \ARGF In-Place Mode
247 Option `-i` sets the \ARGF in-place mode for the invoked Ruby program;
248 see ARGF#inplace_mode=:
251 $ ruby -e 'p ARGF.inplace_mode'
253 $ ruby -i -e 'p ARGF.inplace_mode'
255 $ ruby -i.bak -e 'p ARGF.inplace_mode'
256 ".bak"
259 ### `-I`: Add to `$LOAD_PATH`
261 The argument to option `-I` specifies a directory
262 to be added to the array in global variable `$LOAD_PATH`;
263 the option may be given more than once:
265 ```sh
266 $ pushd /tmp
267 $ ruby -e 'p $LOAD_PATH.size'
269 $ ruby -I my_lib -I some_lib -e 'p $LOAD_PATH.size'
271 $ ruby -I my_lib -I some_lib -e 'p $LOAD_PATH.take(2)'
272 ["/tmp/my_lib", "/tmp/some_lib"]
273 $ popd
276 Whitespace between the option and its argument may be omitted.
278 ### `-l`: \Set Output Record Separator; Chop Lines
280 Option `-l`, when given with option `-n` or `-p`,
281 modifies line-ending processing by:
283 - Setting global variable output record separator `$\`
284   to the current value of input record separator `$/`;
285   this affects line-oriented output (such a the output from Kernel#puts).
286 - Calling String#chop! on each line read.
288 Without option `-l` (unchopped):
290 ```sh
291 $ ruby -n -e 'p $_' desiderata.txt
292 "Go placidly amid the noise and the haste,\n"
293 "and remember what peace there may be in silence.\n"
294 "As far as possible, without surrender,\n"
295 "be on good terms with all persons.\n"
298 With option `-l' (chopped):
300 ```sh
301 $ ruby -ln -e 'p $_' desiderata.txt
302 "Go placidly amid the noise and the haste,"
303 "and remember what peace there may be in silence."
304 "As far as possible, without surrender,"
305 "be on good terms with all persons."
308 See also:
310 - {Option -0}[rdoc-ref:ruby/options.md@0-3A+Set+-24-2F+-28Input+Record+Separator-29]:
311   \Set `$/` (input record separator).
312 - {Option -a}[rdoc-ref:ruby/options.md@a-3A+Split+Input+Lines+into+Fields]:
313   Split input lines into fields.
314 - {Option -F}[rdoc-ref:ruby/options.md@F-3A+Set+Input+Field+Separator]:
315   \Set input field separator.
316 - {Option -n}[rdoc-ref:ruby/options.md@n-3A+Run+Program+in+gets+Loop]:
317   Run program in `gets` loop.
318 - {Option -p}[rdoc-ref:ruby/options.md@p-3A+-n-2C+with+Printing]:
319   `-n`, with printing.
321 ### `-n`: Run Program in `gets` Loop
323 Option `-n` runs your program in a Kernel#gets loop:
326 while gets
327   # Your Ruby code.
331 Note that `gets` reads the next line and sets global variable `$_`
332 to the last read line:
334 ```sh
335 $ ruby -n -e 'puts $_' desiderata.txt
336 Go placidly amid the noise and the haste,
337 and remember what peace there may be in silence.
338 As far as possible, without surrender,
339 be on good terms with all persons.
342 See also:
344 - {Option -0}[rdoc-ref:ruby/options.md@0-3A+Set+-24-2F+-28Input+Record+Separator-29]:
345   \Set `$/` (input record separator).
346 - {Option -a}[rdoc-ref:ruby/options.md@a-3A+Split+Input+Lines+into+Fields]:
347   Split input lines into fields.
348 - {Option -F}[rdoc-ref:ruby/options.md@F-3A+Set+Input+Field+Separator]:
349   \Set input field separator.
350 - {Option -l}[rdoc-ref:ruby/options.md@l-3A+Set+Output+Record+Separator-3B+Chop+Lines]:
351   \Set output record separator; chop lines.
352 - {Option -p}[rdoc-ref:ruby/options.md@p-3A+-n-2C+with+Printing]:
353   `-n`, with printing.
355 ### `-p`: `-n`, with Printing
357 Option `-p` is like option `-n`, but also prints each line:
359 ```sh
360 $ ruby -p -e 'puts $_.size' desiderata.txt
362 Go placidly amid the noise and the haste,
364 and remember what peace there may be in silence.
366 As far as possible, without surrender,
368 be on good terms with all persons.
371 See also:
373 - {Option -0}[rdoc-ref:ruby/options.md@0-3A+Set+-24-2F+-28Input+Record+Separator-29]:
374   \Set `$/` (input record separator).
375 - {Option -a}[rdoc-ref:ruby/options.md@a-3A+Split+Input+Lines+into+Fields]:
376   Split input lines into fields.
377 - {Option -F}[rdoc-ref:ruby/options.md@F-3A+Set+Input+Field+Separator]:
378   \Set input field separator.
379 - {Option -l}[rdoc-ref:ruby/options.md@l-3A+Set+Output+Record+Separator-3B+Chop+Lines]:
380   \Set output record separator; chop lines.
381 - {Option -n}[rdoc-ref:ruby/options.md@n-3A+Run+Program+in+gets+Loop]:
382   Run program in `gets` loop.
384 ### `-r`: Require Library
386 The argument to option `-r` specifies a library to be required
387 before executing the Ruby program;
388 the option may be given more than once:
390 ```sh
391 $ ruby -e 'p defined?(JSON); p defined?(CSV)'
394 $ ruby -r CSV -r JSON -e 'p defined?(JSON); p defined?(CSV)'
395 "constant"
396 "constant"
399 Whitespace between the option and its argument may be omitted.
401 ### `-s`: Define Global Variable
403 Option `-s` specifies that a "custom option" is to define a global variable
404 in the invoked Ruby program:
406 - The custom option must appear _after_ the program name.
407 - The custom option must begin with single hyphen (e.g., `-foo`),
408   not two hyphens (e.g., `--foo`).
409 - The name of the global variable is based on the option name:
410   global variable `$foo` for custom option`-foo`.
411 - The value of the global variable is the string option argument if given,
412   `true` otherwise.
414 More than one custom option may be given:
417 $ cat t.rb
418 p [$foo, $bar]
419 $ ruby t.rb
420 [nil, nil]
421 $ ruby -s t.rb -foo=baz
422 ["baz", nil]
423 $ ruby -s t.rb -foo
424 [true, nil]
425 $ ruby -s t.rb -foo=baz -bar=bat
426 ["baz", "bat"]
429 The option may not be used with
430 {option -e}[rdoc-ref:ruby/options.md@e-3A+Execute+Given+Ruby+Code]
432 ### `-S`: Search Directories in `ENV['PATH']`
434 Option `-S` specifies that the Ruby interpreter
435 is to search (if necessary) the directories whose paths are in the program's
436 `PATH` environment variable;
437 the program is executed in the shell's current working directory
438 (not necessarily in the directory where the program is found).
440 This example uses adds path `'tmp/'` to the `PATH` environment variable:
442 ```sh
443 $ export PATH=/tmp:$PATH
444 $ echo "puts File.basename(Dir.pwd)" > /tmp/t.rb
445 $ ruby -S t.rb
446 ruby
449 ### `-v`: Print Version; \Set `$VERBOSE`
451 Options `-v` prints the Ruby version and sets global variable `$VERBOSE`:
454 $ ruby -e 'p $VERBOSE'
455 false
456 $ ruby -v -e 'p $VERBOSE'
457 ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x64-mingw-ucrt]
458 true
461 ### `-w`: Synonym for `-W1`
463 Option `-w` (lowercase letter) is equivalent to option `-W1` (uppercase letter).
465 ### `-W`: \Set \Warning Policy
467 Any Ruby code can create a <i>warning message</i> by calling method Kernel#warn;
468 methods in the Ruby core and standard libraries can also create warning messages.
469 Such a message may be printed on `$stderr`
470 (or not, depending on certain settings).
472 Option `-W` helps determine whether a particular warning message
473 will be written,
474 by setting the initial value of global variable `$-W`:
476 - `-W0`: Sets `$-W` to `0` (silent; no warnings).
477 - `-W1`: Sets `$-W` to `1` (moderate verbosity).
478 - `-W2`: Sets `$-W` to `2` (high verbosity).
479 - `-W`: Same as `-W2` (high verbosity).
480 - Option not given: Same as `-W1` (moderate verbosity).
482 The value of `$-W`, in turn, determines which warning messages (if any)
483 are to be printed to `$stdout` (see Kernel#warn):
485 ```sh
486 $ ruby -W1 -e 'p $foo'
488 $ ruby -W2 -e 'p $foo'
489 -e:1: warning: global variable '$foo' not initialized
493 Ruby code may also define warnings for certain categories;
494 these are the default settings for the defined categories:
497 Warning[:experimental] # => true
498 Warning[:deprecated]   # => false
499 Warning[:performance]  # => false
502 They may also be set:
504 Warning[:experimental] = false
505 Warning[:deprecated]   = true
506 Warning[:performance]  = true
509 You can suppress a category by prefixing `no-` to the category name:
512 $ ruby -W:no-experimental -e 'p IO::Buffer.new'
513 #<IO::Buffer>
516 ### `-x`: Execute Ruby Code Found in Text
518 Option `-x` executes a Ruby program whose code is embedded
519 in other, non-code, text:
521 The ruby code:
523 - Begins after the first line beginning with `'#!` and containing string `'ruby'`.
524 - Ends before any one of:
526     - End-of-file.
527     - A line consisting of `'__END__'`,
528     - Character `Ctrl-D` or `Ctrl-Z`.
530 Example:
532 ```sh
533 $ cat t.txt
534 Leading garbage.
535 #!ruby
536 puts File.basename(Dir.pwd)
537 __END__
538 Trailing garbage.
540 $ ruby -x t.txt
541 ruby
544 The optional argument specifies the directory where the text file
545 is to be found;
546 the Ruby code is executed in that directory:
548 ```sh
549 $ cp t.txt /tmp/
550 $ ruby -x/tmp t.txt
556 If an argument is given, it must immediately follow the option
557 (no intervening whitespace or equal-sign character `'='`).
559 ### `--backtrace-limit`: \Set Backtrace Limit
561 Option `--backtrace-limit` sets a limit on the number of entries
562 to be displayed in a backtrace.
564 See Thread::Backtrace.limit.
566 ### `--copyright`: Print Ruby Copyright
568 Option `--copyright` prints a copyright message:
570 ```sh
571 $ ruby --copyright
572 ruby - Copyright (C) 1993-2024 Yukihiro Matsumoto
575 ### `--debug`: Alias for `-d`
577 Option `--debug` is an alias for
578 {option -d}[rdoc-ref:ruby/options.md@d-3A+Set+-24DEBUG+to+true].
580 ### `--disable`: Disable Features
582 Option `--disable` specifies features to be disabled;
583 the argument is a comma-separated list of the features to be disabled:
585 ```sh
586 ruby --disable=gems,rubyopt t.rb
589 The supported features:
591 - `gems`: Rubygems (default: enabled).
592 - `did_you_mean`: [`did_you_mean`](https://github.com/ruby/did_you_mean) (default: enabled).
593 - `rubyopt`: `RUBYOPT` environment variable (default: enabled).
594 - `frozen-string-literal`: Freeze all string literals (default: disabled).
595 - `jit`: JIT compiler (default: disabled).
597 See also {option --enable}[options_md.html#label--enable-3A+Enable+Features].
599 ### `--dump`: Dump Items
601 Option `--dump` specifies items to be dumped;
602 the argument is a comma-separated list of the items.
604 Some of the argument values cause the command to behave as if a different
605 option was given:
607 - `--dump=copyright`:
608   Same as {option \-\-copyright}[options_md.html#label--copyright-3A+Print+Ruby+Copyright].
609 - `--dump=help`:
610   Same as {option \-\-help}[options_md.html#label--help-3A+Print+Help+Message].
611 - `--dump=syntax`:
612   Same as {option -c}[rdoc-ref:ruby/options.md@c-3A+Check+Syntax].
613 - `--dump=usage`:
614   Same as {option -h}[rdoc-ref:ruby/options.md@h-3A+Print+Short+Help+Message].
615 - `--dump=version`:
616   Same as {option \-\-version}[options_md.html#label--version-3A+Print+Ruby+Version].
618 For other argument values and examples,
619 see {Option --dump}[option_dump_md.html].
621 ### `--enable`: Enable Features
623 Option `--enable` specifies features to be enabled;
624 the argument is a comma-separated list of the features to be enabled.
626 ```sh
627 ruby --enable=gems,rubyopt t.rb
630 For the features,
631 see {option --disable}[options_md.html#label--disable-3A+Disable+Features].
633 ### `--encoding`: Alias for `-E`.
635 Option `--encoding` is an alias for
636 {option -E}[rdoc-ref:ruby/options.md@E-3A+Set+Default+Encodings].
638 ### `--external-encoding`: \Set Default External \Encoding
640 Option `--external-encoding`
641 sets the default external encoding for the invoked Ruby program;
642 for values of +encoding+,
643 see {Encoding: Names and Aliases}[rdoc-ref:encodings.rdoc@Names+and+Aliases].
645 ```sh
646 $ ruby -e 'puts Encoding::default_external'
647 UTF-8
648 $ ruby --external-encoding=cesu-8 -e 'puts Encoding::default_external'
649 CESU-8
652 ### `--help`: Print Help Message
654 Option `--help` prints a long help message.
656 Arguments and additional options are ignored.
658 For a shorter help message, use option `-h`.
660 ### `--internal-encoding`: \Set Default Internal \Encoding
662 Option `--internal-encoding`
663 sets the default internal encoding for the invoked Ruby program;
664 for values of +encoding+,
665 see {Encoding: Names and Aliases}[rdoc-ref:encodings.rdoc@Names+and+Aliases].
667 ```sh
668 $ ruby -e 'puts Encoding::default_internal.nil?'
669 true
670 $ ruby --internal-encoding=cesu-8 -e 'puts Encoding::default_internal'
671 CESU-8
674 ### `--verbose`: \Set `$VERBOSE`
676 Option `--verbose` sets global variable `$VERBOSE` to `true`
677 and disables input from `$stdin`.
679 ### `--version`: Print Ruby Version
681 Option `--version` prints the version of the Ruby interpreter, then exits.
683 ## Experimental Options
685 These options are experimental in the current Ruby release,
686 and may be modified or withdrawn in later releases.
688 ### `--jit`
690 Option `-jit` enables JIT compilation with the default option.
692 #### `--jit-debug`
694 Option `--jit-debug` enables JIT debugging (very slow);
695 adds compiler flags if given.
697 #### `--jit-max-cache=num`
699 Option `--jit-max-cache=num` sets the maximum number of methods
700 to be JIT-ed in a cache; default: 100).
702 #### `--jit-min-calls=num`
704 Option `jit-min-calls=num` sets the minimum number of calls to trigger JIT
705 (for testing); default: 10000).
707 #### `--jit-save-temps`
709 Option `--jit-save-temps` saves JIT temporary files in $TMP or /tmp (for testing).
711 #### `--jit-verbose`
713 Option `--jit-verbose` prints JIT logs of level `num` or less
714 to `$stderr`; default: 0.
716 #### `--jit-wait`
718 Option `--jit-wait` waits until JIT compilation finishes every time (for testing).
720 #### `--jit-warnings`
722 Option `--jit-warnings` enables printing of JIT warnings.