Fixed extra_pip_arg parsing error in pip_repository rules. (#613)

diff --git a/examples/pip_repository_annotations/WORKSPACE b/examples/pip_repository_annotations/WORKSPACE
index d09ed69..95d978b 100644
--- a/examples/pip_repository_annotations/WORKSPACE
+++ b/examples/pip_repository_annotations/WORKSPACE
@@ -2,10 +2,9 @@
 
 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 
-http_archive(
+local_repository(
     name = "rules_python",
-    sha256 = "cd6730ed53a002c56ce4e2f396ba3b3be262fd7cb68339f0377a45e8227fe332",
-    url = "https://github.com/bazelbuild/rules_python/releases/download/0.5.0/rules_python-0.5.0.tar.gz",
+    path = "../..",
 )
 
 http_archive(
diff --git a/examples/pip_repository_annotations/requirements.in b/examples/pip_repository_annotations/requirements.in
index 2309722..a955311 100644
--- a/examples/pip_repository_annotations/requirements.in
+++ b/examples/pip_repository_annotations/requirements.in
@@ -1 +1,5 @@
+# This flag allows for regression testing requirements arguments in 
+# `pip_repository` rules.
+--extra-index-url https://pypi.python.org/simple/
+
 wheel
diff --git a/examples/pip_repository_annotations/requirements.txt b/examples/pip_repository_annotations/requirements.txt
index 51d1dfc..db762cb 100644
--- a/examples/pip_repository_annotations/requirements.txt
+++ b/examples/pip_repository_annotations/requirements.txt
@@ -4,6 +4,8 @@
 #
 #    bazel run //:requirements.update
 #
+--extra-index-url https://pypi.python.org/simple/
+
 wheel==0.37.1 \
     --hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a \
     --hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4
diff --git a/python/pip_install/parse_requirements_to_bzl/__init__.py b/python/pip_install/parse_requirements_to_bzl/__init__.py
index 7a23384..3fc2289 100644
--- a/python/pip_install/parse_requirements_to_bzl/__init__.py
+++ b/python/pip_install/parse_requirements_to_bzl/__init__.py
@@ -209,9 +209,11 @@
     arguments.parse_common_args(parser)
     args = parser.parse_args()
 
+    whl_library_args = parse_whl_library_args(args)
+
     # Check for any annotations which match packages in the locked requirements file
     install_requirements = parse_install_requirements(
-        args.requirements_lock, args.extra_pip_args
+        args.requirements_lock, whl_library_args["extra_pip_args"]
     )
     req_names = sorted([req.name for req, _ in install_requirements])
     annotations = args.annotations.collect(req_names)
@@ -230,8 +232,6 @@
         )
 
     with open("requirements.bzl", "w") as requirement_file:
-        whl_library_args = parse_whl_library_args(args)
-
         requirement_file.write(
             generate_parsed_requirements_contents(
                 requirements_lock=args.requirements_lock,
diff --git a/python/pip_install/parse_requirements_to_bzl/parse_requirements_to_bzl_test.py b/python/pip_install/parse_requirements_to_bzl/parse_requirements_to_bzl_test.py
index c0608bf..fb22d63 100644
--- a/python/pip_install/parse_requirements_to_bzl/parse_requirements_to_bzl_test.py
+++ b/python/pip_install/parse_requirements_to_bzl/parse_requirements_to_bzl_test.py
@@ -3,14 +3,20 @@
 import tempfile
 import unittest
 from pathlib import Path
+from textwrap import dedent
+
+from pip._internal.req.req_install import InstallRequirement
 
 from python.pip_install.parse_requirements_to_bzl import (
     generate_parsed_requirements_contents,
+    parse_install_requirements,
     parse_whl_library_args,
 )
 
 
 class TestParseRequirementsToBzl(unittest.TestCase):
+    maxDiff = None
+
     def test_generated_requirements_bzl(self) -> None:
         with tempfile.TemporaryDirectory() as temp_dir:
             requirements_lock = Path(temp_dir) / "requirements.txt"
@@ -64,6 +70,55 @@
             # Assert it gets set to an empty dict by default.
             self.assertIn("'environment': {}", contents, contents)
 
+    def test_parse_install_requirements_with_args(self):
+        # Test requirements files with varying arguments
+        for requirement_args in ("", "--index-url https://index.python.com"):
+            with tempfile.TemporaryDirectory() as temp_dir:
+                requirements_lock = Path(temp_dir) / "requirements.txt"
+                requirements_lock.write_text(
+                    dedent(
+                        """\
+                    {}
+
+                    wheel==0.37.1 \\
+                        --hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a \\
+                        --hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4
+                        # via -r requirements.in
+                    setuptools==58.2.0 \\
+                        --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11 \
+                        --hash=sha256:2c55bdb85d5bb460bd2e3b12052b677879cffcf46c0c688f2e5bf51d36001145
+                        # via -r requirements.in
+                    """.format(
+                            requirement_args
+                        )
+                    )
+                )
+
+                install_req_and_lines = parse_install_requirements(
+                    str(requirements_lock), ["-v"]
+                )
+
+                # There should only be two entries for the two requirements
+                self.assertEqual(len(install_req_and_lines), 2)
+
+                # The first index in each tuple is expected to be an `InstallRequirement` object
+                self.assertIsInstance(install_req_and_lines[0][0], InstallRequirement)
+                self.assertIsInstance(install_req_and_lines[1][0], InstallRequirement)
+
+                # Ensure the requirements text is correctly parsed with the trailing arguments
+                self.assertTupleEqual(
+                    install_req_and_lines[0][1:],
+                    (
+                        "wheel==0.37.1     --hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a     --hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4",
+                    ),
+                )
+                self.assertTupleEqual(
+                    install_req_and_lines[1][1:],
+                    (
+                        "setuptools==58.2.0     --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11                         --hash=sha256:2c55bdb85d5bb460bd2e3b12052b677879cffcf46c0c688f2e5bf51d36001145",
+                    ),
+                )
+
 
 if __name__ == "__main__":
     unittest.main()