/
DDCliApplication.h
189 lines (168 loc) · 5.33 KB
/
DDCliApplication.h
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (c) 2007-2008 Dave Dribin
*
* 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 <Cocoa/Cocoa.h>
@class DDCliApplication;
@class DDGetoptLongParser;
/**
* Methods that the DDCliApplication delegate must implement.
*/
@protocol DDCliApplicationDelegate <NSObject>
/**
* This is the main entry point of a command line application. It is
* called after options have been parsed, and the arguments passed in
* have the options removed.
*
* @param app The corresponding application instance
* @param arguments Command line arguments, post option parsing
* @return The return value of the application
*/
- (int)application:(DDCliApplication *)app
runWithArguments:(NSArray *)arguments;
/**
* Called prior to option parsing so that options may added to the
* options parser.
*
* @param app The corresponding application instance
* @param optionParser The option parser.
*/
- (void)application:(DDCliApplication *)app
willParseOptions:(DDGetoptLongParser *)optionParser;
@end
/**
* A class that represents a command line application.
*/
@interface DDCliApplication : NSObject
{
@private
NSString * _name;
}
/**
* Returns the common shared application.
*
* @return The common shared application
*/
+ (DDCliApplication *)sharedApplication;
/**
* Returns the name of this application.
*
* @return The name of this application
*/
- (NSString *)name;
/**
* Returns the name of this application. Coupled with the
* #DDCliApp global, this makes it easy to print standard Unix-style
* error messages:
*
* @code
ddfprintf(stderr, "%@: An error occured", DDCliApp);
* @endcode
*
* @return The application name
*/
- (NSString *)description;
/**
* Runs a command line application with the specified delegate class,
* and returns the result. This instantiates an instance of the
* delegate class, and releases it up completion. Exceptions are
* trapped, and an error message is printed.
*
* @param delegateClass The class of the delegate or <code>nil</code>
* to search all classes for the delegate.
*/
- (int)runWithClass:(Class)delegateClass;
@end
/**
* @ingroup functions
* @{
*/
/** The shared application. */
extern DDCliApplication * DDCliApp;
/**
* Runs a command line application with the given delegate class.
* This sets up an autorelease pool, and creates an instance of the
* delegate class.
*
* @param delegateClass Class to instantiate for the delegate or
* <code>nil</code> to specify the default delegate class.
*/
int DDCliAppRunWithClass(Class delegateClass);
/**
* Runs a command line application with the default delegate class. To find
* the default delegate class, all classes are searched until one is found
* that implements the DDCliApplicationDelegate protocol. The first matching
* class is used as the delegate class, so be sure to only include a single
* class that implements this protocol per application.
*/
int DDCliAppRunWithDefaultClass();
/** @} */
/**
* @example SimpleApp.m
*
* This is a very simple example application.
*
* @include ddcli_main.m
* @include SimpleApp.h
*/
/**
* @example ExampleApp.m
*
* This is a slighly more complex example application. Here are a
* few sample runs of this program:
*
* @verbatim
% example
example: At least one argument is required
example: Usage [OPTIONS] <argument> [...]
Try `example --help' for more information.
@endverbatim
@verbatim
% example --help
example: Usage [OPTIONS] <argument> [...]
-f, --foo FOO Use foo with FOO
-I, --include FILE Include FILE
-b, --bar[=BAR] Use bar with BAR
--long-opt Enable long option
-v, --verbose Increase verbosity
--version Display version and exit
-h, --help Display this help and exit
A test application for DDCommandLineInterface.
@endverbatim
@verbatim
% example --foo bar --long-opt one.c
foo: bar, bar: (null), longOpt: 1, verbosity: 0
Include directories: ()
Arguments: ("one.c")
@endverbatim
@verbatim
% example -vvv -I/usr/include -I/usr/local/include one.c two.c
foo: (null), bar: (null), longOpt: (null), verbosity: 3
Include directories: ("/usr/include", "/usr/local/include")
Arguments: ("one.c", "two.c")
@endverbatim
*
* Here is the source code:
*
* @include ddcli_main.m
* @include ExampleApp.h
*/