@@ -129,8 +129,17 @@ async def get_transformed_code(argv: list[str]) -> typing.Optional[str]:
129129
130130
131131async def get_action_kwargs (argv : list [str ]) -> tuple [typing .Optional [str ], dict ]:
132- """Get the arguments to `piplite` subcommands from CLI-like tokens."""
132+ """Get the arguments to `piplite` subcommands from CLI-like tokens.
133133
134+ This function handles both command-line arguments and requirements files,
135+ ensuring that index URLs from either source are properly captured and used.
136+
137+ The precedence order is:
138+ 1. Command line --index-url flag
139+ 2. Index URL specified in requirements file
140+ 3. Default system index
141+
142+ """
134143 parser = _get_parser ()
135144
136145 try :
@@ -152,7 +161,7 @@ async def get_action_kwargs(argv: list[str]) -> tuple[typing.Optional[str], dict
152161 context = RequirementsContext ()
153162
154163 # If CLI index URL is provided, it should override within-file-level
155- # index URL for all requirements.
164+ # index URL for all requirements and take precedence .
156165 if args .index_url :
157166 context .index_url = args .index_url
158167
@@ -170,21 +179,17 @@ async def get_action_kwargs(argv: list[str]) -> tuple[typing.Optional[str], dict
170179 all_requirements .extend (context .requirements )
171180
172181 if all_requirements :
173- by_index = {}
174- file_index_url = None
182+ kwargs [ "requirements" ] = []
183+ active_index_url = None
175184
176185 for req , idx in all_requirements :
177- if idx :
178- file_index_url = idx
179- by_index . setdefault ( file_index_url , []) .append (req )
186+ if idx is not None :
187+ active_index_url = idx
188+ kwargs [ "requirements" ] .append (req )
180189
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 )
190+ # Set the final index URL, if we found one
191+ if active_index_url is not None :
192+ kwargs ["index_urls" ] = active_index_url
188193
189194 if args .pre :
190195 kwargs ["pre" ] = True
0 commit comments