Skip to content

Commit c86cc95

Browse files
committed
refactor: consolidate force logic and improve error handling based on review
1 parent f57f3b8 commit c86cc95

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/mcpm/commands/install.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ def prompt_with_default(prompt_text, default="", hide_input=False, required=Fals
9494
return default
9595
if required:
9696
# Cannot fulfill required argument without default in non-interactive mode
97-
raise click.Abort()
97+
raise click.UsageError(
98+
"A required value has no default and cannot be prompted in non-interactive mode."
99+
)
98100
return ""
99101

100102
# if default:
@@ -174,7 +176,7 @@ def install(server_name, force=False, alias=None):
174176

175177
# Confirm addition
176178
alias_text = f" as '{alias}'" if alias else ""
177-
if not force and not should_force_operation() and not Confirm.ask(f"Install this server to global configuration{alias_text}?"):
179+
if not should_force_operation(force) and not Confirm.ask(f"Install this server to global configuration{alias_text}?"):
178180
console.print("[yellow]Operation cancelled.[/]")
179181
return
180182

@@ -219,7 +221,7 @@ def install(server_name, force=False, alias=None):
219221
selected_method = installations[method_id]
220222

221223
# If multiple methods are available and not forced, offer selection
222-
if len(installations) > 1 and not force and not should_force_operation():
224+
if len(installations) > 1 and not should_force_operation(force):
223225
console.print("\n[bold]Available installation methods:[/]")
224226
methods_list = []
225227

@@ -439,7 +441,7 @@ def install(server_name, force=False, alias=None):
439441
)
440442

441443
# Add server to global configuration
442-
success = global_add_server(full_server_config.to_server_config(), force or should_force_operation())
444+
success = global_add_server(full_server_config.to_server_config(), should_force_operation(force))
443445

444446
if success:
445447
# Server has been successfully added to the global configuration

src/mcpm/commands/uninstall.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def uninstall(server_name, force):
6060
print_server_config(server_info)
6161

6262
# Get confirmation if --force is not used
63-
if not force and not should_force_operation():
63+
if not should_force_operation(force):
6464
console.print(f"\n[bold yellow]Are you sure you want to remove:[/] {server_name}")
6565
console.print("[italic]To bypass this confirmation, use --force[/]")
6666
# Use Rich's Confirm for a better user experience

src/mcpm/utils/non_interactive.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ def is_non_interactive() -> bool:
3232
return False
3333

3434

35-
def should_force_operation() -> bool:
35+
def should_force_operation(cli_force_flag: bool = False) -> bool:
3636
"""
3737
Check if operations should be forced (skip confirmations).
3838
39-
Returns True if MCPM_FORCE environment variable is set to 'true'.
39+
Args:
40+
cli_force_flag: Boolean flag from CLI args (e.g. --force)
41+
42+
Returns:
43+
True if cli_force_flag is True OR MCPM_FORCE environment variable is set to 'true'.
4044
"""
41-
return os.getenv("MCPM_FORCE", "").lower() == "true"
45+
return cli_force_flag or os.getenv("MCPM_FORCE", "").lower() == "true"
4246

4347

4448
def should_output_json() -> bool:

0 commit comments

Comments
 (0)