@@ -130,7 +130,6 @@ async def get_transformed_code(argv: list[str]) -> typing.Optional[str]:
130130
131131async def get_action_kwargs (argv : list [str ]) -> tuple [typing .Optional [str ], dict ]:
132132 """Get the arguments to `piplite` subcommands from CLI-like tokens."""
133-
134133 parser = _get_parser ()
135134
136135 try :
@@ -151,40 +150,40 @@ async def get_action_kwargs(argv: list[str]) -> tuple[typing.Optional[str], dict
151150 for req_file in args .requirements or []:
152151 context = RequirementsContext ()
153152
154- # If CLI index URL is provided, it should override within-file-level
155- # index URL for all requirements.
156- if args .index_url :
157- context .index_url = args .index_url
158-
159153 if not Path (req_file ).exists ():
160154 warn (f"piplite could not find requirements file { req_file } " )
161155 continue
162156
157+ # First let the file be processed normally to capture any index URL
163158 for line_no , line in enumerate (
164159 Path (req_file ).read_text (encoding = "utf-8" ).splitlines ()
165160 ):
166161 await _packages_from_requirements_line (
167162 Path (req_file ), line_no + 1 , line , context
168163 )
169164
170- all_requirements .extend (context .requirements )
165+ # If CLI provided an index URL, it should override the file's index URL
166+ # We update all requirements to use the CLI index URL instead. Or, we use
167+ # whatever index URL was found in the file (if any).
168+ if args .index_url :
169+ all_requirements .extend (
170+ (req , args .index_url ) for req , _ in context .requirements
171+ )
172+ else :
173+ all_requirements .extend (context .requirements )
171174
172175 if all_requirements :
173- by_index = {}
174- file_index_url = None
176+ kwargs [ "requirements" ] = []
177+ active_index_url = None
175178
176179 for req , idx in all_requirements :
177- if idx :
178- file_index_url = idx
179- by_index . setdefault ( file_index_url , []) .append (req )
180+ if idx is not None :
181+ active_index_url = idx
182+ kwargs [ "requirements" ] .append (req )
180183
181- # Build final kwargs. We set the index URL if one was found
182- # (either passed to the CLI or passed within the requirements file)
183- kwargs ["requirements" ] = []
184- for idx , reqs in by_index .items ():
185- if idx :
186- kwargs ["index_urls" ] = idx
187- kwargs ["requirements" ].extend (reqs )
184+ # Set the final index URL, if we found one
185+ if active_index_url is not None :
186+ kwargs ["index_urls" ] = active_index_url
188187
189188 if args .pre :
190189 kwargs ["pre" ] = True
0 commit comments