Skip to content

Commit

Permalink
feat. Added the -B|--backupsuffix command line option.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamere-allo-peter committed Mar 21, 2022
1 parent 53645da commit 1526f20
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ This software automatically fixes some errors and warnings reported by
`yamllint`.

```shell
usage: yamlfixer [-h] [-v] [-b] [-d] [-j | -p | -s] [file [file ...]]
usage: yamlfixer [-h] [-v] [-b] [-B BACKUPSUFFIX] [-d] [-j | -p | -s] [file [file ...]]

Fix formatting problems in YAML documents. If no file is specified,
then reads input from `stdin`.
Expand All @@ -56,6 +56,10 @@ optional arguments:
-h, --help show this help message and exit
-v, --version display this program's version number and exit.
-b, --backup make a backup copy of original files as `.orig`
-b, --backup make a backup copy of original files.
-B BACKUPSUFFIX, --backupsuffix BACKUPSUFFIX
sets the suffix for backup files, `.orig` is
the default.
-d, --debug output debug information to stderr.
-l, --listfixes output the list of available fixes.
-j, --jsonsummary output JSON summary to stderr.
Expand All @@ -79,7 +83,8 @@ command line option is used.

Both summaries and diagnostic information are sent to stderr.

This command exits with `-2` if yamllint is not available on your
This command exits with `-3` if there are incompatible command line
options. It exits with `-2` if yamllint is not available on your
system. Otherwise it exits with `0` if all input files either are
skipped or successfully pass `yamllint` strict mode, else `-1`.

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[metadata]
version = 0.4.0
version = 0.4.1
name = yamlfixer-opt-nc
description = automates the fixing of problems reported by yamllint
long_description = file: README.md
Expand Down
2 changes: 1 addition & 1 deletion yamlfixer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import time

__version__ = "0.4.0"
__version__ = "0.4.1"
__author__ = "OPT-NC"
__license__ = "GPLv3+"
__copyright__ = "Copyright (C) 2021-%s %s" % (time.strftime("%Y",
Expand Down
12 changes: 10 additions & 2 deletions yamlfixer/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# -*- coding: utf-8 -*-
#
# This program is free software: you can redistribute it and/or modify
Expand All @@ -20,6 +21,7 @@
import argparse

from . import __version__, __copyright__
from .constants import EXIT_CMDLINEERROR
from .yamlfixer import YAMLFixer

GPLBLURB = """
Expand All @@ -45,7 +47,7 @@ def run():

# Parse the command line arguments
cmdline = argparse.ArgumentParser(description="Fix formatting problems in YAML documents. "
"If no file is specified,\nthen reads input from `stdin`.",
"If no file is specified, then reads input from `stdin`.",
epilog=f"{__copyright__}\n{GPLBLURB}",
formatter_class=argparse.RawDescriptionHelpFormatter)
cmdline.add_argument("-v", "--version",
Expand All @@ -54,7 +56,10 @@ def run():
help="display this program's version number and exit.")
cmdline.add_argument("-b", "--backup",
action="store_true",
help="make a backup copy of original files as `.orig`")
help="make a backup copy of original files.")
cmdline.add_argument("-B", "--backupsuffix",
default=".orig",
help="sets the suffix for backup files, `%(default)s` is the default.")
cmdline.add_argument("-d", "--debug",
action="store_true",
help="output debug information to stderr.")
Expand All @@ -80,6 +85,9 @@ def run():
if cmdline.prog == "__main__.py":
cmdline.prog = "yamlfixer"
arguments = cmdline.parse_args()
if arguments.backupsuffix and not arguments.backup:
sys.stderr.write("ERROR: Option -B|--backupsuffix requires -b|--backup\n")
return EXIT_CMDLINEERROR
if arguments.listfixes:
return YAMLFixer(arguments).listfixes()
return YAMLFixer(arguments).fix()
Expand Down
2 changes: 2 additions & 0 deletions yamlfixer/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# -*- coding: utf-8 -*-
#
# This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -27,3 +28,4 @@
EXIT_OK = 0
EXIT_NOK = -1
EXIT_PROBLEM = -2
EXIT_CMDLINEERROR = -3
3 changes: 2 additions & 1 deletion yamlfixer/filefixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def dump(self, outcontents):
if self.yfixer.arguments.backup: # pylint: disable=no-member
# Try to make a backup of the original file
try:
os.replace(self.filename, f"{self.filename}.orig")
os.replace(self.filename,
f"{self.filename}{self.yfixer.arguments.backupsuffix}")
except PermissionError as msg:
self.yfixer.error(f"impossible to create a backup : {msg}")
# Overwrite the original file with the new contents
Expand Down

0 comments on commit 1526f20

Please sign in to comment.