@@ -51,6 +51,10 @@ def sort_file_contents(
5151 return FAIL
5252
5353
54+ def group_cases_together_key (s : bytes ) -> tuple [bytes , bytes ]:
55+ return s .lower (), s
56+
57+
5458def main (argv : Sequence [str ] | None = None ) -> int :
5559 parser = argparse .ArgumentParser ()
5660 parser .add_argument ('filenames' , nargs = '+' , help = 'Files to sort' )
@@ -61,22 +65,39 @@ def main(argv: Sequence[str] | None = None) -> int:
6165 action = 'store_const' ,
6266 const = bytes .lower ,
6367 default = None ,
64- help = 'fold lower case to upper case characters' ,
68+ help = 'fold lower case to upper case characters. this retains\n '
69+ 'the original order of lines that differ only in case,\n '
70+ 'so you probably want --group-cases-together instead.' ,
6571 )
6672 mutex .add_argument (
73+ '--group-cases-together' ,
74+ action = 'store_const' ,
75+ const = group_cases_together_key ,
76+ default = None ,
77+ help = 'group lines that differ only in case together,\n '
78+ 'so e.g. `c`, `b`, `a`, and `B` are sorted to\n '
79+ '`a`, `B`, `b`, and `c` instead of `B`, `a`, `b`, and `c`.' ,
80+ )
81+ parser .add_argument (
6782 '--unique' ,
6883 action = 'store_true' ,
6984 help = 'ensure each line is unique' ,
7085 )
7186
7287 args = parser .parse_args (argv )
88+ # we can't just use add_mutually_exclusive_group for this since
89+ # --unique is allowed with --group-cases-together
90+ if args .ignore_case and args .unique :
91+ parser .error ('argument --ignore-case: not allowed with argument --unique' )
92+
93+ key = args .ignore_case or args .group_cases_together
7394
7495 retv = PASS
7596
7697 for arg in args .filenames :
7798 with open (arg , 'rb+' ) as file_obj :
7899 ret_for_file = sort_file_contents (
79- file_obj , key = args . ignore_case , unique = args .unique ,
100+ file_obj , key = key , unique = args .unique ,
80101 )
81102
82103 if ret_for_file :
0 commit comments