% example example: At least one argument is required example: Usage [OPTIONS] <argument> [...] Try `example --help' for more information.
% 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.
% example --foo bar --long-opt one.c foo: bar, bar: (null), longOpt: 1, verbosity: 0 Include directories: () Arguments: ("one.c")
% 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")
Here is the source code:
#import <Foundation/Foundation.h> #import "DDCommandLineInterface.h" @interface ExampleApp : NSObject <DDCliApplicationDelegate> { NSString * _foo; NSMutableArray * _includeDirectories; NSString * _bar; NSString * _longOpt; int _verbosity; BOOL _version; BOOL _help; } @end
#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
#import <Foundation/Foundation.h> #import "DDCommandLineInterface.h" #import "ExampleApp.h" int main (int argc, char * const * argv) { return DDCliAppRunWithClass([ExampleApp class]); }