Pārlūkot izejas kodu

[parseopt] Disambiguate the various EINVAL errors

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 gadus atpakaļ
vecāks
revīzija
c49659c4f2
1 mainītis faili ar 16 papildinājumiem un 2 dzēšanām
  1. 16
    2
      src/core/parseopt.c

+ 16
- 2
src/core/parseopt.c Parādīt failu

@@ -40,6 +40,17 @@ FILE_LICENCE ( GPL2_OR_LATER );
40 40
 #define EINFO_ECANCELED_NO_OP \
41 41
 	__einfo_uniqify ( EINFO_ECANCELED, 0x01, "Nothing to do" )
42 42
 
43
+/* Disambiguate the various error codes */
44
+#define EINVAL_INTEGER __einfo_error ( EINFO_EINVAL_INTEGER )
45
+#define EINFO_EINVAL_INTEGER \
46
+	__einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid integer value" )
47
+#define EINVAL_UNKNOWN_OPTION __einfo_error ( EINFO_EINVAL_UNKNOWN_OPTION )
48
+#define EINFO_EINVAL_UNKNOWN_OPTION \
49
+	__einfo_uniqify ( EINFO_EINVAL, 0x02, "Unrecognised option" )
50
+#define EINVAL_MISSING_ARGUMENT __einfo_error ( EINFO_EINVAL_MISSING_ARGUMENT )
51
+#define EINFO_EINVAL_MISSING_ARGUMENT \
52
+	__einfo_uniqify ( EINFO_EINVAL, 0x03, "Missing argument" )
53
+
43 54
 /**
44 55
 * Parse string value
45 56
  *
@@ -75,7 +86,7 @@ int parse_integer ( const char *text, unsigned int *value ) {
75 86
 	*value = strtoul ( text, &endp, 0 );
76 87
 	if ( *endp ) {
77 88
 		printf ( "\"%s\": invalid integer value\n", text );
78
-		return -EINVAL;
89
+		return -EINVAL_INTEGER;
79 90
 	}
80 91
 
81 92
 	return 0;
@@ -202,10 +213,13 @@ int reparse_options ( int argc, char **argv, struct command_descriptor *cmd,
202 213
 			print_usage ( cmd, argv );
203 214
 			return -ECANCELED_NO_OP;
204 215
 		case '?' :
216
+			/* Print usage message */
217
+			print_usage ( cmd, argv );
218
+			return -EINVAL_UNKNOWN_OPTION;
205 219
 		case ':' :
206 220
 			/* Print usage message */
207 221
 			print_usage ( cmd, argv );
208
-			return -EINVAL;
222
+			return -EINVAL_MISSING_ARGUMENT;
209 223
 		default:
210 224
 			/* Search for an option to parse */
211 225
 			for ( i = 0 ; i < cmd->num_options ; i++ ) {

Notiek ielāde…
Atcelt
Saglabāt