diff --git a/Library/Homebrew/cli/args.rb b/Library/Homebrew/cli/args.rb
index 61ffb1d7223e2868219b2e212e82daa4d4cc4b74..a951194e7cb9ce08a9625724aa6c8556dd41af11 100644
--- a/Library/Homebrew/cli/args.rb
+++ b/Library/Homebrew/cli/args.rb
@@ -5,13 +5,14 @@ require "ostruct"
 module Homebrew
   module CLI
     class Args < OpenStruct
-      attr_accessor :processed_options
+      attr_accessor :processed_options, :args_parsed
       # undefine tap to allow --tap argument
       undef tap
 
       def initialize(argv:)
         super
         @argv = argv
+        @args_parsed = false
         @processed_options = []
       end
 
@@ -132,7 +133,12 @@ module Homebrew
 
       def downcased_unique_named
         # Only lowercase names, not paths, bottle filenames or URLs
-        remaining.map do |arg|
+        arguments = if args_parsed
+          remaining
+        else
+          cmdline_args.reject { |arg| arg.start_with?("-") }
+        end
+        arguments.map do |arg|
           if arg.include?("/") || arg.end_with?(".tar.gz") || File.exist?(arg)
             arg
           else
@@ -141,10 +147,18 @@ module Homebrew
         end.uniq
       end
 
+      def head
+        (args_parsed && HEAD?) || cmdline_args.include?("--HEAD")
+      end
+
+      def devel
+        (args_parsed && devel?) || cmdline_args.include?("--devel")
+      end
+
       def spec(default = :stable)
-        if HEAD?
+        if head
           :head
-        elsif devel?
+        elsif devel
           :devel
         else
           default
diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb
index 3992537466cc20c3fd5f9388ada98fa59e593610..7e9b21cd6f6c4535991e4d58acb812353b986ae3 100644
--- a/Library/Homebrew/cli/parser.rb
+++ b/Library/Homebrew/cli/parser.rb
@@ -13,7 +13,7 @@ module Homebrew
       attr_reader :processed_options, :hide_from_man_page
 
       def self.parse(args = ARGV, &block)
-        new(&block).parse(args)
+        new(args, &block).parse(args)
       end
 
       def self.global_options
@@ -25,10 +25,11 @@ module Homebrew
         }
       end
 
-      def initialize(&block)
+      def initialize(args = ARGV, &block)
         @parser = OptionParser.new
         @args = Homebrew::CLI::Args.new(argv: ARGV_WITHOUT_MONKEY_PATCHING)
         @args[:remaining] = []
+        @args[:cmdline_args] = args.dup
         @constraints = []
         @conflicts = []
         @switch_sources = {}
@@ -139,7 +140,7 @@ module Homebrew
         end
         check_constraint_violations
         @args[:remaining] = remaining_args
-        @args_parsed = true
+        @args.args_parsed = @args_parsed = true
         @args.processed_options = @processed_options
         Homebrew.args = @args
         cmdline_args.freeze