Skip to content

Commit

Permalink
Add sort-first to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Mar 15, 2023
1 parent 1ef69c0 commit 986349c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ This project can be used as either a command line utility or a Python library. R
```console
$ toml-sort --help
usage: toml-sort [-h] [--version] [-o OUTPUT] [-i] [-I] [-a] [--no-sort-tables] [--sort-table-keys]
[--sort-inline-tables] [--sort-inline-arrays] [--no-header] [--no-comments] [--no-header-comments]
[--no-footer-comments] [--no-inline-comments] [--no-block-comments]
[--sort-inline-tables] [--sort-inline-arrays] [--sort-first KEYS] [--no-header] [--no-comments]
[--no-header-comments] [--no-footer-comments] [--no-inline-comments] [--no-block-comments]
[--spaces-before-inline-comment {1,2,3,4}] [--spaces-indent-inline-array {2,4,6,8}]
[--trailing-comma-inline-array] [--check]
[F [F ...]]
[F ...]

Toml sort: a sorting utility for toml files.

Expand All @@ -64,6 +64,8 @@ sort:
--sort-table-keys Sort the keys in tables and arrays of tables (excluding inline tables and arrays).
--sort-inline-tables Sort inline tables.
--sort-inline-arrays Sort inline arrays.
--sort-first KEYS Table keys that will be sorted first in the output. Multiple keys can be given separated by a
comma.

comments:
exclude comments from output
Expand Down Expand Up @@ -120,6 +122,7 @@ no_footer_comments = true
no_inline_comments = true
no_block_comments = true
no_sort_tables = true
sort_first = ["key1", "key2"]
sort_table_keys = true
sort_inline_tables = true
sort_inline_arrays = true
Expand All @@ -137,6 +140,7 @@ Only the following options can be included in an override:

```toml
[tool.tomlsort.overrides."path.to.key"]
first = ["key1", "key2"]
table_keys = true
inline_tables = true
inline_arrays = true
Expand Down
12 changes: 12 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def test_load_config_file_read():
"[tool.tomlsort]\nspaces_before_inline_comment=4",
{"spaces_before_inline_comment": 4},
),
("[tool.tomlsort]\nsort_first=['x', 'y']", {"sort_first": "x,y"}),
],
)
def test_load_config_file(toml, expected):
Expand Down Expand Up @@ -339,6 +340,17 @@ def test_load_config_file_invalid(toml):
"test.789": SortOverrideConfiguration(inline_arrays=False),
},
),
(
"""
[tool.tomlsort.overrides]
"test.123".first = ["one", "two", "three"]
""",
{
"test.123": SortOverrideConfiguration(
first=["one", "two", "three"]
),
},
),
],
)
def test_load_config_overrides(toml, expected):
Expand Down
14 changes: 14 additions & 0 deletions toml_sort/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def parse_config(tomlsort_section: TOMLDocument) -> Dict[str, Any]:
validate_and_copy(
config, clean_config, "trailing_comma_inline_array", bool
)
validate_and_copy(config, clean_config, "sort_first", list)
if "sort_first" in clean_config:
clean_config["sort_first"] = ",".join(clean_config["sort_first"])

if config:
printerr(f"Unexpected configuration values: {config}")
Expand Down Expand Up @@ -242,6 +245,16 @@ def get_parser(defaults: Dict[str, Any]) -> ArgumentParser:
help=("Sort inline arrays."),
action="store_true",
)
sort.add_argument(
"--sort-first",
help=(
"Table keys that will be sorted first in the output. Multiple "
"keys can be given separated by a comma."
),
metavar="KEYS",
type=str,
default="",
)
comments = parser.add_argument_group(
"comments", "exclude comments from output"
)
Expand Down Expand Up @@ -386,6 +399,7 @@ def cli( # pylint: disable=too-many-branches
table_keys=bool(args.sort_table_keys or args.all),
inline_tables=bool(args.sort_inline_tables or args.all),
inline_arrays=bool(args.sort_inline_arrays or args.all),
first=args.sort_first.split(","),
),
format_config=FormattingConfiguration(
spaces_before_inline_comment=args.spaces_before_inline_comment,
Expand Down

0 comments on commit 986349c

Please sign in to comment.