1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2000-2015, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: uoptions.c
12 * tab size: 8 (not used)
15 * created on: 2000apr17
16 * created by: Markus W. Scherer
18 * This file provides a command line argument parser.
21 #include "unicode/utypes.h"
26 u_parseArgs(int argc
, char* argv
[],
27 int optionCount
, UOption options
[]) {
30 char c
, stopOptions
=0;
34 if(!stopOptions
&& *arg
=='-' && (c
=arg
[1])!=0) {
35 /* process an option */
39 /* process a long option */
41 /* stop processing options after "--" */
44 /* search for the option string */
46 for(j
=0; j
<optionCount
; ++j
) {
47 if(options
[j
].longName
&& uprv_strcmp(arg
, options
[j
].longName
)==0) {
53 /* no option matches */
58 if(option
->hasArg
!=UOPT_NO_ARG
) {
59 /* parse the argument for the option, if any */
60 if(i
+1<argc
&& !(argv
[i
+1][0]=='-' && argv
[i
+1][1]!=0)) {
61 /* argument in the next argv[], and there is not an option in there */
62 option
->value
=argv
[++i
];
63 } else if(option
->hasArg
==UOPT_REQUIRES_ARG
) {
64 /* there is no argument, but one is required: return with error */
70 if(option
->optionFn
!=NULL
&& option
->optionFn(option
->context
, option
)<0) {
71 /* the option function was called and returned an error */
77 /* process one or more short options */
79 /* search for the option letter */
81 for(j
=0; j
<optionCount
; ++j
) {
82 if(c
==options
[j
].shortName
) {
88 /* no option matches */
93 if(option
->hasArg
!=UOPT_NO_ARG
) {
94 /* parse the argument for the option, if any */
96 /* argument following in the same argv[] */
98 /* do not process the rest of this arg as option letters */
100 } else if(i
+1<argc
&& !(argv
[i
+1][0]=='-' && argv
[i
+1][1]!=0)) {
101 /* argument in the next argv[], and there is not an option in there */
102 option
->value
=argv
[++i
];
103 /* this break is redundant because we know that *arg==0 */
105 } else if(option
->hasArg
==UOPT_REQUIRES_ARG
) {
106 /* there is no argument, but one is required: return with error */
112 if(option
->optionFn
!=NULL
&& option
->optionFn(option
->context
, option
)<0) {
113 /* the option function was called and returned an error */
118 /* get the next option letter */
124 /* go to next argv[] */
127 /* move a non-option up in argv[] */
128 argv
[remaining
++]=arg
;