]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/uoptions.c
2 *******************************************************************************
4 * Copyright (C) 2000-2015, 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 if(option
->optionFn
!=NULL
&& option
->optionFn(option
->context
, option
)<0) {
69 /* the option function was called and returned an error */
75 /* process one or more short options */
77 /* search for the option letter */
79 for(j
=0; j
<optionCount
; ++j
) {
80 if(c
==options
[j
].shortName
) {
86 /* no option matches */
91 if(option
->hasArg
!=UOPT_NO_ARG
) {
92 /* parse the argument for the option, if any */
94 /* argument following in the same argv[] */
96 /* do not process the rest of this arg as option letters */
98 } else if(i
+1<argc
&& !(argv
[i
+1][0]=='-' && argv
[i
+1][1]!=0)) {
99 /* argument in the next argv[], and there is not an option in there */
100 option
->value
=argv
[++i
];
101 /* this break is redundant because we know that *arg==0 */
103 } else if(option
->hasArg
==UOPT_REQUIRES_ARG
) {
104 /* there is no argument, but one is required: return with error */
110 if(option
->optionFn
!=NULL
&& option
->optionFn(option
->context
, option
)<0) {
111 /* the option function was called and returned an error */
116 /* get the next option letter */
122 /* go to next argv[] */
125 /* move a non-option up in argv[] */
126 argv
[remaining
++]=arg
;