Skip to content

Commit

Permalink
Add --check cmd line option (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
f18m authored and lovesegfault committed Jan 13, 2019
1 parent a694e3e commit 21a02fa
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions beautysh/beautysh.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self):
self.tab_str = ' '
self.tab_size = 4
self.backup = False
self.check_only = False

def read_file(self, fp):
"""Read input file."""
Expand Down Expand Up @@ -180,9 +181,15 @@ def beautify_file(self, path):
data = self.read_file(path)
result, error = self.beautify_string(data, path)
if(data != result):
if(self.backup):
self.write_file(path+'.bak', data)
self.write_file(path, result)
if(self.check_only):
if not error:
# we want to return 0 (success) only if the given file is already
# well formatted:
error = (result != data)
else:
if(self.backup):
self.write_file(path+'.bak', data)
self.write_file(path, result)
return error

def main(self):
Expand All @@ -198,6 +205,9 @@ def main(self):
parser.add_argument('--backup', '-b', action='store_true',
help="Beautysh will create a backup file in the "
"same path as the original.")
parser.add_argument('--check', '-c', action='store_true',
help="Beautysh will just check the files without doing "
"any in-place beautify.")
parser.add_argument('--tab', '-t', action='store_true',
help="Sets indentation to tabs instead of spaces")
args = parser.parse_args()
Expand All @@ -208,6 +218,7 @@ def main(self):
args.indent_size = args.indent_size[0]
self.tab_size = args.indent_size
self.backup = args.backup
self.check_only = args.check
if (args.tab):
self.tab_size = 1
self.tab_str = '\t'
Expand Down

0 comments on commit 21a02fa

Please sign in to comment.