Created
August 16, 2008 15:28
-
-
Save sunny/5722 to your computer and use it in GitHub Desktop.
Simple way of taking out single arguments from ARGV if they match a given regexp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# args_remove | |
# Simple way of taking out single arguments from ARGV if they match | |
# a given regexp. | |
# | |
# by Sunny Ripert under the WTFPL | |
# | |
# Example: | |
# | |
# if args_remove '--usage' | |
# puts "Usage: app [--progress] [--verbose] [-r|--recursive] [--days=N] [--tag=X --tag=Y...] files" | |
# exit 0 | |
# end | |
# | |
# progress = args_remove '--progress' | |
# recursive = args_remove /^(-r|--recursive)$/ | |
# verbosity = args_remove('--verbose') ? :high : :low | |
# days = args_remove(/^--days=(\d+)$/) { |match| match[1].to_i } || 42 | |
# | |
# tags = [] | |
# while tag = args_remove(/^--tag=(\w+)$/) { |match| match[1] } | |
# tags.push tag | |
# end | |
# | |
# files = ARGV | |
def args_remove(regexp) | |
ARGV.each_with_index do |arg, key| | |
if match = arg.match(regexp) | |
ARGV.slice!(key) | |
return block_given? ? yield(match) : true | |
end | |
end | |
false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment