Commit graph

72 commits

Author SHA1 Message Date
Nobuyoshi Nakada
8f19f0aad5 [ruby/optparse] Fix completion of key-value pairs array
Enum array may be the list of pairs of key and value.  Check if only
key is completable, not pair.

Fix https://github.com/ruby/optparse/pull/93
Fix https://github.com/ruby/optparse/pull/94

a8d0ba8dac
2025-03-17 10:18:49 +00:00
Koichi ITO
f4c16c57aa [ruby/optparse] Make the result of tty? obtainable with flexible stdout
In mock testing for stdout, `StringIO.new` is sometimes used to redirect the output.
In such cases, the assignment is done with `$stdout = StringIO.new`, not the constant `STDOUT`.
e.g., https://github.com/rubocop/rubocop/blob/v1.71.1/lib/rubocop/rspec/shared_contexts.rb#L154-L164

After assigning `StringIO.new`, `$stdout.tty?` returns `false`,
allowing the standard output destination to be switched during test execution.

```ruby
STDOUT.tty?       # => true
StringIO.new.tty? # => false
```

However, since `STDOUT.tty?` returns `true`, a failure occurred in environments
where the environment variables `RUBY_PAGER` or `PAGER` are set.
e.g., https://github.com/rubocop/rubocop/pull/13784

To address this, `STDOUT` has been updated to `$stdout` so that the result of `tty?` can be flexibly overridden.

A potential concern is that `$stdout`, unlike `STDOUT`,
does not always represent the standard output at the time the Ruby process started.
However, no concrete examples of issues related to this have been identified.

`STDOUT.tty?` is the logic of optparse introduced in https://github.com/ruby/optparse/pull/70.

This PR replaces `STDOUT` with `$stdout` throughout, based on the assumption
that `$stdout` is sufficient for use with optparse.

262cf6f9ac
2025-03-10 10:19:58 +00:00
Nobuyoshi Nakada
9e265b583b [ruby/optparse] Add post-check of value
Fix https://github.com/ruby/optparse/pull/80

050a87d029
2025-03-10 09:55:29 +00:00
Nobuyoshi Nakada
cdf36d6bfd [ruby/optparse] Allow non-string enum list #79
Command line arguments are strings, convert enum list elements to
strings to match.

c5ec052efc
2025-03-09 14:32:17 +00:00
Nobuyoshi Nakada
213c27825a [ruby/optparse] Add test for enum arguments
0a0e977b7c
2025-03-09 14:32:17 +00:00
fatkodima
a35d324862 [ruby/optparse] Fix parsing array arguments with :into option
19700e96d8
2024-08-05 02:28:12 +00:00
Nobuyoshi Nakada
5d76fe6b2a [ruby/optparse] Invoke pager for --help
77dccce37c
2024-03-01 07:10:08 +00:00
Nobuyoshi Nakada
41c0fb6991 [ruby/optparse] Add exact: keyword argument
07e83673a8
2024-02-23 21:16:59 +09:00
Nobuyoshi Nakada
db73226bf6 [ruby/optparse] Adjust arguments for lambda-callbacks
Rake uses [lambda] as callbacks.
Calling it without omitted argument raises an `ArgumentError`.

lambda: https://github.com/ruby/rake/blob/master/lib/rake/application.rb#L543

213cb03b59
2024-02-09 19:58:31 +09:00
Nobuyoshi Nakada
2c6767b71e [ruby/optparse] Respect default values in block parameters
Fix https://github.com/ruby/optparse/pull/55

9d53e74aa4
2024-02-09 19:58:19 +09:00
fatkodima
f7a407cabd [ruby/optparse] Fix require_exact to work with options defined as --[no]-something
4e346ad337
2024-02-09 03:31:13 +00:00
Junichi Ito
cea6951ecf [ruby/optparse] Add symbolize_names to getopts
3e63d878f8
2022-12-26 15:09:21 +09:00
Nobuyoshi Nakada
502ca37dde [ruby/optparse] The encoding argument of Regexp.new has been ignored since 1.9
766f567405
2022-12-21 14:09:12 +09:00
Yusuke Endoh
f3ad68dd16 [ruby/optparse] Fix the test failure i ruby/ruby
```
$ make test-all TESTS=test/optparse/
...

[148/178] TestOptionParserDidYouMean#test_raise_unknown = 0.00 s
  1) Failure:
TestOptionParserDidYouMean#test_raise_unknown [/home/mame/work/ruby/test/optparse/test_optparse.rb:106]:
<["--bar"]> expected but was
<[]>.
```

