Merge pull request #3980 from GauthamGoli/arg-parser-refactor
cli_parser: Add class method to make options declaration more readable
This commit is contained in:
		
						commit
						f37b0bc987
					
				@ -4,6 +4,10 @@ require "ostruct"
 | 
				
			|||||||
module Homebrew
 | 
					module Homebrew
 | 
				
			||||||
  module CLI
 | 
					  module CLI
 | 
				
			||||||
    class Parser
 | 
					    class Parser
 | 
				
			||||||
 | 
					      def self.parse(&block)
 | 
				
			||||||
 | 
					        new(&block).parse
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      def initialize(&block)
 | 
					      def initialize(&block)
 | 
				
			||||||
        @parser = OptionParser.new
 | 
					        @parser = OptionParser.new
 | 
				
			||||||
        @parsed_args = OpenStruct.new
 | 
					        @parsed_args = OpenStruct.new
 | 
				
			||||||
 | 
				
			|||||||
@ -55,7 +55,7 @@ module Homebrew
 | 
				
			|||||||
  module_function
 | 
					  module_function
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def audit
 | 
					  def audit
 | 
				
			||||||
    args = Homebrew::CLI::Parser.new do
 | 
					    args = Homebrew::CLI::Parser.parse do
 | 
				
			||||||
      switch      "--strict"
 | 
					      switch      "--strict"
 | 
				
			||||||
      switch      "--online"
 | 
					      switch      "--online"
 | 
				
			||||||
      switch      "--new-formula"
 | 
					      switch      "--new-formula"
 | 
				
			||||||
@ -67,7 +67,7 @@ module Homebrew
 | 
				
			|||||||
      comma_array "--except"
 | 
					      comma_array "--except"
 | 
				
			||||||
      comma_array "--only-cops"
 | 
					      comma_array "--only-cops"
 | 
				
			||||||
      comma_array "--except-cops"
 | 
					      comma_array "--except-cops"
 | 
				
			||||||
    end.parse
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Homebrew.auditing = true
 | 
					    Homebrew.auditing = true
 | 
				
			||||||
    inject_dump_stats!(FormulaAuditor, /^audit_/) if args.audit_debug?
 | 
					    inject_dump_stats!(FormulaAuditor, /^audit_/) if args.audit_debug?
 | 
				
			||||||
 | 
				
			|||||||
@ -11,9 +11,9 @@ module Homebrew
 | 
				
			|||||||
  module_function
 | 
					  module_function
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def edit
 | 
					  def edit
 | 
				
			||||||
    args = Homebrew::CLI::Parser.new do
 | 
					    args = Homebrew::CLI::Parser.parse do
 | 
				
			||||||
      switch "--force"
 | 
					      switch "--force"
 | 
				
			||||||
    end.parse
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    unless (HOMEBREW_REPOSITORY/".git").directory?
 | 
					    unless (HOMEBREW_REPOSITORY/".git").directory?
 | 
				
			||||||
      raise <<~EOS
 | 
					      raise <<~EOS
 | 
				
			||||||
 | 
				
			|||||||
@ -23,10 +23,10 @@ module Homebrew
 | 
				
			|||||||
  module_function
 | 
					  module_function
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def irb
 | 
					  def irb
 | 
				
			||||||
    args = Homebrew::CLI::Parser.new do
 | 
					    args = Homebrew::CLI::Parser.parse do
 | 
				
			||||||
      switch "--examples"
 | 
					      switch "--examples"
 | 
				
			||||||
      switch "--pry", env: :pry
 | 
					      switch "--pry", env: :pry
 | 
				
			||||||
    end.parse
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if args.examples?
 | 
					    if args.examples?
 | 
				
			||||||
      puts "'v8'.f # => instance of the v8 formula"
 | 
					      puts "'v8'.f # => instance of the v8 formula"
 | 
				
			||||||
 | 
				
			|||||||
@ -20,10 +20,10 @@ module Homebrew
 | 
				
			|||||||
  TARGET_DOC_PATH = HOMEBREW_REPOSITORY/"docs"
 | 
					  TARGET_DOC_PATH = HOMEBREW_REPOSITORY/"docs"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def man
 | 
					  def man
 | 
				
			||||||
    @args = Homebrew::CLI::Parser.new do
 | 
					    @args = Homebrew::CLI::Parser.parse do
 | 
				
			||||||
      switch "--fail-if-changed"
 | 
					      switch "--fail-if-changed"
 | 
				
			||||||
      switch "--link"
 | 
					      switch "--link"
 | 
				
			||||||
    end.parse
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    raise UsageError unless ARGV.named.empty?
 | 
					    raise UsageError unless ARGV.named.empty?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -11,9 +11,9 @@ module Homebrew
 | 
				
			|||||||
  module_function
 | 
					  module_function
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def release_notes
 | 
					  def release_notes
 | 
				
			||||||
    args = Homebrew::CLI::Parser.new do
 | 
					    args = Homebrew::CLI::Parser.parse do
 | 
				
			||||||
      switch "--markdown"
 | 
					      switch "--markdown"
 | 
				
			||||||
    end.parse
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    previous_tag = ARGV.named.first
 | 
					    previous_tag = ARGV.named.first
 | 
				
			||||||
    previous_tag ||= Utils.popen_read("git tag --list --sort=-version:refname")
 | 
					    previous_tag ||= Utils.popen_read("git tag --list --sort=-version:refname")
 | 
				
			||||||
 | 
				
			|||||||
@ -23,7 +23,7 @@ module Homebrew
 | 
				
			|||||||
  module_function
 | 
					  module_function
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def tests
 | 
					  def tests
 | 
				
			||||||
    args = Homebrew::CLI::Parser.new do
 | 
					    args = Homebrew::CLI::Parser.parse do
 | 
				
			||||||
      switch "--no-compat"
 | 
					      switch "--no-compat"
 | 
				
			||||||
      switch "--generic"
 | 
					      switch "--generic"
 | 
				
			||||||
      switch "-v", "--verbose"
 | 
					      switch "-v", "--verbose"
 | 
				
			||||||
@ -31,7 +31,7 @@ module Homebrew
 | 
				
			|||||||
      switch "--online"
 | 
					      switch "--online"
 | 
				
			||||||
      flag   "--only", required: true
 | 
					      flag   "--only", required: true
 | 
				
			||||||
      flag   "--seed", required: true
 | 
					      flag   "--seed", required: true
 | 
				
			||||||
    end.parse
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    HOMEBREW_LIBRARY_PATH.cd do
 | 
					    HOMEBREW_LIBRARY_PATH.cd do
 | 
				
			||||||
      ENV.delete("HOMEBREW_VERBOSE")
 | 
					      ENV.delete("HOMEBREW_VERBOSE")
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user