]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/uoptions.c
2 *******************************************************************************
4 * Copyright (C) 2000, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: uoptions.c
10 * tab size: 8 (not used)
13 * created on: 2000apr17
14 * created by: Markus W. Scherer
16 * This file provides a command line argument parser.
19 #include "unicode/utypes.h"
24 u_parseArgs(int argc
, char* argv
[],
25 int optionCount
, UOption options
[]) {
28 char c
, stopOptions
=0;
32 if(!stopOptions
&& *arg
=='-' && (c
=arg
[1])!=0) {
33 /* process an option */
37 /* process a long option */
39 /* stop processing options after "--" */
42 /* search for the option string */
44 for(j
=0; j
<optionCount
; ++j
) {
45 if(options
[j
].longName
&& uprv_strcmp(arg
, options
[j
].longName
)==0) {
51 /* no option matches */
56 if(option
->hasArg
!=UOPT_NO_ARG
) {
57 /* parse the argument for the option, if any */
58 if(i
+1<argc
&& !(argv
[i
+1][0]=='-' && argv
[i
+1][1]!=0)) {
59 /* argument in the next argv[], and there is not an option in there */
60 option
->value
=argv
[++i
];
61 } else if(option
->hasArg
==UOPT_REQUIRES_ARG
) {
62 /* there is no argument, but one is required: return with error */
68 /* process one or more short options */
70 /* search for the option letter */
72 for(j
=0; j
<optionCount
; ++j
) {
73 if(c
==options
[j
].shortName
) {
79 /* no option matches */
84 if(option
->hasArg
!=UOPT_NO_ARG
) {
85 /* parse the argument for the option, if any */
87 /* argument following in the same argv[] */
89 /* do not process the rest of this arg as option letters */
91 } else if(i
+1<argc
&& !(argv
[i
+1][0]=='-' && argv
[i
+1][1]!=0)) {
92 /* argument in the next argv[], and there is not an option in there */
93 option
->value
=argv
[++i
];
94 /* this break is redundant because we know that *arg==0 */
96 } else if(option
->hasArg
==UOPT_REQUIRES_ARG
) {
97 /* there is no argument, but one is required: return with error */
102 /* get the next option letter */
108 if(option
!=0 && option
->optionFn
!=0 && option
->optionFn(option
->context
, option
)<0) {
109 /* the option function was called and returned an error */
113 /* go to next argv[] */
116 /* move a non-option up in argv[] */
117 argv
[remaining
++]=arg
;