Permalink
Browse files

Working code commit.

  • Loading branch information...
1 parent 8e56bf3 commit 95d01a568b627e87e82d415846962517c0f9ed27 @tomaz committed Mar 15, 2012
@@ -9,7 +9,10 @@
/* Begin PBXBuildFile section */
73484A011511FD010031EF85 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 73484A001511FD010031EF85 /* Foundation.framework */; };
73484A041511FD010031EF85 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 73484A031511FD010031EF85 /* main.m */; };
- 73484A081511FD010031EF85 /* GBCli.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = 73484A071511FD010031EF85 /* GBCli.1 */; };
+ 73484A1B1511FDA90031EF85 /* GBCommandLineParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 73484A161511FDA90031EF85 /* GBCommandLineParser.m */; };
+ 73484A1C1511FDA90031EF85 /* GBOptionsHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 73484A181511FDA90031EF85 /* GBOptionsHelper.m */; };
+ 73484A1D1511FDA90031EF85 /* GBSettings.m in Sources */ = {isa = PBXBuildFile; fileRef = 73484A1A1511FDA90031EF85 /* GBSettings.m */; };
+ 73484A201511FE9E0031EF85 /* GBSettings+Application.m in Sources */ = {isa = PBXBuildFile; fileRef = 73484A1F1511FE9E0031EF85 /* GBSettings+Application.m */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@@ -19,7 +22,6 @@
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
- 73484A081511FD010031EF85 /* GBCli.1 in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 1;
};
@@ -29,8 +31,15 @@
734849FC1511FD010031EF85 /* GBCli */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = GBCli; sourceTree = BUILT_PRODUCTS_DIR; };
73484A001511FD010031EF85 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
73484A031511FD010031EF85 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
- 73484A061511FD010031EF85 /* GBCli-Prefix.pch */ = {isa = PBXFileReference; path = "GBCli-Prefix.pch"; sourceTree = "<group>"; };
- 73484A071511FD010031EF85 /* GBCli.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = GBCli.1; sourceTree = "<group>"; };
+ 73484A061511FD010031EF85 /* GBCli-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "GBCli-Prefix.pch"; sourceTree = "<group>"; };
+ 73484A151511FDA90031EF85 /* GBCommandLineParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GBCommandLineParser.h; sourceTree = "<group>"; };
+ 73484A161511FDA90031EF85 /* GBCommandLineParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GBCommandLineParser.m; sourceTree = "<group>"; };
+ 73484A171511FDA90031EF85 /* GBOptionsHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GBOptionsHelper.h; sourceTree = "<group>"; };
+ 73484A181511FDA90031EF85 /* GBOptionsHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GBOptionsHelper.m; sourceTree = "<group>"; };
+ 73484A191511FDA90031EF85 /* GBSettings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GBSettings.h; sourceTree = "<group>"; };
+ 73484A1A1511FDA90031EF85 /* GBSettings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GBSettings.m; sourceTree = "<group>"; };
+ 73484A1E1511FE9E0031EF85 /* GBSettings+Application.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GBSettings+Application.h"; sourceTree = "<group>"; };
+ 73484A1F1511FE9E0031EF85 /* GBSettings+Application.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GBSettings+Application.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -73,9 +82,8 @@
73484A021511FD010031EF85 /* GBCli */ = {
isa = PBXGroup;
children = (
- 73484A031511FD010031EF85 /* main.m */,
- 73484A071511FD010031EF85 /* GBCli.1 */,
- 73484A051511FD010031EF85 /* Supporting Files */,
+ 73484A141511FD8B0031EF85 /* lib */,
+ 73484A121511FD7F0031EF85 /* example */,
);
path = GBCli;
sourceTree = "<group>";
@@ -88,6 +96,31 @@
name = "Supporting Files";
sourceTree = "<group>";
};
+ 73484A121511FD7F0031EF85 /* example */ = {
+ isa = PBXGroup;
+ children = (
+ 73484A031511FD010031EF85 /* main.m */,
+ 73484A1E1511FE9E0031EF85 /* GBSettings+Application.h */,
+ 73484A1F1511FE9E0031EF85 /* GBSettings+Application.m */,
+ 73484A051511FD010031EF85 /* Supporting Files */,
+ );
+ name = example;
+ sourceTree = "<group>";
+ };
+ 73484A141511FD8B0031EF85 /* lib */ = {
+ isa = PBXGroup;
+ children = (
+ 73484A151511FDA90031EF85 /* GBCommandLineParser.h */,
+ 73484A161511FDA90031EF85 /* GBCommandLineParser.m */,
+ 73484A171511FDA90031EF85 /* GBOptionsHelper.h */,
+ 73484A181511FDA90031EF85 /* GBOptionsHelper.m */,
+ 73484A191511FDA90031EF85 /* GBSettings.h */,
+ 73484A1A1511FDA90031EF85 /* GBSettings.m */,
+ );
+ name = lib;
+ path = src;
+ sourceTree = "<group>";
+ };
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@@ -139,6 +172,10 @@
buildActionMask = 2147483647;
files = (
73484A041511FD010031EF85 /* main.m in Sources */,
+ 73484A1B1511FDA90031EF85 /* GBCommandLineParser.m in Sources */,
+ 73484A1C1511FDA90031EF85 /* GBOptionsHelper.m in Sources */,
+ 73484A1D1511FDA90031EF85 /* GBSettings.m in Sources */,
+ 73484A201511FE9E0031EF85 /* GBSettings+Application.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -229,6 +266,7 @@
73484A0D1511FD010031EF85 /* Release */,
);
defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
View
@@ -1,79 +0,0 @@
-.\"Modified from man(1) of FreeBSD, the NetBSD mdoc.template, and mdoc.samples.
-.\"See Also:
-.\"man mdoc.samples for a complete listing of options
-.\"man mdoc for the short list of editing options
-.\"/usr/share/misc/mdoc.template
-.Dd 3/15/12 \" DATE
-.Dt GBCli 1 \" Program name and manual section number
-.Os Darwin
-.Sh NAME \" Section Header - required - don't modify
-.Nm GBCli,
-.\" The following lines are read in generating the apropos(man -k) database. Use only key
-.\" words here as the database is built based on the words here and in the .ND line.
-.Nm Other_name_for_same_program(),
-.Nm Yet another name for the same program.
-.\" Use .Nm macro to designate other names for the documented program.
-.Nd This line parsed for whatis database.
-.Sh SYNOPSIS \" Section Header - required - don't modify
-.Nm
-.Op Fl abcd \" [-abcd]
-.Op Fl a Ar path \" [-a path]
-.Op Ar file \" [file]
-.Op Ar \" [file ...]
-.Ar arg0 \" Underlined argument - use .Ar anywhere to underline
-arg2 ... \" Arguments
-.Sh DESCRIPTION \" Section Header - required - don't modify
-Use the .Nm macro to refer to your program throughout the man page like such:
-.Nm
-Underlining is accomplished with the .Ar macro like this:
-.Ar underlined text .
-.Pp \" Inserts a space
-A list of items with descriptions:
-.Bl -tag -width -indent \" Begins a tagged list
-.It item a \" Each item preceded by .It macro
-Description of item a
-.It item b
-Description of item b
-.El \" Ends the list
-.Pp
-A list of flags and their descriptions:
-.Bl -tag -width -indent \" Differs from above in tag removed
-.It Fl a \"-a flag as a list item
-Description of -a flag
-.It Fl b
-Description of -b flag
-.El \" Ends the list
-.Pp
-.\" .Sh ENVIRONMENT \" May not be needed
-.\" .Bl -tag -width "ENV_VAR_1" -indent \" ENV_VAR_1 is width of the string ENV_VAR_1
-.\" .It Ev ENV_VAR_1
-.\" Description of ENV_VAR_1
-.\" .It Ev ENV_VAR_2
-.\" Description of ENV_VAR_2
-.\" .El
-.Sh FILES \" File used or created by the topic of the man page
-.Bl -tag -width "/Users/joeuser/Library/really_long_file_name" -compact
-.It Pa /usr/share/file_name
-FILE_1 description
-.It Pa /Users/joeuser/Library/really_long_file_name
-FILE_2 description
-.El \" Ends the list
-.\" .Sh DIAGNOSTICS \" May not be needed
-.\" .Bl -diag
-.\" .It Diagnostic Tag
-.\" Diagnostic informtion here.
-.\" .It Diagnostic Tag
-.\" Diagnostic informtion here.
-.\" .El
-.Sh SEE ALSO
-.\" List links in ascending order by section, alphabetically within a section.
-.\" Please do not reference files that do not exist without filing a bug report
-.Xr a 1 ,
-.Xr b 1 ,
-.Xr c 1 ,
-.Xr a 2 ,
-.Xr b 2 ,
-.Xr a 3 ,
-.Xr b 3
-.\" .Sh BUGS \" Document known, unremedied bugs
-.\" .Sh HISTORY \" Document history if command behaves in a unique manner
@@ -0,0 +1,39 @@
+//
+// GBSettings+Application.h
+// GBCli
+//
+// Created by Tomaž Kragelj on 3/15/12.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import "GBSettings.h"
+
+@interface GBSettings (Application)
+
+#pragma mark - Initialization & disposal
+
++ (id)mySettingsWithName:(NSString *)name parent:(GBSettings *)parent;
+
+#pragma mark - Project information
+
+@property (nonatomic, copy) NSString *projectName;
+@property (nonatomic, copy) NSString *projectVersion;
+
+#pragma mark - Paths
+
+@property (nonatomic, strong) NSArray *inputPaths;
+@property (nonatomic, strong) NSArray *outputPaths;
+
+#pragma mark - Debugging aid
+
+@property (nonatomic, assign) BOOL printSettings;
+
+@end
+
+#pragma mark -
+
+@interface GBSettings (Helpers)
+
+- (void)applyFactoryDefaults;
+
+@end
@@ -0,0 +1,48 @@
+//
+// GbSettings+Application.m
+// GBCli
+//
+// Created by Tomaž Kragelj on 3/15/12.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import "GBSettings+Application.h"
+
+@implementation GBSettings (Application)
+
+#pragma mark - Initialization & disposal
+
++ (id)mySettingsWithName:(NSString *)name parent:(GBSettings *)parent {
+ id result = [self settingsWithName:name parent:parent];
+ if (result) {
+ [result registerArrayForKey:@"output"];
+ }
+ return result;
+}
+
+#pragma mark - Project information
+
+GB_SYNTHESIZE_COPY(NSString *, projectName, setProjectName, @"project-name")
+GB_SYNTHESIZE_COPY(NSString *, projectVersion, setProjectVersion, @"project-version")
+
+#pragma mark - Paths
+
+GB_SYNTHESIZE_OBJECT(NSArray *, inputPaths, setInputPaths, @"input")
+GB_SYNTHESIZE_OBJECT(NSArray *, outputPaths, setOutputPaths, @"output")
+
+#pragma mark - Debugging aid
+
+GB_SYNTHESIZE_BOOL(printSettings, setPrintSettings, @"print-settings")
+
+@end
+
+#pragma mark -
+
+@implementation GBSettings (Helpers)
+
+- (void)applyFactoryDefaults {
+ self.projectVersion = @"1.0";
+ self.printSettings = NO;
+}
+
+@end
View
@@ -6,17 +6,83 @@
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
-#import <Foundation/Foundation.h>
+#import "GBSettings+Application.h"
+#import "GBCommandLineParser.h"
+#import "GBOptionsHelper.h"
-int main(int argc, const char * argv[])
-{
+GBOptionDefinition GBOptionDefinitions[] = {
+ { 0, nil, @"PROJECT INFO", GBOptionSeparator|GBOptionNoCmdLine },
+ { 'p', @"project-name", @"Project name", GBValueRequired },
+ { 'v', @"project-version", @"Project version", GBValueRequired },
+
+ { 0, nil, @"PATHS", GBOptionSeparator|GBOptionNoCmdLine },
+ { 'o', @"output", @"Output path, repeat for multiple paths", GBValueRequired },
+
+ { 0, nil, @"MISCELLANEOUS", GBOptionSeparator|GBOptionNoCmdLine },
+ { 0, @"print-settings", @"Print settings for current run", GBValueNone },
+ { 'v', @"version", @"Display version and exit", GBValueNone|GBOptionNoPrint },
+ { '?', @"help", @"Display this help and exit", GBValueNone|GBOptionNoPrint },
+
+ { 0, nil, nil, 0 }
+};
+int main(int argc, char * argv[]) {
@autoreleasepool {
-
- // insert code here...
- NSLog(@"Hello, World!");
-
+ // Initialize settings stack.
+ GBSettings *factoryDefaults = [GBSettings mySettingsWithName:@"Factory" parent:nil];
+ GBSettings *settings = [GBSettings mySettingsWithName:@"CmdLine" parent:factoryDefaults];
+
+ // Initialize options helper class.
+ GBOptionsHelper *options = [[GBOptionsHelper alloc] init];
+ options.applicationVersion = ^{ return @"1.0"; };
+ options.applicationBuild = ^{ return @"100"; };
+ options.printValuesHeader = ^{ return @"%APPNAME version %APPVERSION (build %APPBUILD)\n"; };
+ options.printValuesArgumentsHeader = ^{ return @"Running with arguments:\n"; };
+ options.printValuesOptionsHeader = ^{ return @"Running with options:\n"; };
+ options.printValuesFooter = ^{ return @"\nEnd of values print...\n"; };
+ options.printHelpHeader = ^{ return @"Usage %APPNAME [OPTIONS] <arguments separated by space>"; };
+ options.printHelpFooter = ^{ return @"\nSwitches that don't accept value can use negative form with --no-<name> prefix."; };
+ [options registerOptionsFromDefinitions:GBOptionDefinitions];
+
+ // Initialize command line parser and parse cmd line.
+ GBCommandLineParser *parser = [[GBCommandLineParser alloc] init];
+ [options registerOptionsToCommandLineParser:parser];
+ __block BOOL commandLineValid = YES;
+ __block BOOL finished = NO;
+ [parser parseOptionsWithArguments:argv count:argc block:^(GBParseFlags flags, NSString *option, id value, BOOL *stop) {
+ switch (flags) {
+ case GBParseFlagUnknownOption:
+ printf("Unknown command line option %s!\n", option.UTF8String);
+ commandLineValid = NO;
+ break;
+ case GBParseFlagMissingValue:
+ printf("Missing value for command line option %s!\n", option.UTF8String);
+ commandLineValid = NO;
+ break;
+ case GBParseFlagArgument:
+ [settings addArgument:value];
+ break;
+ case GBParseFlagOption:
+ if ([option isEqualToString:@"version"]) {
+ [options printVersion];
+ finished = YES;
+ *stop = YES;
+ } else if ([option isEqualToString:@"help"]) {
+ [options printHelp];
+ finished = YES;
+ *stop = YES;
+ } else {
+ [settings setObject:value forKey:option];
+ }
+ break;
+ }
+ }];
+ if (finished) return 0;
+ if (!commandLineValid) return 1;
+
+ // Apply factory defaults and print settings if necessary.
+ [factoryDefaults applyFactoryDefaults];
+ if (settings.printSettings) [options printValuesFromSettings:settings];
}
return 0;
}
-
Oops, something went wrong.

0 comments on commit 95d01a5

Please sign in to comment.