@@ -591,62 +591,82 @@ impl<'a> State<'a> {
591
591
}
592
592
}
593
593
594
- #[ uucore:: main]
595
- #[ allow( clippy:: cognitive_complexity) ]
596
- pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
597
- let matches = uu_app ( ) . try_get_matches_from ( args) ?;
598
-
599
- let keys = parse_field_number_option ( matches. get_one :: < String > ( "j" ) . map ( |s| s. as_str ( ) ) ) ?;
600
- let key1 = parse_field_number_option ( matches. get_one :: < String > ( "1" ) . map ( |s| s. as_str ( ) ) ) ?;
601
- let key2 = parse_field_number_option ( matches. get_one :: < String > ( "2" ) . map ( |s| s. as_str ( ) ) ) ?;
594
+ fn parse_separator ( value_os : & OsString ) -> UResult < Sep > {
595
+ #[ cfg( unix) ]
596
+ let value = value_os. as_bytes ( ) ;
597
+ #[ cfg( not( unix) ) ]
598
+ let value = match value_os. to_str ( ) {
599
+ Some ( value) => value. as_bytes ( ) ,
600
+ None => {
601
+ return Err ( USimpleError :: new (
602
+ 1 ,
603
+ "unprintable field separators are only supported on unix-like platforms" ,
604
+ ) ) ;
605
+ }
606
+ } ;
607
+ match value. len ( ) {
608
+ 0 => Ok ( Sep :: Line ) ,
609
+ 1 => Ok ( Sep :: Char ( value[ 0 ] ) ) ,
610
+ 2 if value[ 0 ] == b'\\' && value[ 1 ] == b'0' => Ok ( Sep :: Char ( 0 ) ) ,
611
+ _ => Err ( USimpleError :: new (
612
+ 1 ,
613
+ format ! ( "multi-character tab {}" , value_os. to_string_lossy( ) ) ,
614
+ ) ) ,
615
+ }
616
+ }
602
617
603
- let mut settings = Settings :: default ( ) ;
618
+ fn parse_print_settings ( matches : & clap:: ArgMatches ) -> UResult < ( bool , bool , bool ) > {
619
+ let mut print_joined = true ;
620
+ let mut print_unpaired1 = false ;
621
+ let mut print_unpaired2 = false ;
604
622
605
623
let v_values = matches. get_many :: < String > ( "v" ) ;
606
624
if v_values. is_some ( ) {
607
- settings . print_joined = false ;
625
+ print_joined = false ;
608
626
}
609
627
610
628
let unpaired = v_values
611
629
. unwrap_or_default ( )
612
630
. chain ( matches. get_many ( "a" ) . unwrap_or_default ( ) ) ;
613
631
for file_num in unpaired {
614
632
match parse_file_number ( file_num) ? {
615
- FileNum :: File1 => settings . print_unpaired1 = true ,
616
- FileNum :: File2 => settings . print_unpaired2 = true ,
633
+ FileNum :: File1 => print_unpaired1 = true ,
634
+ FileNum :: File2 => print_unpaired2 = true ,
617
635
}
618
636
}
619
637
638
+ Ok ( ( print_joined, print_unpaired1, print_unpaired2) )
639
+ }
640
+
641
+ fn get_and_parse_field_number ( matches : & clap:: ArgMatches , key : & str ) -> UResult < Option < usize > > {
642
+ let value = matches. get_one :: < String > ( key) . map ( |s| s. as_str ( ) ) ;
643
+ parse_field_number_option ( value)
644
+ }
645
+
646
+ /// Parses the command-line arguments and constructs a `Settings` struct.
647
+ ///
648
+ /// This function takes the matches from the command-line arguments, processes them,
649
+ /// and returns a `Settings` struct that encapsulates the configuration for the program.
650
+ #[ allow( clippy:: field_reassign_with_default) ]
651
+ fn parse_settings ( matches : & clap:: ArgMatches ) -> UResult < Settings > {
652
+ let keys = get_and_parse_field_number ( matches, "j" ) ?;
653
+ let key1 = get_and_parse_field_number ( matches, "1" ) ?;
654
+ let key2 = get_and_parse_field_number ( matches, "2" ) ?;
655
+
656
+ let ( print_joined, print_unpaired1, print_unpaired2) = parse_print_settings ( matches) ?;
657
+
658
+ let mut settings = Settings :: default ( ) ;
659
+
660
+ settings. print_joined = print_joined;
661
+ settings. print_unpaired1 = print_unpaired1;
662
+ settings. print_unpaired2 = print_unpaired2;
663
+
620
664
settings. ignore_case = matches. get_flag ( "i" ) ;
621
665
settings. key1 = get_field_number ( keys, key1) ?;
622
666
settings. key2 = get_field_number ( keys, key2) ?;
623
-
624
667
if let Some ( value_os) = matches. get_one :: < OsString > ( "t" ) {
625
- #[ cfg( unix) ]
626
- let value = value_os. as_bytes ( ) ;
627
- #[ cfg( not( unix) ) ]
628
- let value = match value_os. to_str ( ) {
629
- Some ( value) => value. as_bytes ( ) ,
630
- None => {
631
- return Err ( USimpleError :: new (
632
- 1 ,
633
- "unprintable field separators are only supported on unix-like platforms" ,
634
- ) )
635
- }
636
- } ;
637
- settings. separator = match value. len ( ) {
638
- 0 => Sep :: Line ,
639
- 1 => Sep :: Char ( value[ 0 ] ) ,
640
- 2 if value[ 0 ] == b'\\' && value[ 1 ] == b'0' => Sep :: Char ( 0 ) ,
641
- _ => {
642
- return Err ( USimpleError :: new (
643
- 1 ,
644
- format ! ( "multi-character tab {}" , value_os. to_string_lossy( ) ) ,
645
- ) )
646
- }
647
- } ;
668
+ settings. separator = parse_separator ( value_os) ?;
648
669
}
649
-
650
670
if let Some ( format) = matches. get_one :: < String > ( "o" ) {
651
671
if format == "auto" {
652
672
settings. autoformat = true ;
@@ -677,6 +697,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
677
697
678
698
settings. line_ending = LineEnding :: from_zero_flag ( matches. get_flag ( "z" ) ) ;
679
699
700
+ Ok ( settings)
701
+ }
702
+
703
+ #[ uucore:: main]
704
+ pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
705
+ let matches = uu_app ( ) . try_get_matches_from ( args) ?;
706
+
707
+ let settings = parse_settings ( & matches) ?;
708
+
680
709
let file1 = matches. get_one :: < String > ( "file1" ) . unwrap ( ) ;
681
710
let file2 = matches. get_one :: < String > ( "file2" ) . unwrap ( ) ;
682
711
0 commit comments