PageRenderTime 41ms CodeModel.GetById 17ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 15#import "ERCommandRunner.h"
 16
 17#import "ERCommand.h"
 18#import "ERUtilities.h"
 19
 20
 21@interface ERCommandRunner (PrivateMethods)
 22// Given a command and a dictionary of required arguments that are missing
 23// from our |parameters_|, print out the argument name and help blurb to
 24// standard out.
 25- (void)printMissingArgs:(NSDictionary *)missingArgs
 26              forCommand:(ERCommand *)command;
 27
 28// Given an argument dictionary (argument name as the key, help blurb as
 29// the value), print it to standard out.
 30- (void)printArgDictionary:(NSDictionary *)args;
 31@end  // PrivateMethods
 32
 33
 34@implementation ERCommandRunner
 35
 36- (id)init {
 37  NSDictionary *defaults = [[NSUserDefaults standardUserDefaults]
 38                             dictionaryRepresentation];
 39  return [self initWithParameters:defaults];
 40
 41}  // init
 42
 43
 44- (id)initWithParameters:(NSDictionary *)parameters {
 45
 46  if ((self = [super init])) {
 47    parameters_ = [parameters retain];
 48    commands_ = [[NSMutableDictionary alloc] init];
 49  }
 50
 51  if (parameters_ == nil) {
 52    [self release];
 53    return nil;
 54  }
 55
 56  return self;
 57
 58}  // initWithParameters
 59
 60
 61- (void)dealloc {
 62  [parameters_ release];
 63  [commands_ release];
 64
 65  [super dealloc];
 66
 67}  // dealloc
 68
 69
 70- (void)registerCommand:(ERCommand *)command {
 71  NSString *commandName = [command name];
 72  [commands_ setObject:command forKey:commandName];
 73
 74}  // registerCommand
 75
 76
 77- (void)printMissingArgs:(NSDictionary *)missingArgs 
 78              forCommand:(ERCommand *)command {
 79  fprintf(stdout, "Command '%s' is missing some arguments:\n",
 80          [[command name] UTF8String]);
 81
 82  NSEnumerator *enumerator = [missingArgs keyEnumerator];
 83  NSString *arg;
 84  while ((arg = [enumerator nextObject])) {
 85    NSString *description = [missingArgs objectForKey:arg];
 86    fprintf(stdout, "  %s : %s\n", [arg UTF8String], [description UTF8String]);
 87  }
 88
 89}  // printMissingArgs
 90
 91
 92- (ERCommandResult)runCommandNamed:(NSString *)commandName {
 93  // Make sure we actually have this command.
 94  ERCommand *command = [commands_ objectForKey:commandName];
 95  if (command == nil) return kERCommandNotFound;
 96
 97  // Make sure all of the required arguments for this command
 98  // can be found.  Start out with a copy of the command's
 99  // required arguments and remove what's in our |parameters_|.  If there's
100  // anything left over then the caller didn't provide everything that's
101  // needed.
102  NSMutableDictionary *missingArgs =
103    [[[command requiredArguments] mutableCopy] autorelease];;
104
105  [missingArgs removeObjectsForKeys:[parameters_ allKeys]];
106
107  // Missing arguments.  Give the smack-down.
108  if ([missingArgs count] > 0) {
109    [self printMissingArgs:missingArgs forCommand:command];
110    return kERMissingRequiredArguments;
111  }
112
113  // Run the command.
114  BOOL result = [command runWithArguments:parameters_];
115
116  return (result) ? kERCommandResultOK : kERCommandCompletionFailure;;
117
118}  // run
119
120
121- (void)printUsage {
122  fprintf(stdout, "EngineRunner version '%s' supports these commands:\n",
123          [CONVERT_SYMBOL_TO_NSSTRING(UPDATE_ENGINE_VERSION) UTF8String]);
124
125  NSArray *commandNames =
126    [[commands_ allKeys] sortedArrayUsingSelector:@selector(compare:)];
127
128  NSEnumerator *nameEnumerator = [commandNames objectEnumerator];
129
130  NSString *commandName;
131  while ((commandName = [nameEnumerator nextObject])) {
132    ERCommand *command = [commands_ objectForKey:commandName];
133    fprintf(stdout, "    %s : %s\n",
134            [commandName UTF8String],
135            [[command blurb] UTF8String]);
136  }
137
138  fprintf(stdout,
139          "Run 'EngineRunner cmdName -help' for help on a single command\n");
140
141}  // printUsage
142
143
144- (void)printArgDictionary:(NSDictionary *)args {
145  NSArray *argNames =
146    [[args allKeys] sortedArrayUsingSelector:@selector(compare:)];
147  NSEnumerator *argEnumerator = [argNames objectEnumerator];
148
149  NSString *arg;
150  while ((arg = [argEnumerator nextObject])) {
151    NSString *description = [args objectForKey:arg];
152    fprintf(stdout, "    -%s : %s\n",
153            [arg UTF8String], [description UTF8String]);
154  }
155
156}  // printArgDictionary
157
158
159- (void)printUsageForCommandName:(NSString *)commandName {
160  ERCommand *command = [commands_ objectForKey:commandName];
161
162  // If the command is unknown, blort out our usage.
163  if (command == nil) {
164    [self printUsage];
165    return;
166  }
167
168  fprintf(stdout, "%s : %s\n",
169          [[command name] UTF8String], [[command blurb] UTF8String]);
170
171  NSDictionary *args;
172
173  args = [command requiredArguments];
174  if ([args count] > 0) {
175    fprintf(stdout, "  Required arguments:\n");
176    [self printArgDictionary:args];
177  }
178
179  args = [command optionalArguments];
180  if ([args count] > 0) {
181    fprintf(stdout, "  Optional arguments:\n");
182    [self printArgDictionary:args];
183  }
184
185}  // printUsageForCommandName
186
187@end  // ERCommandRunner