Repository: Tobi208/pypdf-cli (language: python)

Issue:
Add -a --all flag to merge all files in the current directory



Discussion / hints:

Fix patch:
diff --git a/src/pypdf_cli.py b/src/pypdf_cli.py
index 7103dae..3d51d20 100644
--- a/src/pypdf_cli.py
+++ b/src/pypdf_cli.py
@@ -1,4 +1,4 @@
-from os import makedirs
+from os import makedirs, listdir, getcwd
 from os.path import basename, dirname
 
 import click
@@ -250,16 +250,23 @@ def insert(input_files, output, select_pages):
 
 
 @click.command()
-@click.argument('input-files', nargs=-1, type=click.Path(exists=True))
+@click.argument('input-files', nargs=-1, type=click.Path(exists=True), required=False)
 @click.option('--output', '-o', type=click.Path(), help=OUTPUT_HELP)
-def merge(input_files, output):
+@click.option('--all', '-a', is_flag=True, default=False, help=ALL_HELP)
+def merge(input_files, output, all):
     """
     Merge two or more pdf files.
     Files are appended in the order they are entered.
 
     INPUT_FILES are the locations of at least two pdf files to be merged.
+    Merge all files in the current directory if no input is given.
     """
 
+    # if no input files are specified or 'all' flag is set,
+    # take all pdf files in current directory
+    if len(input_files) == 0 or all:
+        input_files = [f for f in listdir(getcwd()) if f.endswith('.pdf')]
+
     # specific verification
     if len(input_files) < 2:
         raise click.BadParameter(message='Cannot merge less than two files.')


Test patch:
diff --git a/tests/test_pypdf-cli.py b/tests/test_pypdf-cli.py
index 9b49eaf..bec6c96 100644
--- a/tests/test_pypdf-cli.py
+++ b/tests/test_pypdf-cli.py
@@ -316,6 +316,11 @@ def test_merge():
     result = RUNNER.invoke(merge, [TEST_FILE, '-o', TEST_OUT])
     assert result.exit_code == 2
 
+    # I'm sorry but testing merge without input files / merge --all for merging 
+    # all files in a current directory is a nightmare because testing with
+    # click doesn't let me implement a proper teardown to delete generated files
+    # making test cases for merge all manual anyway. Too much of a headache.
+
 
 def test_split():
     RUNNER.invoke(split, [TEST_FILE, '-o', TEST_OUT, '-p', '3'])