From c5236a407b2d640a9a43eb1da686c94bf42adcf3 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Tue, 2 Apr 2024 14:33:31 -0700 Subject: [PATCH] Port commands to use AbstractCommand --- cmd/alias.rb | 62 ++++++++++++++++++++++++-------------------------- cmd/unalias.rb | 28 +++++++++++------------ 2 files changed, 43 insertions(+), 47 deletions(-) diff --git a/cmd/alias.rb b/cmd/alias.rb index 26b3b90..1a75f4b 100755 --- a/cmd/alias.rb +++ b/cmd/alias.rb @@ -1,44 +1,42 @@ # frozen_string_literal: true -require "cli/parser" +require "abstract_command" require_relative "../lib/aliases" module Homebrew - module_function - - def alias_args - Homebrew::CLI::Parser.new do - usage_banner "`alias` [ ... | =]" - description <<~EOS - Show existing aliases. If no aliases are given, print the whole list. - EOS - switch "--edit", - description: "Edit aliases in a text editor. Either one or all aliases may be opened at once. " \ - "If the given alias doesn't exist it'll be pre-populated with a template." - named_args max: 1 - end - end - - def alias - args = alias_args.parse + module Cmd + class Alias < AbstractCommand + cmd_args do + usage_banner "`alias` [ ... | =]" + description <<~EOS + Show existing aliases. If no aliases are given, print the whole list. + EOS + switch "--edit", + description: "Edit aliases in a text editor. Either one or all aliases may be opened at once. " \ + "If the given alias doesn't exist it'll be pre-populated with a template." + named_args max: 1 + end - arg = args.named.first - split_arg = arg.split("=", 2) if arg.present? + def run + arg = args.named.first + split_arg = arg.split("=", 2) if arg.present? - Aliases.init + Aliases.init - if args.edit? - if arg.blank? - Aliases.edit_all - else - Aliases.edit arg + if args.edit? + if arg.blank? + Aliases.edit_all + else + Aliases.edit arg + end + elsif /.=./.match?(arg) + Aliases.add(*split_arg) + elsif arg.present? + Aliases.show arg + else + Aliases.show + end end - elsif /.=./.match?(arg) - Aliases.add(*split_arg) - elsif arg.present? - Aliases.show arg - else - Aliases.show end end end diff --git a/cmd/unalias.rb b/cmd/unalias.rb index aed24fa..ef825e9 100755 --- a/cmd/unalias.rb +++ b/cmd/unalias.rb @@ -1,24 +1,22 @@ # frozen_string_literal: true -require "cli/parser" +require "abstract_command" require_relative "../lib/aliases" module Homebrew - module_function + module Cmd + class Unalias < AbstractCommand + cmd_args do + description <<~EOS + Remove aliases. + EOS + named_args :alias, min: 1 + end - def unalias_args - Homebrew::CLI::Parser.new do - description <<~EOS - Remove aliases. - EOS - named_args :alias, min: 1 + def run + Aliases.init + args.named.each { |a| Aliases.remove a } + end end end - - def unalias - args = unalias_args.parse - - Aliases.init - args.named.each { |a| Aliases.remove a } - end end