Skip to content

Commit

Permalink
Change to collect all errors from parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Apr 17, 2020
1 parent 6b51fc2 commit 80eda65
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
9 changes: 8 additions & 1 deletion lib/tty/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def remaining
end
alias :remaining_args :remaining

# The parsing errors
#
# @api public
def errors
@errors ||= {}
end

# Parse command line arguments
#
# @param [Array<String>] argv
Expand All @@ -45,7 +52,7 @@ def remaining
# @api public
def parse(argv = ARGV, env = ENV, **config)
parser = Parser.new(self.class.parameters, **config)
@parameters, @remaining = parser.parse(argv, env)
@parameters, @remaining, @errors = parser.parse(argv, env)
end
end
end # Option
Expand Down
8 changes: 5 additions & 3 deletions lib/tty/option/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def initialize(parameters, **config)
def parse(argv, env)
argv = argv.dup
params = {}
errors = {}
ignored = []

# split argv into processable args and leftovers
Expand All @@ -54,16 +55,17 @@ def parse(argv, env)
PARAMETER_PARSERS.each do |name, parser_type|
parser = parser_type.new(parameters.send(name), **config)
if name == :environments
parsed, argv = parser.parse(argv, env)
parsed, argv, err = parser.parse(argv, env)
else
parsed, argv = parser.parse(argv)
parsed, argv, err = parser.parse(argv)
end
params.merge!(parsed)
errors.merge!(err)
end

argv += ignored unless ignored.empty?

[params, argv]
[params, argv, errors]
end
end # Parser
end # Option
Expand Down
30 changes: 26 additions & 4 deletions spec/unit/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ def new_command(&block)
end
end

expect {
cmd.parse([])
}.to raise_error(TTY::Option::MissingParameter,
"need to provide 'foo' argument")
cmd.parse([], raise_on_parsing_error: false)

expect(cmd.errors[:foo]).to eq({
missing_parameter: "need to provide 'foo' argument"
})
end

it "defauls argument to a value" do
Expand Down Expand Up @@ -647,6 +648,27 @@ def new_command(&block)
expect(cmd.params[:name]).to eq("web")
expect(cmd.params[:cmd_env]).to eq("production")
end

it "collects errors from different parsers" do
cmd = new_command do
argument(:foo) { required }

keyword(:bar) { required }

option(:baz) { required; long "--baz string" }

env(:qux) { required }
end

cmd.parse(%w[], raise_on_parsing_error: false)

expect(cmd.errors).to eq({
foo: { missing_parameter: "need to provide 'foo' argument" },
bar: { missing_parameter: "need to provide 'bar' keyword" },
baz: { missing_parameter: "need to provide '--baz' option" },
qux: { missing_parameter: "need to provide 'qux' environment" }
})
end
end

context "when remaining arguments" do
Expand Down

0 comments on commit 80eda65

Please sign in to comment.