Skip to content

Commit

Permalink
Merge pull request #6 from StephanLoor/add-flag-all-files-to-basic-co…
Browse files Browse the repository at this point in the history
…mmands

Add flag `--all` to all basic commands
  • Loading branch information
MarkusSagen authored Jul 23, 2024
2 parents add0d6d + e3142cf commit 3d38e1d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 39 deletions.
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,22 @@ Your Python Typer application, rejx, provides a command line interface (CLI) for

### `fix`

Purpose: Applies the changes from a specified .rej file to its corresponding original file.
Purpose: Applies the changes from one or more specified .rej file to their corresponding original file.
Usage:

`rejx fix path/to/file.rej`
`rejx fix path/to/file1.rej path/to/file2.rej ...`

### `fix_all`

Purpose: Searches for all .rej files in the current directory and subdirectories, then applies their changes to the corresponding original files.
Usage:

`rejx fix_all`
Passing the optional flag `--all` applies the changes from all .rej files to their corresponding original files. Usage:
`rejx fix --all`

### `diff`

Purpose: Displays the differences between the current files and the changes proposed in the .rej files.
Purpose: Displays the differences between the current file(s) and the changes proposed in the corresponding .rej file(s).
Usage:

`rejx diff`
`rejx diff <filename1> <filename2> ...`

If no file name is passed, this displays the difference for all .rej files.

Note: This command uses a pager for output. Use arrow keys or Vim bindings to navigate, and q to quit.

Expand All @@ -67,13 +65,21 @@ For list view (default):

### `clean`

Purpose: Deletes all .rej files in the current directory and subdirectories. It has an optional preview feature.
Purpose: Deletes specified .rej files. It has an optional preview feature.
Usage:

`rejx clean`
`rejx clean path/to/file1.rej path/to/file2.rej ...`

With preview:

`rejx clean --preview`
`rejx clean path/to/file1.rej path/to/file2.rej ... --preview`

By passing the optional `--all` flag, this command deletes all the .rej files in the current directory and subdirectories.
Usage:
`rejx clean --all`

This can be combined with the `--preview` option.
Usage:
`rejx clean --all --preview`

______________________________________________________________________
2 changes: 0 additions & 2 deletions src/rejx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
diff,
find_rej_files,
fix,
fix_all,
ls,
parse_rej_file,
process_rej_file,
Expand All @@ -19,7 +18,6 @@
"diff",
"find_rej_files",
"fix",
"fix_all",
"ls",
"parse_rej_file",
"process_rej_file",
Expand Down
65 changes: 44 additions & 21 deletions src/rejx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,53 +189,63 @@ def process_rej_file(rej_file_path: str) -> bool:


@app.command()
def fix(rej_file_path: str) -> None:
def fix(
rej_files: list[str] = typer.Argument(default=None), # noqa: B008
apply_to_all_files: Optional[bool] = typer.Option(False, "--all", help="Apply changes from all .rej files.", show_default=False),
) -> None:
"""Applies changes from a specified .rej file to its corresponding original file.
Args:
----
rej_file_path (str): The path to the .rej file to be processed.
rej_file (str): The path to the .rej file to be processed.
apply_to_all_files (Optional[bool]): Determines whether all files should be fixed. Default: False.
Example:
-------
To fix a specific .rej file, run:
```bash
rejx fix path/to/file.rej
```
"""
process_rej_file(rej_file_path)


@app.command()
def fix_all() -> None:
"""Searches for and applies changes from all .rej files in the current directory and subdirectories.
Example:
-------
To fix all .rej files found in the current and subdirectories, run:
To fix all files, run:
```bash
rejx fix_all
rejx fix --all
```
"""
for rej_file in find_rej_files():
process_rej_file(rej_file)
if apply_to_all_files:
for rej_file in find_rej_files():
process_rej_file(rej_file)

elif rej_files is None:
logging.error("No file name specified")

else:
for rej_file in rej_files:
process_rej_file(rej_file)


@app.command()
def diff() -> None:
def diff(
rej_files: list[str] = typer.Argument(default=None), # noqa: B008
) -> None:
"""Displays the diff of changes proposed by .rej files against their corresponding original files.
Displays the diff for all .rej files If no file names are specified.
Example:
-------
To display diffs for all .rej files, run:
```bash
rejx diff
```
"""
if rej_files is None:
rej_files = find_rej_files()

console = Console()
file_logs = []

for rej_file in find_rej_files():
for rej_file in rej_files:
try:
target_file_path = rej_file.replace(".rej", "")
rej_lines = parse_rej_file(rej_file)
Expand Down Expand Up @@ -377,31 +387,44 @@ def ls(

@app.command()
def clean(
rej_files: list[str] = typer.Argument(default=None), # noqa: B008
apply_to_all_files: Optional[bool] = typer.Option(False, "--all", help="Apply changes from all .rej files.", show_default=False),
preview: bool = typer.Option(
False,
help="Preview files before deleting",
),
) -> None:
"""Deletes all .rej files in the current directory and subdirectories.
"""Deletes one or all .rej files in the current directory and subdirectories.
Optional preview before deletion.
Args:
----
rej_files (Optional, List[str]): a list of names of files to be deleted.
apply_to_all_files (Optional, bool): determines if all files should be removed. Defaults to "False".
preview (bool): If True, previews the files before deleting. Defaults to False.
Example:
-------
- To delete a file file.txt.rej without preview, run:
```bash
rejx clean file.txt.rej
```
- To delete all .rej files without preview, run:
```bash
rejx clean
rejx clean --all
```
- To preview files before deletion, run:
```bash
rejx clean --preview
rejx clean --all --preview
```
"""
rej_files = find_rej_files()
if apply_to_all_files:
rej_files = find_rej_files()

elif rej_files is None:
logging.error("No filename specified.")

console = Console()

if not rej_files:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_rejx.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_fix(sample_rej_file: str):


def test_fix_all():
result = runner.invoke(app, ["fix-all"])
result = runner.invoke(app, ["fix", "--all"])
assert result.exit_code == 0


Expand All @@ -106,12 +106,12 @@ def test_ls_tree():


def test_clean():
result = runner.invoke(app, ["clean"])
result = runner.invoke(app, ["clean","--all"])
assert result.exit_code == 0


def test_clean_with_preview():
result = runner.invoke(app, ["clean", "--preview"], input="y\n")
result = runner.invoke(app, ["clean","all", "--preview"], input="y\n")
assert result.exit_code == 0


Expand Down

0 comments on commit 3d38e1d

Please sign in to comment.