From 28135ff24b41d0a87352b0b21be4ba38042443d6 Mon Sep 17 00:00:00 2001 From: Casey Duquette Date: Mon, 16 Mar 2020 03:08:52 -0700 Subject: [PATCH] refactor(argv): Migrate arg parsing to CLI::Parser --- brew-rmtree.rb | 2 +- cmd/brew-rmtree.rb | 60 ++++++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/brew-rmtree.rb b/brew-rmtree.rb index a839212..b42ac0f 100644 --- a/brew-rmtree.rb +++ b/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" diff --git a/cmd/brew-rmtree.rb b/cmd/brew-rmtree.rb index 68acb54..42975d3 100755 --- a/cmd/brew-rmtree.rb +++ b/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. @@ -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. @@ -32,7 +31,7 @@ #: `brew rmtree` --force #: Force the removal of even if other formulae depend on it. #: -#: `brew rmtree` --ignore +#: `brew rmtree` --ignore= #: Remove , but don't remove its dependency of require 'keg' @@ -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 @@ -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` [] [] + + 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 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