Skip to content

Commit

Permalink
Convert double-dash arguments into filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
djanowski committed Jun 7, 2013
1 parent 6c95c43 commit f533c39
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
22 changes: 19 additions & 3 deletions bin/bjob
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

require_relative "../vendor/clap"

class Args < Hash
def [](item)
return super(item) if include?(item)

if item.start_with?("--")
key = item[2..-1]

-> value { fetch(:*).call(key, value) }
end
end
end

if File.exist?(".bjobrc")
ARGV.concat(File.read(".bjobrc").split("\n"))
end
Expand All @@ -10,11 +22,15 @@ exit(1) if ARGV.empty?

ENV["BATCH_INTERACTIVE"] = ENV["PS1"] ? "1" : "0"

args = Clap.run ARGV,
filter = {}

args = Clap.run ARGV, Args[
"-r" => -> file { require file },
"-i" => -> { ENV["BATCH_INTERACTIVE"] = "1" },
"-s" => -> { ENV["BATCH_INTERACTIVE"] = "0" }
"-s" => -> { ENV["BATCH_INTERACTIVE"] = "0" },
:* => -> key, value { filter[key.to_sym] = value }
]

constant = Module.const_get(args.first)

constant.run
constant.run((filter if filter.size > 0))
6 changes: 6 additions & 0 deletions test/bin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,10 @@ def assert_empty(str)

assert_empty err
end

test "parses command line arguments as a filter" do |bjob, _, capture, root|
out, err, status = bjob.(%w[-r ./test/jobs/ten Ten --count 5])

assert_equal 5, Dir["test/tmp/ten/*.txt"].size
end
end
6 changes: 5 additions & 1 deletion test/jobs/ten.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def process(item)
end

def items(filter = nil)
(0..9).to_a
if filter && filter.include?(:count)
filter.fetch(:count).to_i.times.to_a
else
(0..9).to_a
end
end
end

0 comments on commit f533c39

Please sign in to comment.