From 380926f9c4db3db19010475c5cbf0898ec560f2f Mon Sep 17 00:00:00 2001 From: Keyan Pishdadian Date: Sun, 21 Jun 2015 21:49:45 -0400 Subject: [PATCH] Add --default-no flag, adjust README --- README.md | 2 ++ src/codemod.py | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6c79a0d..45d0bb8 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ Options (all optional) include: matching file extensions passed in --extensions --accept-all Automatically accept all changes (use with caution) + --default-no + Set default behavior to reject the change. --editor Specify an editor, e.g. "vim" or "emacs". If omitted, defaults to $EDITOR environment variable. diff --git a/src/codemod.py b/src/codemod.py index d1bad29..14c0c7a 100755 --- a/src/codemod.py +++ b/src/codemod.py @@ -92,7 +92,7 @@ def the_filter(path): extensions=['php', 'phpt', 'js', 'css', 'rb', 'erb'] ) -def run_interactive(query, editor=None, just_count=False): +def run_interactive(query, editor=None, just_count=False, default_no=False): """ Asks the user about each patch suggested by the result of the query. @@ -128,7 +128,7 @@ def run_interactive(query, editor=None, just_count=False): for patch in suggestions: _save_bookmark(patch.start_position) - _ask_about_patch(patch, editor) + _ask_about_patch(patch, editor, default_no) print 'Searching...' _delete_bookmark() if yes_to_all: @@ -567,8 +567,9 @@ def print_file_line(line_number): print_file_line(i) yes_to_all = False -def _ask_about_patch(patch, editor): +def _ask_about_patch(patch, editor, default_no): global yes_to_all + default_action = 'n' if default_no else 'y' terminal_clear() terminal_print('%s\n' % patch.render_range(), color='WHITE') print @@ -580,9 +581,13 @@ def _ask_about_patch(patch, editor): if patch.new_lines is not None: if not yes_to_all: - print 'Accept change (y = yes [default], n = no, e = edit, \ -A = yes to all, E = yes+edit)? ', - p = _prompt('yneEA', default='y') + if default_no: + print ('Accept change (y = yes, n = no [default], e = edit, ' + + 'A = yes to all, E = yes+edit)? '), + else: + print ('Accept change (y = yes [default], n = no, e = edit, ' + + 'A = yes to all, E = yes+edit)? '), + p = _prompt('yneEA', default=default_action) else: p = 'y' else: @@ -810,6 +815,9 @@ def _parse_command_line(): parser.add_argument('--accept-all', action='store_true', help='Automatically accept all changes (use with caution).') + parser.add_argument('--default-no', action='store_true', + help='If set, this will make the default option to not accept the change.') + parser.add_argument('--editor', action='store', type=str, help='Specify an editor, e.g. "vim" or emacs". ' 'If omitted, defaults to $EDITOR environment variable.') @@ -857,6 +865,7 @@ def _parse_command_line(): if arguments.editor is not None: options['editor'] = arguments.editor options['just_count'] = arguments.count + options['default_no'] = arguments.default_no return options