Skip to content
This repository has been archived by the owner on Jun 10, 2021. It is now read-only.

Commit

Permalink
Merge pull request #57 from keyan/kp_add_default_no
Browse files Browse the repository at this point in the history
Add --default-no flag
  • Loading branch information
asm89 committed Sep 3, 2015
2 parents 86a6670 + 380926f commit 560e394
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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.
Expand Down
21 changes: 15 additions & 6 deletions src/codemod.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,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.
Expand Down Expand Up @@ -125,7 +125,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:
Expand Down Expand Up @@ -564,8 +564,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
Expand All @@ -577,9 +578,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:
Expand Down Expand Up @@ -807,6 +812,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.')
Expand Down Expand Up @@ -854,6 +862,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

Expand Down

0 comments on commit 560e394

Please sign in to comment.