@@ -41,54 +41,41 @@ impl Builtin for Cut {
4141 let mut files = Vec :: new ( ) ;
4242
4343 // Parse arguments
44- let mut i = 0 ;
45- while i < ctx. args . len ( ) {
46- let arg = & ctx. args [ i] ;
47- if arg == "-d" {
48- i += 1 ;
49- if i < ctx. args . len ( ) {
50- delimiter = ctx. args [ i] . chars ( ) . next ( ) . unwrap_or ( '\t' ) ;
51- }
52- } else if let Some ( d) = arg. strip_prefix ( "-d" ) {
53- delimiter = d. chars ( ) . next ( ) . unwrap_or ( '\t' ) ;
54- } else if arg == "-f" {
55- i += 1 ;
56- if i < ctx. args . len ( ) {
57- spec = ctx. args [ i] . clone ( ) ;
58- mode = CutMode :: Fields ;
59- }
60- } else if let Some ( f) = arg. strip_prefix ( "-f" ) {
61- spec = f. to_string ( ) ;
44+ let mut p = super :: arg_parser:: ArgParser :: new ( ctx. args ) ;
45+ while !p. is_done ( ) {
46+ if let Some ( val) = p. flag_value_opt ( "-d" ) {
47+ delimiter = val. chars ( ) . next ( ) . unwrap_or ( '\t' ) ;
48+ } else if let Some ( val) = p. flag_value_opt ( "-f" ) {
49+ spec = val. to_string ( ) ;
6250 mode = CutMode :: Fields ;
63- } else if arg == "-c" || arg == "-b" {
64- i += 1 ;
65- if i < ctx. args . len ( ) {
66- spec = ctx. args [ i] . clone ( ) ;
67- mode = CutMode :: Chars ;
68- }
69- } else if let Some ( c) = arg. strip_prefix ( "-c" ) {
70- spec = c. to_string ( ) ;
51+ } else if let Some ( val) = p. flag_value_opt ( "-c" ) {
52+ spec = val. to_string ( ) ;
7153 mode = CutMode :: Chars ;
72- } else if let Some ( b ) = arg . strip_prefix ( "-b" ) {
73- spec = b . to_string ( ) ;
54+ } else if let Some ( val ) = p . flag_value_opt ( "-b" ) {
55+ spec = val . to_string ( ) ;
7456 mode = CutMode :: Chars ;
75- } else if arg == "-s" {
57+ } else if p . flag ( "-s" ) {
7658 only_delimited = true ;
77- } else if arg == "-z" {
59+ } else if p . flag ( "-z" ) {
7860 zero_terminated = true ;
79- } else if arg == "--complement" {
61+ } else if p . flag ( "--complement" ) {
8062 complement = true ;
81- } else if let Some ( od) = arg. strip_prefix ( "--output-delimiter=" ) {
82- output_delimiter = Some ( od. to_string ( ) ) ;
83- } else if arg == "--output-delimiter" {
84- i += 1 ;
85- if i < ctx. args . len ( ) {
86- output_delimiter = Some ( ctx. args [ i] . clone ( ) ) ;
63+ } else if let Some ( val) = p
64+ . current ( )
65+ . and_then ( |s| s. strip_prefix ( "--output-delimiter=" ) )
66+ {
67+ output_delimiter = Some ( val. to_string ( ) ) ;
68+ p. advance ( ) ;
69+ } else if p. flag ( "--output-delimiter" ) {
70+ if let Some ( val) = p. positional ( ) {
71+ output_delimiter = Some ( val. to_string ( ) ) ;
8772 }
88- } else if !arg. starts_with ( '-' ) {
89- files. push ( arg. clone ( ) ) ;
73+ } else if let Some ( arg) = p. current ( ) . filter ( |s| !s. starts_with ( '-' ) ) {
74+ files. push ( arg. to_string ( ) ) ;
75+ p. advance ( ) ;
76+ } else {
77+ p. advance ( ) ;
9078 }
91- i += 1 ;
9279 }
9380
9481 if spec. is_empty ( ) {
0 commit comments