Related Code Files:
interactive_utils.py- The new shared utility moduletext_undo.py- Example of migrated tool- All Python tools that use
input()or interactive prompts
We've created a common solution for handling non-interactive environments across all tools. This prevents ugly EOF errors and provides clear, actionable error messages.
Add to your imports:
try:
from interactive_utils import (
safe_input, get_confirmation, check_auto_yes_env,
get_tool_env_var, require_interactive, Colors
)
except ImportError:
# Fallback for older installations
print("Warning: interactive_utils module not found. Using basic input handling.", file=sys.stderr)
safe_input = input
# ... provide basic fallbacksBefore:
response = input("Enter value: ")
confirm = input("Proceed? [y/N]: ")After:
response = safe_input(
"Enter value: ",
tool_name="my_tool",
env_var="MY_TOOL_ASSUME_YES"
)
confirmed = get_confirmation(
"Proceed?",
default=False,
tool_name="my_tool",
env_var="MY_TOOL_ASSUME_YES"
)In your argument parsing:
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--yes', '-y', action='store_true',
help='Skip confirmations')
args = parser.parse_args()
# Check environment variable
check_auto_yes_env('my_tool', args)For operations that cannot work in non-interactive mode:
# For selecting from a list
require_interactive("interactive file selection", tool_name="my_tool")
choice = input("Select file (1-10): ") # Safe to use input() after require_interactiveif not get_confirmation("Delete file?", tool_name="my_tool"):
print("Operation cancelled")
returnif not get_confirmation(
"This will delete all data!",
typed_confirmation="DELETE ALL",
tool_name="my_tool"
):
returnname = safe_input(
"Enter name [default]: ",
default="default",
tool_name="my_tool"
)Each tool should support:
{TOOL_NAME}_ASSUME_YES=1- Auto-confirm all prompts- Standard CI variables are auto-detected
High Priority (frequently hit EOF errors):
- ✅
text_undo.py- DONE - ❌
safe_file_manager.py- Already has good support, needs integration - ❌
replace_text_v9.py- Confirmation prompts - ❌
replace_text_ast_v3.py- Confirmation prompts - ❌
unified_refactor_v2.py- Confirmation prompts - ❌
refactor_rename_v2.py- Confirmation prompts
Medium Priority:
- All tools with
--confirm-eachoptions - Tools that show diffs and ask for confirmation
- Any tool using
getpassfor sensitive input
Test your migrated tool:
# Should show clear error
echo "test" | python my_tool.py
# Should work
echo "test" | python my_tool.py --yes
# Should work
MY_TOOL_ASSUME_YES=1 python my_tool.py- Consistent UX - All tools behave the same way
- CI/CD Ready - Works in all automation environments
- Clear Errors - Users know exactly what to do
- Backward Compatible - Fallbacks for older installations