argv: move options_only to cli/args
This commit is contained in:
		
							parent
							
								
									7dd0466bf3
								
							
						
					
					
						commit
						4586632855
					
				@ -12,6 +12,22 @@ module Homebrew
 | 
			
		||||
        super
 | 
			
		||||
        @argv = argv
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def to_cli_option(name)
 | 
			
		||||
        if name.length == 2
 | 
			
		||||
          "-#{name.tr("?", "")}"
 | 
			
		||||
        else
 | 
			
		||||
          "--#{name.tr("_", "-").tr("?", "")}"
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def options_only
 | 
			
		||||
        to_h.keys
 | 
			
		||||
            .map(&:to_s)
 | 
			
		||||
            .reject { |name| %w[argv remaining].include?(name) }
 | 
			
		||||
            .map(&method(:to_cli_option))
 | 
			
		||||
            .select { |arg| arg.start_with?("-") }
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@ OPTION_DESC_WIDTH = 43
 | 
			
		||||
module Homebrew
 | 
			
		||||
  module CLI
 | 
			
		||||
    class Parser
 | 
			
		||||
      attr_reader :processed_options, :hide_from_man_page
 | 
			
		||||
      attr_reader :processed_options, :hide_from_man_page, :args
 | 
			
		||||
 | 
			
		||||
      def self.parse(args = ARGV, &block)
 | 
			
		||||
        new(&block).parse(args)
 | 
			
		||||
 | 
			
		||||
@ -25,6 +25,6 @@ module Homebrew
 | 
			
		||||
    raise "`brew cat` doesn't support multiple arguments" if args.remaining.size > 1
 | 
			
		||||
 | 
			
		||||
    cd HOMEBREW_REPOSITORY
 | 
			
		||||
    safe_system "cat", formulae.first.path, *ARGV.options_only
 | 
			
		||||
    safe_system "cat", formulae.first.path, *Homebrew.args.options_only
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -70,7 +70,7 @@ module Homebrew
 | 
			
		||||
        puts Formatter.columns(full_names)
 | 
			
		||||
      else
 | 
			
		||||
        ENV["CLICOLOR"] = nil
 | 
			
		||||
        safe_system "ls", *ARGV.options_only << HOMEBREW_CELLAR
 | 
			
		||||
        safe_system "ls", *Homebrew.args.options_only << HOMEBREW_CELLAR
 | 
			
		||||
      end
 | 
			
		||||
    elsif args.verbose? || !$stdout.tty?
 | 
			
		||||
      system_command! "find", args: ARGV.kegs.map(&:to_s) + %w[-not -type d -print], print_stdout: true
 | 
			
		||||
 | 
			
		||||
@ -57,7 +57,7 @@ module Homebrew
 | 
			
		||||
          git -C "#{git_cd}" fetch --unshallow
 | 
			
		||||
      EOS
 | 
			
		||||
    end
 | 
			
		||||
    args = ARGV.options_only
 | 
			
		||||
    args = Homebrew.args.options_only
 | 
			
		||||
    args += ["--follow", "--", path] unless path.nil?
 | 
			
		||||
    system "git", "log", *args
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@ -82,7 +82,7 @@ module Homebrew
 | 
			
		||||
          --
 | 
			
		||||
          #{HOMEBREW_LIBRARY_PATH}/test.rb
 | 
			
		||||
          #{f.path}
 | 
			
		||||
        ].concat(ARGV.options_only)
 | 
			
		||||
        ].concat(Homebrew.args.options_only)
 | 
			
		||||
 | 
			
		||||
        if f.head?
 | 
			
		||||
          args << "--HEAD"
 | 
			
		||||
 | 
			
		||||
@ -210,4 +210,20 @@ describe Homebrew::CLI::Parser do
 | 
			
		||||
      expect { parser.parse(["--switch-b"]) }.to raise_error(RuntimeError, /Arguments were already parsed!/)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "test argv extensions" do
 | 
			
		||||
    subject(:parser) {
 | 
			
		||||
      described_class.new do
 | 
			
		||||
        switch "--foo"
 | 
			
		||||
        switch "-v"
 | 
			
		||||
        switch "-d"
 | 
			
		||||
        switch "-s"
 | 
			
		||||
      end
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    it "#options_only" do
 | 
			
		||||
      parser.parse(["--foo", "-vds", "a", "b", "cdefg"])
 | 
			
		||||
      expect(Homebrew.args.options_only).to eq %w[--foo -v -d -s]
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user