-
Notifications
You must be signed in to change notification settings - Fork 0
/
displayutil_argutils.m
174 lines (136 loc) · 4.57 KB
/
displayutil_argutils.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
displayutil - displayutil_argutils.m
command line argument handling utility functions
History:
v. 1.0.0 (04/06/2021) - Initial version
v. 1.0.1 (04/15/2021) - Restrict args to exact matches
v. 1.0.2 (09/05/2021) - add help mode
v. 1.0.3 (09/07/2021) - add verbose mode
v. 1.0.4 (09/17/2021) - change verbose, extended, and hidden modes
to -l (long), -a (all), and -p (private),
respectively
v. 1.0.5 (04/27/2022) - add plain help without a dash as a valid
help mode
v. 1.0.6 (04/30/2022) - add checking for yes as a mode / argument
Copyright (c) 2021-2022 Sriranga R. Veeraraghavan <ranga@calalum.org>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import <strings.h>
#import <ApplicationServices/ApplicationServices.h>
#import "displayutil_argutils.h"
/* program name */
const char *gPgmName = "displayutil";
/* option strings */
const char *gStrEnable = "enable";
const char *gStrOn = "on";
const char *gStrDisable = "disable";
const char *gStrOff = "off";
const char *gStrAll = "all";
const char *gStrModeYesLong = "yes";
const char *gStrModeYesShort = "y";
const char *gStrModeAll = "-a";
const char *gStrModeAllLong = "-al";
const char *gStrModeHelpShort = "-h";
const char *gStrModeHelpLong = "-help";
const char *gStrModeHelpPlain = "help";
const char *gStrModeLong = "-l";
const char *gStrModeLongAll = "-la";
const char *gStrModePrivate = "-p";
/* isArg - check if the arg is the requested mode */
bool isArg(const char *arg,
const char *longMode,
const char *shortMode)
{
size_t modeStrLen = 0;
if (arg == NULL || arg[0] == '\0')
{
return false;
}
if (longMode != NULL)
{
modeStrLen = strlen(longMode);
if (modeStrLen > 1 &&
strncasecmp(arg, longMode, modeStrLen) == 0)
{
if (strlen(arg) == modeStrLen)
{
return true;
}
return false;
}
}
if (shortMode == NULL)
{
return false;
}
modeStrLen = strlen(shortMode);
if (modeStrLen < 1)
{
return false;
}
if (strncasecmp(arg, shortMode, modeStrLen) == 0 &&
strlen(arg) == modeStrLen)
{
return true;
}
return false;
}
/* isArgEnable - check if the arg is enable mode */
bool isArgEnable(const char *arg)
{
return isArg(arg, gStrEnable, gStrOn);
}
/* isArgDisable - check if the arg is disable mode */
bool isArgDisable(const char *arg)
{
return isArg(arg, gStrDisable, gStrOff);
}
/* isArgAll - check if the arg is all output mode */
bool isArgAll(const char *arg)
{
return isArg(arg, gStrModeAll, NULL);
}
/* isArgAllLong - check if the arg is all and long output mode */
bool isArgAllLong(const char *arg)
{
return isArg(arg, gStrModeAllLong, gStrModeLongAll);
}
/* isArgYes - check if the arg is yes */
bool isArgYes(const char *arg)
{
return isArg(arg, gStrModeYesLong, gStrModeYesShort);
}
/* isArgHelp - check if the arg is help mode */
bool isArgHelp(const char *arg)
{
if (isArg(arg, gStrModeHelpLong, gStrModeHelpShort))
{
return true;
}
return isArg(arg, gStrModeHelpPlain, NULL);
}
/* isArgLong - check if the arg is long output mode */
bool isArgLong(const char *arg)
{
return isArg(arg, gStrModeLong, NULL);
}
/* isArgPrivate - check if the arg is private information output mode */
bool isArgPrivate(const char *arg)
{
return isArg(arg, gStrModePrivate, NULL);
}