In the old test/unit (bundled in ruby/ruby), when a test class inherits from
another test class, the child class runs all the tests defined in the parent
class.
However, it looks like the new test/unit does not do so. This is because the
test failure does not occur in ruby/optparse.

As a tentative solution, this changes the option names in TestOptionParser to
avoid the name conflict with TestOptionParserDidYouMean.

fee86ef7a4
2022-11-28 16:20:40 +00:00
Nobuyoshi Nakada
0bfb185654 [ruby/optparse] Add raise_unknown flag
(https://github.com/ruby/optparse/pull/38)

12529653cd
2022-11-28 14:24:06 +00:00
Nobuyoshi Nakada
d5fb76a6c8 [ruby/optparse] Add tests for load(into:)
51f7e060ee
2022-10-29 13:47:23 +00:00
Nobuyoshi Nakada
4dac53f0a4 [ruby/optparse] Add tests for OptionParser#load
fb34a1d3a3
2022-10-29 13:40:19 +00:00
Maciek Rząsa
0f231f2bab [ruby/optparse] Don't treat empty string as an option description
078638ee6d
2022-10-08 19:30:34 +09:00
konsolebox
419ad1e13e [ruby/optparse] Also accept '-' as an optional argument (https://github.com/ruby/optparse/pull/35)
f2b8318631
2022-07-29 19:10:10 +09:00
Jeremy Evans
ab3cb29bd9 Avoid defining the same test class in multiple files
Should fix issues with parallel testing sometimes not running all
tests.

This should be viewed skipping whitespace changes.

Fixes [Bug #18731]
2022-04-22 15:00:16 -07:00
Nobuyoshi Nakada
e28dbd0f3d
[ruby/optparse] Fix for ruby 3.0 or earlier
9e29d86c12
2022-01-12 21:16:02 +09:00
Nobuyoshi Nakada
743a41f7e3
[ruby/optparse] DidYouMean::PlainFormatter is deprecated
0ac9957696
2022-01-12 21:16:02 +09:00
Yuki Nishijima
03c54a09ec Revert "Revert "Replace an deprecated constant with a new one in did_you_mean""
This reverts commit 1527f7569b.
2021-12-24 23:21:55 +09:00
Yuki Nishijima
1527f7569b Revert "Replace an deprecated constant with a new one in did_you_mean"
This reverts commit 573d9d3a4a.
2021-12-23 10:48:27 +09:00
Yuki Nishijima
573d9d3a4a Replace an deprecated constant with a new one in did_you_mean 2021-12-22 21:21:27 +09:00
Nobuyoshi Nakada
b5d9c44267 [ruby/optparse] Fix typo [ci skip]
0aa18752cf
2021-11-03 19:55:24 +09:00
Yuki Nishijima
905be49bf6 Remove the test for DYM's verbose formatter 2021-10-22 21:14:01 -04:00
Alexander Popov
2afbe7113a
[ruby/optparse] Add EditorConfig file
More info here: https://editorconfig.org/

For example, `ruby/ruby` has it: 05ebaee/.editorconfig

Also fix some offenses.

29402e7e0e
2021-04-28 11:56:15 +09:00
Nobuyoshi Nakada
e8317d90b0
[ruby/optparse] Fixed error message of unparsed non-option
Close https://github.com/ruby/optparse/issues/3

94c5cf4032
2021-03-29 19:37:24 +09:00
Jeremy Evans
eca8ffaa0b
[ruby/optparse] Add OptionParser#require_exact accessor
This allows you to disable allowing abbreviations of long options
and using short options for long options.

Implements Ruby Feature #11523

dfefb2d2e2
2021-03-29 15:55:41 +09:00
Nobuyoshi Nakada
5ad32d5504
LoadError is not a subclass of StandardError 2019-11-29 11:25:23 +09:00
MSP-Greg
71ffe40a44 test/optparse/test_did_you_mean.rb - fix suggestion order 2019-10-21 09:53:56 +09:00
Nobuyoshi Nakada
e2b719bed6
Support DidYouMean by AmbiguousOption too 2019-10-18 17:46:53 +09:00
Nobuyoshi Nakada
e29e49abf0
Default DidYouMean.formatter to PlainFormatter 2019-10-18 16:43:40 +09:00
Nobuyoshi Nakada
d4e52f672c
Added no suggestion test case 2019-10-18 15:19:26 +09:00
Nobuyoshi Nakada
46fa301e82
Use DidYouMean.formatter
Instead of building messages separately.
2019-10-18 15:18:36 +09:00
Yusuke Endoh
c3b64a86bc lib/optparse.rb: Show a did_you_mean hint for unknown option
```
require 'optparse'

OptionParser.new do |opts|
  opts.on("-f", "--foo", "foo") {|v| }
  opts.on("-b", "--bar", "bar") {|v| }
  opts.on("-c", "--baz", "baz") {|v| }
end.parse!
```

```
$ ruby test.rb --baa
Traceback (most recent call last):
test.rb:7:in `<main>': invalid option: --baa (OptionParser::InvalidOption)
Did you mean?  baz
               bar
```
2019-10-18 12:20:58 +09:00
Nobuyoshi Nakada
ef45fd53e4
Fixed the test-case class name for zsh 2019-06-28 14:09:17 +09:00
Nobuyoshi Nakada
c9b28fd7ae
Allow --enable/--disable options to take an argument
[Bug #15850]
2019-05-15 17:31:11 +09:00
nobu
2ad7fb6dc1 optparse: Suppress warnings
Ruby warns "instance variable `@version` not initialized" for optparse when it receives `--version` option.

`test.rb`

```ruby
require 'optparse'
Version = '1'
OptionParser.new.parse(ARGV)
```

```
$ ruby -w test.rb --version
/home/pocke/.rbenv/versions/2.5.1/lib/ruby/2.5.0/optparse.rb:1168: warning: instance variable @version not initialized
/home/pocke/.rbenv/versions/2.5.1/lib/ruby/2.5.0/optparse.rb:1175: warning: instance variable @release not initialized
test 1
```

This change will suppress the warnings.

[Fix GH-1871]

From: Masataka Pocke Kuwabara <kuwabara@pocke.me>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63342 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-06 09:53:33 +00:00
nobu
db1de35a31 optparse.rb: froze string literals
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62047 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-26 03:41:04 +00:00
nobu
30d23ec903 multiple arguments to write
Make write methods of IO-like objects accept multiple arguments,
as well as IO#write.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60383 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-23 14:05:07 +00:00
nobu
3fe5d355a8 optparse.rb: case-sensitive candidate
* lib/optparse.rb (candidate): short options are case-sensitive by
  the default, should not match case-different options..
  https://github.com/mernen/completion-ruby/pull/9#issuecomment-317287946

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59412 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-24 01:56:04 +00:00
nobu
89bf90a370 optparse.rb: get rid of eval
* lib/optparse.rb: try Float() and Integer() instead of eval,
  which does too much things.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59312 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-12 02:49:53 +00:00
nobu
8ddf0263bb Fix DecimalInteger converting to octal bug
Previously if the input started with a '0' then it will be converted
as octal even though it has been specified as a decimal. This commit
forces the number to be interpreted as a decimal.

[ruby-core:81927] [Bug #13722] [Fix GH-1665]
Author:    william <william.mccumstie@outlook.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59273 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-06 11:49:03 +00:00
nobu
0687baaf57 optparse/kwargs.rb
* lib/optparse/kwargs.rb (OptionParser#define_by_keywords):
  [EXPERIMENTAL] extract command line option definitions from the
  information of keyword arguments.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56423 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-14 13:26:13 +00:00
nobu
99ad512486 optparse.rb: hyphenize
* lib/optparse.rb (make_switch, parse_in_order): unify underscores
  to hyphens.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56419 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-14 08:20:26 +00:00
nobu
dd1be236d9 optparse.rb: fix char class option
* lib/optparse.rb (OptionParser::Completion.candidate): get rid of
  nil as key names.  [ruby-core:75773] [Bug #12438]
* lib/optparse.rb (OptionParser#make_switch): char class option
  cannot be NoArgument, default to RequiredArgument.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55228 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-31 08:28:48 +00:00
nobu
49684589cd optparse.rb: into kwdarg
* lib/optparse.rb (OptionParser#order!): add `into` optional
  keyword argument to store the results.  [Feature #11191]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53444 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-01-06 08:23:10 +00:00
naruse
3e92b635fb Add frozen_string_literal: false for all files
When you change this to true, you may need to add more tests.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53141 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-16 05:07:31 +00:00