Skip to content

Commit c6ef8e4

Browse files
committed
cli: optimize --reconnect option implementation
1 parent 00f8d96 commit c6ef8e4

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

pymobiledevice3/__main__.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,19 @@ def cli(reconnect: bool) -> None:
179179
RECONNECT = reconnect
180180

181181

182-
def main() -> None:
182+
def invoke_cli_with_error_handling() -> bool:
183+
"""
184+
Invoke the command line interface and return `True` if the failure reason of the command was that the device was
185+
disconnected.
186+
"""
183187
try:
184188
cli()
185189
except NoDeviceConnectedError:
186190
logger.error('Device is not connected')
191+
return True
187192
except ConnectionAbortedError:
188193
logger.error('Device was disconnected')
189-
if RECONNECT:
190-
lockdown = retry_create_using_usbmux(None)
191-
lockdown.close()
192-
cli()
194+
return True
193195
except NotPairedError:
194196
logger.error('Device is not paired')
195197
except UserDeniedPairingError:
@@ -258,6 +260,24 @@ def main() -> None:
258260
except QuicProtocolNotSupportedError as e:
259261
logger.error(str(e))
260262

263+
return False
264+
265+
266+
def main() -> None:
267+
# Retry to invoke the CLI
268+
while invoke_cli_with_error_handling():
269+
# If reached here, this means the failure reason was that the device is disconnected
270+
if not RECONNECT:
271+
# If not invoked with the `--reconnect` option, break here
272+
break
273+
try:
274+
# Wait for the device to be available again
275+
lockdown = retry_create_using_usbmux(None)
276+
lockdown.close()
277+
except KeyboardInterrupt:
278+
print('Aborted.')
279+
break
280+
261281

262282
if __name__ == '__main__':
263283
main()

0 commit comments

Comments
 (0)