forked from ddribin/ddcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleApp.m
110 lines (95 loc) · 2.94 KB
/
ExampleApp.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
#import "ExampleApp.h"
@implementation ExampleApp
- (id)init;
{
self = [super init];
if (self == nil)
return nil;
_includeDirectories = [[NSMutableArray alloc] init];
return self;
}
- (void)setVerbose:(BOOL)verbose;
{
if (verbose)
_verbosity++;
else if (_verbosity > 0)
_verbosity--;
}
- (void)setInclude:(NSString *)file;
{
if ([file isEqualToString:@"invalid"])
{
DDCliParseException * e =
[DDCliParseException parseExceptionWithReason:@"Invalid include name"
exitCode:EX_USAGE];
@throw e;
}
[_includeDirectories addObject:file];
}
- (void)printUsage:(FILE *)stream;
{
ddfprintf(stream, @"%@: Usage [OPTIONS] <argument> [...]\n", DDCliApp);
}
- (void)printHelp;
{
[self printUsage: stdout];
printf("\n"
" -f, --foo FOO Use foo with FOO\n"
" -I, --include FILE Include FILE\n"
" -b, --bar[=BAR] Use bar with BAR\n"
" --long-opt Enable long option\n"
" -v, --verbose Increase verbosity\n"
" --version Display version and exit\n"
" -h, --help Display this help and exit\n"
"\n"
"A test application for DDCommandLineInterface.\n");
}
- (void)printVersion;
{
ddprintf(@"%@ version %s\n", DDCliApp, CURRENT_MARKETING_VERSION);
}
- (void)application:(DDCliApplication *)app
willParseOptions:(DDGetoptLongParser *)optionsParser;
{
DDGetoptOption optionTable[] =
{
// Long Short Argument options
{"foo", 'f', DDGetoptRequiredArgument},
{"include", 'I', DDGetoptRequiredArgument},
{"bar", 'b', DDGetoptOptionalArgument},
{"long-opt", 0, DDGetoptNoArgument},
{"verbose", 'v', DDGetoptNoArgument},
{"version", 0, DDGetoptNoArgument},
{"help", 'h', DDGetoptNoArgument},
{nil, 0, 0},
};
[optionsParser addOptionsFromTable:optionTable];
}
- (int)application:(DDCliApplication *)app
runWithArguments:(NSArray *)arguments;
{
if (_help)
{
[self printHelp];
return EXIT_SUCCESS;
}
if (_version)
{
[self printVersion];
return EXIT_SUCCESS;
}
if ([arguments count] < 1)
{
ddfprintf(stderr, @"%@: At least one argument is required\n", DDCliApp);
[self printUsage: stderr];
ddfprintf(stderr, @"Try `%@ --help' for more information.\n",
DDCliApp);
return EX_USAGE;
}
ddprintf(@"foo: %@, bar: %@, longOpt: %@, verbosity: %d\n",
_foo, _bar, _longOpt, _verbosity);
ddprintf(@"Include directories: %@\n", _includeDirectories);
ddprintf(@"Arguments: %@\n", arguments);
return EXIT_SUCCESS;
}
@end