@@ -13,46 +13,49 @@ use uucore::{
13
13
encoding:: Format ,
14
14
error:: { UResult , UUsageError } ,
15
15
} ;
16
- const ENCODINGS : & [ ( & str , Format , & str ) ] = & [
17
- ( "base64" , Format :: Base64 , "same as 'base64' program" ) ,
18
- ( "base64url" , Format :: Base64Url , "file- and url-safe base64" ) ,
19
- ( "base32" , Format :: Base32 , "same as 'base32' program" ) ,
20
- (
21
- "base32hex" ,
22
- Format :: Base32Hex ,
23
- "extended hex alphabet base32" ,
24
- ) ,
25
- ( "base16" , Format :: Base16 , "hex encoding" ) ,
26
- (
27
- "base2lsbf" ,
28
- Format :: Base2Lsbf ,
29
- "bit string with least significant bit (lsb) first" ,
30
- ) ,
31
- (
32
- "base2msbf" ,
33
- Format :: Base2Msbf ,
34
- "bit string with most significant bit (msb) first" ,
35
- ) ,
36
- (
37
- "z85" ,
38
- Format :: Z85 ,
39
- "ascii85-like encoding;\n \
40
- when encoding, input length must be a multiple of 4;\n \
41
- when decoding, input length must be a multiple of 5",
42
- ) ,
43
- ] ;
16
+
17
+ fn get_encodings ( ) -> Vec < ( & ' static str , Format , String ) > {
18
+ vec ! [
19
+ ( "base64" , Format :: Base64 , get_message( "basenc-help-base64" ) ) ,
20
+ (
21
+ "base64url" ,
22
+ Format :: Base64Url ,
23
+ get_message( "basenc-help-base64url" ) ,
24
+ ) ,
25
+ ( "base32" , Format :: Base32 , get_message( "basenc-help-base32" ) ) ,
26
+ (
27
+ "base32hex" ,
28
+ Format :: Base32Hex ,
29
+ get_message( "basenc-help-base32hex" ) ,
30
+ ) ,
31
+ ( "base16" , Format :: Base16 , get_message( "basenc-help-base16" ) ) ,
32
+ (
33
+ "base2lsbf" ,
34
+ Format :: Base2Lsbf ,
35
+ get_message( "basenc-help-base2lsbf" ) ,
36
+ ) ,
37
+ (
38
+ "base2msbf" ,
39
+ Format :: Base2Msbf ,
40
+ get_message( "basenc-help-base2msbf" ) ,
41
+ ) ,
42
+ ( "z85" , Format :: Z85 , get_message( "basenc-help-z85" ) ) ,
43
+ ]
44
+ }
44
45
45
46
pub fn uu_app ( ) -> Command {
46
47
let about: & ' static str = Box :: leak ( get_message ( "basenc-about" ) . into_boxed_str ( ) ) ;
47
48
let usage: & ' static str = Box :: leak ( get_message ( "basenc-usage" ) . into_boxed_str ( ) ) ;
48
49
50
+ let encodings = get_encodings ( ) ;
49
51
let mut command = base_common:: base_app ( about, usage) ;
50
- for encoding in ENCODINGS {
52
+
53
+ for encoding in & encodings {
51
54
let raw_arg = Arg :: new ( encoding. 0 )
52
55
. long ( encoding. 0 )
53
- . help ( encoding. 2 )
56
+ . help ( & encoding. 2 )
54
57
. action ( ArgAction :: SetTrue ) ;
55
- let overriding_arg = ENCODINGS
58
+ let overriding_arg = encodings
56
59
. iter ( )
57
60
. fold ( raw_arg, |arg, enc| arg. overrides_with ( enc. 0 ) ) ;
58
61
command = command. arg ( overriding_arg) ;
@@ -64,10 +67,17 @@ fn parse_cmd_args(args: impl uucore::Args) -> UResult<(Config, Format)> {
64
67
let matches = uu_app ( )
65
68
. try_get_matches_from ( args. collect_lossy ( ) )
66
69
. with_exit_code ( 1 ) ?;
67
- let format = ENCODINGS
70
+
71
+ let encodings = get_encodings ( ) ;
72
+ let format = encodings
68
73
. iter ( )
69
74
. find ( |encoding| matches. get_flag ( encoding. 0 ) )
70
- . ok_or_else ( || UUsageError :: new ( BASE_CMD_PARSE_ERROR , "missing encoding type" ) ) ?
75
+ . ok_or_else ( || {
76
+ UUsageError :: new (
77
+ BASE_CMD_PARSE_ERROR ,
78
+ & get_message ( "basenc-error-missing-encoding-type" ) ,
79
+ )
80
+ } ) ?
71
81
. 1 ;
72
82
let config = Config :: from ( & matches) ?;
73
83
Ok ( ( config, format) )
0 commit comments