Skip to content

Commit

Permalink
refactor(argv): Migrate arg parsing to CLI::Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
beeftornado committed Mar 16, 2020
1 parent 0d02a96 commit 28135ff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion brew-rmtree.rb
@@ -1,6 +1,6 @@
class BrewRmtree < Formula
homepage "https://github.com/beeftornado/homebrew-rmtree"
url "https://github.com/beeftornado/homebrew-rmtree.git", :tag => "2.2.4"
url "https://github.com/beeftornado/homebrew-rmtree.git", :tag => "2.2.6"
revision 1

head "https://github.com/beeftornado/homebrew-rmtree.git"
Expand Down
60 changes: 39 additions & 21 deletions cmd/brew-rmtree.rb
@@ -1,5 +1,5 @@
#:
#: * `rmtree` [`--force`] [`--dry-run`] [`--quiet`] formula1 [formula2] [formula3]... [`--ignore` formulaX]
#: * `rmtree` [`--force`] [`--dry-run`] [`--quiet`] [`--ignore=`formulaX,formulaY] formula1 [formula2] [formula3]...
#:
#: Remove a formula entirely, including all of its dependencies,
#: unless of course, they are used by another formula.
Expand All @@ -15,8 +15,7 @@
#: option will let you remove 'ruby'. This will NOT bypass dependency checks for the
#: formula's children. If 'ruby' depends on 'git', then 'git' will still not be removed.
#:
#: With `--ignore`, you can ignore some dependencies from being removed. This option
#: must come after the formulae to remove.
#: With `--ignore`, you can ignore some dependencies from being removed.
#:
#: You can use `--dry-run` to see what would be removed without actually removing
#: anything.
Expand All @@ -32,7 +31,7 @@
#: `brew rmtree` --force <formula>
#: Force the removal of <formula> even if other formulae depend on it.
#:
#: `brew rmtree` <formula> --ignore <formula2>
#: `brew rmtree` --ignore=<formula2> <formula>
#: Remove <formula>, but don't remove its dependency of <formula2>

require 'keg'
Expand All @@ -42,6 +41,7 @@
require 'shellwords'
require 'set'
require 'cmd/deps'
require 'cli/parser'

# I am not a ruby-ist and so my style may offend some

Expand Down Expand Up @@ -494,28 +494,46 @@ def rmtree(keg_name, force=false, ignored_kegs=[])
kegs_to_delete_in_order.each { |d| remove_keg(d, @dry_run) }
end

def rmtree_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
`rmtree` [<options>] [<formula>]
Remove a formula entirely, including all of its dependencies, unless of course,
they are used by another formula.
Warning:
Not all formulae declare their dependencies and therefore this command may end
up removing something you still need. It should be used with caution.
EOS
switch "--quiet",
description: "Hide output."
switch "--dry-run",
description: "See what would be removed without actually removing anything."
switch "--force",
description: "Force the removal of <formula> even if other formulae depend on it. " +
"You can override the dependency check for the top-level formula you " +
"are trying to remove. \nFor example, if you try to remove 'ruby', you most likely will " +
"not be able to do this because other fomulae specify this as a dependency. This " +
"option will enable you to remove 'ruby'. This will NOT bypass dependency checks for the " +
"formula's children. If 'ruby' depends on 'git', then 'git' will still not be removed. Sorry."
comma_array "--ignore=",
description: "Ignore some dependencies from being removed. Specify multiple values separated by a comma."
end
end

def main
force = false
ignored_kegs = []
rm_kegs = []
quiet = false
rmtree_args.parse

if ARGV.size < 1 or ['-h', '?', '--help'].include? ARGV.first
abort `brew rmtree --help`
end
force = Homebrew.args.force?
ignored_kegs = []
ignored_kegs.push(*Homebrew.args.ignore)
rm_kegs = Homebrew.args.named
quiet = Homebrew.args.quiet?
@dry_run = Homebrew.args.dry_run?

raise KegUnspecifiedError if Homebrew.args.no_named?

loop { case ARGV[0]
when '--quiet' then ARGV.shift; quiet = true
when '--dry-run' then ARGV.shift; @dry_run = true
when '--force' then ARGV.shift; force = true
when '--ignore' then ARGV.shift; ignored_kegs.push(*ARGV); break
when /^-/ then onoe "Unknown option: #{ARGV.shift.inspect}"; abort `brew rmtree --help`
when /^[^-]/ then rm_kegs.push(ARGV.shift)
else break
end; }

# Turn off output if 'quiet' is specified
if quiet
puts_off
Expand Down

0 comments on commit 28135ff

Please sign in to comment.