""":mod:`ugoira.cli` --- Ugoira Download Command Line
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This provide ugoira download command line executable :program:`ugoira`:

.. sourcecode:: console

   $ ugoira
   Usage: ugoira [OPTIONS] ILLUST_ID

.. describe:: ILLUST_ID

   ``illust-id`` of your wants to download.
   It can get from Pixiv image url. see ``illust-id`` parameter in url.

There are options as well:

.. option:: --speed <amount>

   If you want to make animated image file, you can
   divide seconds of each frames's interval.
   Default value is 1.0 (no change)

.. option:: --format <file_format>
.. option:: -f <file_format>

   Format of result file.
   You can select apng, gif, and zip format.
   Default value is gif.

.. option:: --dest <path>
.. option:: -d <path>

   Path of output file.
   Default is ``./<illust-id>.<format>``\.

"""
from typing import Optional

from click import Path, argument, command, echo, option

from .lib import PixivError, download_ugoira_zip, is_ugoira, save

__all__ = 'ugoira',


@command()
@option(
    '--speed',
    type=float,
    default=1.0,
    help="If you want to make animated image file, "
         "you can divide seconds of each frames's interval."
         " Default value is 1.0 (no change)",
)
@option(
    '--format',
    '-f',
    type=str,
    default='gif',
    help='Format of result file.'
         ' You can select apng, gif, and zip format.'
         ' Default value is gif.'
)
@option(
    '--dest',
    '-d',
    type=Path(),
    help='Path of output file. Default is `./<illust-id>.<format>`.',
)
@argument('illust-id', type=int)
def ugoira(
    speed: float,
    format: str,
    dest: Optional[str],
    illust_id: int,
):
    """ugoira command for download Pixiv Ugokuillust."""

    if is_ugoira(illust_id):
        try:
            blob, frames = download_ugoira_zip(illust_id)
        except PixivError as e:
            echo('Error: {}'.format(e), err=True)
            raise SystemExit(1)

        if dest is None:
            dest = '{}.{}'.format(illust_id, format)

        save(format, dest, blob, frames, speed)
        echo(
            'Download was completed successfully.'
            ' format is {} and output path is {}'.format(
                format,
                dest,
            )
        )
    else:
        echo('Given illust-id is not ugoira.', err=True)
        raise SystemExit(1)

@@ -1522,73 +1522,209 @@
     
-help='Path of output file. Default is %60./%3Cillust-id%3E.%3Cformat%3E%60
+default='%7B%7D.%7B%7D',%0A    help='A format string specifying the path of downloaded files.'%0A         ' The illust ID and your chosen format will replace %7B%7Ds in it, if any.'%0A         ' Default is %5C'./%7B%7D.%7B%7D%5C'
 .'
-,
 %0A)%0A@
@@ -1742,16 +1742,17 @@
 llust-id
+s
 ', type=
@@ -1754,16 +1754,26 @@
 type=int
+, nargs=-1
 )%0Adef ug
@@ -1855,13 +1855,16 @@
 t_id
+s
 : 
-int
+tuple
 ,%0A):
@@ -1921,17 +1921,144 @@
 ust.%22%22%22%0A
-%0A
+    for i, illust_id in enumerate(illust_ids):%0A        echo(%22Downloading %7B%7D (%7B%7D/%7B%7D)%22.format(illust_id, i, len(illust_ids)))%0A    
     if i
@@ -2090,13 +2090,21 @@
     
+    
 try:%0A
+    
     
@@ -2165,16 +2165,20 @@
         
+    
 except P
@@ -2193,16 +2193,20 @@
 r as e:%0A
+    
         
@@ -2263,80 +2263,49 @@
     
-raise SystemExit(1)%0A%0A        if dest is None:%0A            dest = '%7B%7D.%7B%7D'
+    continue%0A%0A            filename = dest
 .for
@@ -2336,16 +2336,20 @@
         
+    
 save(for
@@ -2353,20 +2353,24 @@
 format, 
-dest
+filename
 , blob, 
@@ -2388,24 +2388,28 @@
 ed)%0A        
+    
 echo(%0A      
@@ -2414,16 +2414,20 @@
         
+    
 'Downloa
@@ -2453,24 +2453,28 @@
 cessfully.'%0A
+    
             
@@ -2531,24 +2531,28 @@
             
+    
 format,%0A    
@@ -2567,14 +2567,26 @@
     
-dest,%0A
+    filename,%0A    
     
@@ -2591,24 +2591,28 @@
           )%0A
+    
         )%0A  
@@ -2605,24 +2605,28 @@
           )%0A
+    
     else:%0A  
@@ -2635,29 +2635,30 @@
     
+    
 echo('
-Given illust-id
+Illust ID %7B%7D
  is 
@@ -2673,44 +2673,34 @@
 ra.'
-, err=True)%0A        raise SystemExit(1
+.format(illust_id), err=True
 )%0A

Allow downloading multiple files at once