Skip to content

Commit

Permalink
OptionParser#parse defaults to ARGV. (crystal-lang#8041)
Browse files Browse the repository at this point in the history
OptionParser#parse! deprecate.
  • Loading branch information
didactic-drunk authored and dnamsons committed Jan 10, 2020
1 parent 9b01053 commit 39b197f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion samples/mt_gc_test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fibers_num = 1_000
loops_num = 20
mode = :run

OptionParser.parse! do |parser|
OptionParser.parse do |parser|
parser.on("-i", "--ips", "Benchmark with ips") { mode = :ips }
parser.on("-m", "--measure", "Benchmark with measure") { mode = :measure }
parser.on("-f FIBERS", "--fibers=FIBERS", "Specifies the number of fibers") { |v| fibers_num = v.to_i }
Expand Down
2 changes: 1 addition & 1 deletion samples/wordcount.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end
output_filename = nil
ignore_case = false

OptionParser.parse! do |opts|
OptionParser.parse do |opts|
opts.banner = "Usage: wordcount [OPTIONS] [FILES]"
opts.on("-o NAME", "set output filename") do |filename|
output_filename = filename
Expand Down
16 changes: 7 additions & 9 deletions src/option_parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@ class OptionParser
block : String ->

# Creates a new parser, with its configuration specified in the block,
# and uses it to parse the passed *args*.
def self.parse(args) : self
# and uses it to parse the passed *args* (defaults to `ARGV`).
def self.parse(args = ARGV) : self
parser = OptionParser.new
yield parser
parser.parse(args)
parser
end

# Creates a new parser, with its configuration specified in the block,
# and uses it to parse the arguments passed to the program.
@[Deprecated("Use `parse` instead.")]
def self.parse! : self
parse(ARGV) { |parser| yield parser }
end
Expand Down Expand Up @@ -181,15 +180,14 @@ class OptionParser
end
end

# Parses the passed *args*, running the handlers associated to each option.
def parse(args)
# Parses the passed *args* (defaults to `ARGV`), running the handlers associated to each option.
def parse(args = ARGV)
ParseTask.new(self, args).parse
end

# Parses the passed the arguments passed to the program,
# running the handlers associated to each option.
@[Deprecated("Use `parse` instead.")]
def parse!
parse ARGV
parse
end

private def check_starts_with_dash(arg, name, allow_empty = false)
Expand Down
2 changes: 1 addition & 1 deletion src/spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ require "./spec/dsl"
module Spec
end

OptionParser.parse! do |opts|
OptionParser.parse do |opts|
opts.banner = "crystal spec runner"
opts.on("-e ", "--example STRING", "run examples whose full nested names include STRING") do |pattern|
Spec.pattern = pattern
Expand Down

0 comments on commit 39b197f

Please sign in to comment.