Skip to content

Commit 9fc6ba5

Browse files
authored
Merge pull request #16 from dyka3773/develop
feat: Add essential CLI enhancements (--version and --quiet flags)
2 parents 5aac22a + 4c5ba37 commit 9fc6ba5

File tree

6 files changed

+485
-54
lines changed

6 files changed

+485
-54
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ mvn-tree-visualizer --filename "maven_dependency_file" --output "dependencies.js
9292
mvn-tree-visualizer --filename "maven_dependency_file" --output "diagram.html" --watch
9393
```
9494

95+
#### Quiet Mode (For Automation/Scripts)
96+
```bash
97+
# Only show errors, suppress success messages
98+
mvn-tree-visualizer --filename "maven_dependency_file" --output "diagram.html" --quiet
99+
100+
# Short form also available
101+
mvn-tree-visualizer --filename "maven_dependency_file" --output "diagram.html" -q
102+
```
103+
95104
> **💡 Tip:** In watch mode, the tool will monitor for changes to your Maven dependency files and automatically regenerate the diagram. Perfect for development workflows! Press `Ctrl+C` to stop watching.
96105
97106
### Step 3: View the output
@@ -123,6 +132,8 @@ Each example includes:
123132
| `--watch` | Watch for file changes and auto-regenerate diagram | `False` |
124133
| `--directory` | The directory to scan for the Maven dependency file(s) | current directory |
125134
| `--keep-tree` | Keep the intermediate `dependency_tree.txt` file | `False` |
135+
| `--quiet`, `-q` | Suppress all console output except errors | `False` |
136+
| `--version`, `-v` | Show the current version and exit | - |
126137
| `--help` | Show the help message and exit | - |
127138

128139
### Theme Options

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ dev = [
4848
"build>=1.0.0",
4949
"git-changelog>=2.5.3",
5050
"tox>=4.28.4",
51+
"pytest>=8.4.1",
5152
]
5253

5354
[build-system]

src/mvn_tree_visualizer/cli.py

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import argparse
2+
import sys
23
import time
4+
import traceback
5+
from importlib import metadata
36
from pathlib import Path
47
from typing import NoReturn
58

@@ -12,6 +15,14 @@
1215
from .validation import find_dependency_files, validate_dependency_files, validate_output_directory
1316

1417

18+
def get_version() -> str:
19+
"""Get the current version of the package."""
20+
try:
21+
return metadata.version("mvn-tree-visualizer")
22+
except metadata.PackageNotFoundError:
23+
return "unknown"
24+
25+
1526
def generate_diagram(
1627
directory: str,
1728
output_file: str,
@@ -20,6 +31,7 @@ def generate_diagram(
2031
output_format: str,
2132
show_versions: bool,
2233
theme: str = "minimal",
34+
quiet: bool = False,
2335
) -> None:
2436
"""Generate the dependency diagram with comprehensive error handling."""
2537
timestamp = time.strftime("%H:%M:%S")
@@ -31,7 +43,7 @@ def generate_diagram(
3143

3244
# Show what files we found
3345
dependency_files = find_dependency_files(directory, filename)
34-
if len(dependency_files) > 1:
46+
if len(dependency_files) > 1 and not quiet:
3547
print(f"[{timestamp}] Found {len(dependency_files)} dependency files")
3648

3749
# Setup paths
@@ -103,31 +115,42 @@ def generate_diagram(
103115
except Exception as e:
104116
raise OutputGenerationError(f"Error generating {output_format.upper()} output: {e}")
105117

106-
print(f"[{timestamp}] ✅ Diagram generated and saved to {output_file}")
118+
if not quiet:
119+
print(f"[{timestamp}] SUCCESS: Diagram generated and saved to {output_file}")
107120

108121
except MvnTreeVisualizerError as e:
109122
# Our custom errors already have helpful messages
110-
print(f"[{timestamp}] ❌ Error: {e}")
123+
print(f"[{timestamp}] ERROR: {e}", file=sys.stderr)
124+
raise # Re-raise the exception for the caller to handle
111125
except KeyboardInterrupt:
112-
print(f"\n[{timestamp}] ⏹️ Operation cancelled by user")
126+
print(f"\n[{timestamp}] Operation cancelled by user", file=sys.stderr)
127+
raise # Re-raise for the caller to handle
113128
except Exception as e:
114129
# Unexpected errors
115-
print(f"[{timestamp}] ❌ Unexpected error: {e}")
116-
print("This is an internal error. Please report this issue with the following details:")
117-
print(f" - Directory: {directory}")
118-
print(f" - Filename: {filename}")
119-
print(f" - Output: {output_file}")
120-
print(f" - Format: {output_format}")
121-
import traceback
130+
print(f"[{timestamp}] UNEXPECTED ERROR: {e}", file=sys.stderr)
131+
print("This is an internal error. Please report this issue with the following details:", file=sys.stderr)
132+
print(f" - Directory: {directory}", file=sys.stderr)
133+
print(f" - Filename: {filename}", file=sys.stderr)
134+
print(f" - Output: {output_file}", file=sys.stderr)
135+
print(f" - Format: {output_format}", file=sys.stderr)
122136

123137
traceback.print_exc()
138+
raise # Re-raise for the caller to handle
124139

125140

126141
def cli() -> NoReturn:
127142
parser = argparse.ArgumentParser(
128143
prog="mvn-tree-visualizer",
129144
description="Generate a dependency diagram from a file.",
130145
)
146+
147+
parser.add_argument(
148+
"-v",
149+
"--version",
150+
action="version",
151+
version=f"mvn-tree-visualizer {get_version()}",
152+
)
153+
131154
parser.add_argument(
132155
"directory",
133156
type=str,
@@ -186,6 +209,13 @@ def cli() -> NoReturn:
186209
help="Theme for the diagram visualization. Default is 'minimal'.",
187210
)
188211

212+
parser.add_argument(
213+
"-q",
214+
"--quiet",
215+
action="store_true",
216+
help="Suppress all console output except errors. Perfect for CI/CD pipelines and scripted usage.",
217+
)
218+
189219
args = parser.parse_args()
190220
directory: str = args.directory
191221
output_file: str = args.output
@@ -195,28 +225,46 @@ def cli() -> NoReturn:
195225
show_versions: bool = args.show_versions
196226
watch_mode: bool = args.watch
197227
theme: str = args.theme
228+
quiet: bool = args.quiet
198229

199230
# Generate initial diagram
200-
print("Generating initial diagram...")
201-
generate_diagram(directory, output_file, filename, keep_tree, output_format, show_versions, theme)
231+
if not quiet:
232+
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
233+
print(f"[{timestamp}] Generating initial diagram...")
234+
235+
try:
236+
generate_diagram(directory, output_file, filename, keep_tree, output_format, show_versions, theme, quiet)
237+
except MvnTreeVisualizerError:
238+
sys.exit(1)
239+
except KeyboardInterrupt:
240+
sys.exit(130) # Standard exit code for SIGINT
241+
except Exception:
242+
sys.exit(1)
202243

203244
if not watch_mode:
204-
print("You can open it in your browser to view the dependency tree.")
205-
print("Thank you for using mvn-tree-visualizer!")
245+
if not quiet:
246+
print("You can open it in your browser to view the dependency tree.")
247+
print("Thank you for using mvn-tree-visualizer!")
206248
return
207249

208250
# Watch mode
209251
def regenerate_callback():
210252
"""Callback function for file watcher."""
211-
generate_diagram(directory, output_file, filename, keep_tree, output_format, show_versions, theme)
253+
try:
254+
generate_diagram(directory, output_file, filename, keep_tree, output_format, show_versions, theme, quiet)
255+
except Exception:
256+
# In watch mode, we don't want to exit on errors, just log them
257+
print("Error during diagram regeneration:", file=sys.stderr)
258+
traceback.print_exc()
212259

213260
watcher = FileWatcher(directory, filename, regenerate_callback)
214261
watcher.start()
215262

216263
try:
217264
watcher.wait()
218265
finally:
219-
print("Thank you for using mvn-tree-visualizer!")
266+
if not quiet:
267+
print("Thank you for using mvn-tree-visualizer!")
220268

221269

222270
if __name__ == "__main__":

0 commit comments

Comments
 (0)