diff --git a/language_formatters_pre_commit_hooks/pretty_format_toml.py b/language_formatters_pre_commit_hooks/pretty_format_toml.py index 6b41911..17f0309 100644 --- a/language_formatters_pre_commit_hooks/pretty_format_toml.py +++ b/language_formatters_pre_commit_hooks/pretty_format_toml.py @@ -20,8 +20,25 @@ def pretty_format_toml(argv: typing.Optional[typing.List[str]] = None) -> int: dest="autofix", help="Automatically fixes encountered not-pretty-formatted files", ) - + parser.add_argument( + "--indent", + type=int, + default="2", + help="The number of spaces to be used as delimiter for indentation level (Default: %(default)s)", + ) parser.add_argument("filenames", nargs="*", help="Filenames to fix") + parser.add_argument( + "--trailing-commas", + action="store_true", + dest="trailing_commas", + help="Add trailing commas to inline arrays", + ) + parser.add_argument( + "--no-sort", + action="store_true", + dest="no_sort", + help="Don't sort keys", + ) args = parser.parse_args(argv) status = 0 @@ -39,11 +56,11 @@ def pretty_format_toml(argv: typing.Optional[typing.List[str]] = None) -> int: inline=True, block=True, ), - sort_config=SortConfiguration(tables=True), + sort_config=SortConfiguration(tables=not args.no_sort, table_keys=not args.no_sort), format_config=FormattingConfiguration( spaces_before_inline_comment=2, - spaces_indent_inline_array=2, - trailing_comma_inline_array=False, + spaces_indent_inline_array=args.indent, + trailing_comma_inline_array=args.trailing_commas, ), ).sorted() diff --git a/test-data/pretty_format_toml/indent2-pretty-formatted.toml b/test-data/pretty_format_toml/indent2-pretty-formatted.toml new file mode 100644 index 0000000..6963c82 --- /dev/null +++ b/test-data/pretty_format_toml/indent2-pretty-formatted.toml @@ -0,0 +1,4 @@ +dependencies = [ + "numpy", + "scikit-learn" +] diff --git a/test-data/pretty_format_toml/indent4-pretty-formatted.toml b/test-data/pretty_format_toml/indent4-pretty-formatted.toml new file mode 100644 index 0000000..dde01fe --- /dev/null +++ b/test-data/pretty_format_toml/indent4-pretty-formatted.toml @@ -0,0 +1,4 @@ +dependencies = [ + "numpy", + "scikit-learn" +] diff --git a/test-data/pretty_format_toml/no-sort-pretty-formatted.toml b/test-data/pretty_format_toml/no-sort-pretty-formatted.toml new file mode 100644 index 0000000..f203575 --- /dev/null +++ b/test-data/pretty_format_toml/no-sort-pretty-formatted.toml @@ -0,0 +1,4 @@ +[project] +name = "example" +version = "1.0.0" +description = "Example" diff --git a/test-data/pretty_format_toml/sorted-pretty-formatted.toml b/test-data/pretty_format_toml/sorted-pretty-formatted.toml new file mode 100644 index 0000000..79efec4 --- /dev/null +++ b/test-data/pretty_format_toml/sorted-pretty-formatted.toml @@ -0,0 +1,4 @@ +[project] +description = "Example" +name = "example" +version = "1.0.0" diff --git a/tests/pretty_format_toml_test.py b/tests/pretty_format_toml_test.py index a20ef8f..e5f8082 100644 --- a/tests/pretty_format_toml_test.py +++ b/tests/pretty_format_toml_test.py @@ -30,6 +30,21 @@ def test_pretty_format_toml(filename, expected_retval): assert pretty_format_toml([filename]) == expected_retval +@pytest.mark.parametrize( + ("filename", "args", "expected_retval"), + ( + ("indent2-pretty-formatted.toml", [], 0), + ("indent2-pretty-formatted.toml", ["--indent=4"], 1), + ("indent4-pretty-formatted.toml", [], 1), + ("indent4-pretty-formatted.toml", ["--indent=4"], 0), + ("no-sort-pretty-formatted.toml", ["--no-sort"], 0), + ("no-sort-pretty-formatted.toml", [], 1), + ), +) +def test_pretty_format_toml_custom_cli_arguments(filename, args, expected_retval): + assert pretty_format_toml([filename] + args) == expected_retval + + def test_pretty_format_toml_autofix(tmpdir): run_autofix_test( tmpdir,