/core/externals/update-engine/Samples/EngineRunner/ERCommandRunner.m

http://macfuse.googlecode.com/ · Objective C · 187 lines · 106 code · 54 blank · 27 comment · 13 complexity · ebb8888b21af3e18a699cd5aef0ede98 MD5 · raw file

  1. // Copyright 2008 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "ERCommandRunner.h"
  15. #import "ERCommand.h"
  16. #import "ERUtilities.h"
  17. @interface ERCommandRunner (PrivateMethods)
  18. // Given a command and a dictionary of required arguments that are missing
  19. // from our |parameters_|, print out the argument name and help blurb to
  20. // standard out.
  21. - (void)printMissingArgs:(NSDictionary *)missingArgs
  22. forCommand:(ERCommand *)command;
  23. // Given an argument dictionary (argument name as the key, help blurb as
  24. // the value), print it to standard out.
  25. - (void)printArgDictionary:(NSDictionary *)args;
  26. @end // PrivateMethods
  27. @implementation ERCommandRunner
  28. - (id)init {
  29. NSDictionary *defaults = [[NSUserDefaults standardUserDefaults]
  30. dictionaryRepresentation];
  31. return [self initWithParameters:defaults];
  32. } // init
  33. - (id)initWithParameters:(NSDictionary *)parameters {
  34. if ((self = [super init])) {
  35. parameters_ = [parameters retain];
  36. commands_ = [[NSMutableDictionary alloc] init];
  37. }
  38. if (parameters_ == nil) {
  39. [self release];
  40. return nil;
  41. }
  42. return self;
  43. } // initWithParameters
  44. - (void)dealloc {
  45. [parameters_ release];
  46. [commands_ release];
  47. [super dealloc];
  48. } // dealloc
  49. - (void)registerCommand:(ERCommand *)command {
  50. NSString *commandName = [command name];
  51. [commands_ setObject:command forKey:commandName];
  52. } // registerCommand
  53. - (void)printMissingArgs:(NSDictionary *)missingArgs
  54. forCommand:(ERCommand *)command {
  55. fprintf(stdout, "Command '%s' is missing some arguments:\n",
  56. [[command name] UTF8String]);
  57. NSEnumerator *enumerator = [missingArgs keyEnumerator];
  58. NSString *arg;
  59. while ((arg = [enumerator nextObject])) {
  60. NSString *description = [missingArgs objectForKey:arg];
  61. fprintf(stdout, " %s : %s\n", [arg UTF8String], [description UTF8String]);
  62. }
  63. } // printMissingArgs
  64. - (ERCommandResult)runCommandNamed:(NSString *)commandName {
  65. // Make sure we actually have this command.
  66. ERCommand *command = [commands_ objectForKey:commandName];
  67. if (command == nil) return kERCommandNotFound;
  68. // Make sure all of the required arguments for this command
  69. // can be found. Start out with a copy of the command's
  70. // required arguments and remove what's in our |parameters_|. If there's
  71. // anything left over then the caller didn't provide everything that's
  72. // needed.
  73. NSMutableDictionary *missingArgs =
  74. [[[command requiredArguments] mutableCopy] autorelease];;
  75. [missingArgs removeObjectsForKeys:[parameters_ allKeys]];
  76. // Missing arguments. Give the smack-down.
  77. if ([missingArgs count] > 0) {
  78. [self printMissingArgs:missingArgs forCommand:command];
  79. return kERMissingRequiredArguments;
  80. }
  81. // Run the command.
  82. BOOL result = [command runWithArguments:parameters_];
  83. return (result) ? kERCommandResultOK : kERCommandCompletionFailure;;
  84. } // run
  85. - (void)printUsage {
  86. fprintf(stdout, "EngineRunner version '%s' supports these commands:\n",
  87. [CONVERT_SYMBOL_TO_NSSTRING(UPDATE_ENGINE_VERSION) UTF8String]);
  88. NSArray *commandNames =
  89. [[commands_ allKeys] sortedArrayUsingSelector:@selector(compare:)];
  90. NSEnumerator *nameEnumerator = [commandNames objectEnumerator];
  91. NSString *commandName;
  92. while ((commandName = [nameEnumerator nextObject])) {
  93. ERCommand *command = [commands_ objectForKey:commandName];
  94. fprintf(stdout, " %s : %s\n",
  95. [commandName UTF8String],
  96. [[command blurb] UTF8String]);
  97. }
  98. fprintf(stdout,
  99. "Run 'EngineRunner cmdName -help' for help on a single command\n");
  100. } // printUsage
  101. - (void)printArgDictionary:(NSDictionary *)args {
  102. NSArray *argNames =
  103. [[args allKeys] sortedArrayUsingSelector:@selector(compare:)];
  104. NSEnumerator *argEnumerator = [argNames objectEnumerator];
  105. NSString *arg;
  106. while ((arg = [argEnumerator nextObject])) {
  107. NSString *description = [args objectForKey:arg];
  108. fprintf(stdout, " -%s : %s\n",
  109. [arg UTF8String], [description UTF8String]);
  110. }
  111. } // printArgDictionary
  112. - (void)printUsageForCommandName:(NSString *)commandName {
  113. ERCommand *command = [commands_ objectForKey:commandName];
  114. // If the command is unknown, blort out our usage.
  115. if (command == nil) {
  116. [self printUsage];
  117. return;
  118. }
  119. fprintf(stdout, "%s : %s\n",
  120. [[command name] UTF8String], [[command blurb] UTF8String]);
  121. NSDictionary *args;
  122. args = [command requiredArguments];
  123. if ([args count] > 0) {
  124. fprintf(stdout, " Required arguments:\n");
  125. [self printArgDictionary:args];
  126. }
  127. args = [command optionalArguments];
  128. if ([args count] > 0) {
  129. fprintf(stdout, " Optional arguments:\n");
  130. [self printArgDictionary:args];
  131. }
  132. } // printUsageForCommandName
  133. @end // ERCommandRunner