Skip to content

Commit

Permalink
Support multiple args delimited by space (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherRegularDude authored Mar 30, 2021
1 parent 70d91f4 commit 5f82373
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ RSpec/EmptyLineAfterHook:
Naming/FileName:
Exclude:
- lib/ci-helper.rb

Style/HashConversion:
Exclude:
- spec/**/*_spec.rb
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ A command can accept list of options (parameters). Option values are passed thro
For example, the BundlerAudit command accepts the ignored_advisories option
You can set a value of this option by setting the flag `--ignored-advisories ignored-advisory`.
It should be noted that all hyphens in flag names are automatically replaced with underscores.
If command accepts array as option's value, you can separate values with either commas or spaces.
```bash
$ ci-helper BundlerAudit --ignored-advisories first,second
$ ci-helper BundlerAudit --ignored-advisories first,second # Valid
$ ci-helper BundlerAudit --ignored-advisories first second # Valid too!
```

List of available commands:
Expand Down
15 changes: 8 additions & 7 deletions lib/ci_helper/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ def prepare!
end

def parse_options_from(args)
args.each_slice(2).with_object({}) do |args, options|
key = Tools::Inflector.instance.underscore(args.first.split("--").last)
value = args[1] || ""
raise Error, "Not valid options" if key.empty?

options[key.to_sym] = value
end
args
.slice_when { |_el_before, el_after| el_after.start_with?("--") }
.each_with_object({}) do |commands, options|
key = Tools::Inflector.instance.underscore(commands.shift.split("--").last)
raise "Invalid options" if key.empty?
value = commands.size <= 1 ? commands.first : commands
options[key.to_sym] = value || ""
end
end

def perform_command!
Expand Down
3 changes: 3 additions & 0 deletions lib/ci_helper/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def boolean_option(key)

def plural_option(key)
return [] unless options.key?(key)
value = options[key]
return value if value.is_a?(Array)

options[key].split(",")
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ci_helper/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module CIHelper
VERSION = "0.4.1"
VERSION = "0.4.2"
end
14 changes: 13 additions & 1 deletion spec/ci_helper/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
let(:args) { [command_class_name, ""] }

it "raises error" do
expect { described_class.run!(args) }.to raise_error("Not valid options")
expect { described_class.run!(args) }.to raise_error("Invalid options")
end
end

Expand All @@ -46,6 +46,18 @@
end
end

context "with array in flag arguments" do
let(:args) { [command_class_name, "--ignored-advisories", "kek", "pek"] }

let(:expected_command) { "bundle exec bundler-audit check --update --ignore kek pek" }

it "properly parses this option" do
expect(client_response).to eq(0)
expect(popen_executed_commands.size).to eq(1)
expect(popen_executed_commands.first).to eq(expected_command)
end
end

context "with bad thread exit code" do
let(:process_value_exit_status) { 1 }
let(:raised_error_message) do
Expand Down

0 comments on commit 5f82373

Please sign in to comment.