private static int?ParseSwitchArg(IConsoleEnvironment consoleEnvironment, string arg, int valueIndex, ArgInfo switchArg) { Contract.Requires(consoleEnvironment != null); Contract.Requires(arg != null); Contract.Requires(switchArg != null); Contract.Requires(valueIndex < 0 || (valueIndex + 1) <= arg.Length); bool switchValue; if (valueIndex < 0) { switchValue = true; } else { string settingValueStr = arg.Substring(valueIndex + 1); if (!bool.TryParse(settingValueStr, out switchValue)) { consoleEnvironment.ErrWriter.WriteLine( @"Switch '{0}' has an invalid value ('{1}') - it should either be empty, 'true' or 'false'", switchArg.ArgName, settingValueStr); return(2); } } CommandLineParsingContext context = new CommandLineParsingContext(consoleEnvironment); context.ArgName = switchArg.ArgName; switchArg.BoolValueAction(switchValue, context); return(null); }
private int?ParsePositionalArg(IConsoleEnvironment consoleEnvironment, int positionalArgsCovered, string arg) { Contract.Requires(consoleEnvironment != null); Contract.Requires(positionalArgsCovered >= 0); ArgInfo positionalArg = positionalArgs[positionalArgsCovered]; CommandLineParsingContext context = new CommandLineParsingContext(consoleEnvironment); context.ArgName = positionalArg.ArgName; if (positionalArg.ArgType == typeof(string)) { positionalArg.StringValueAction(arg, context); } else if (positionalArg.ArgType == typeof(int)) { int intValue; if (int.TryParse(arg, out intValue)) { positionalArg.IntValueAction(intValue, context); } else { consoleEnvironment.ErrWriter.WriteLine("Argument '{0}' has an invalid value ('{1}') - it should be an integer", positionalArg.ArgName, arg); return(2); } } else { throw new NotImplementedException(); } return(null); }
private static int?ParseSettingArg(IConsoleEnvironment consoleEnvironment, string arg, int valueIndex, ArgInfo settingArg) { Contract.Requires(consoleEnvironment != null); Contract.Requires(arg != null); Contract.Requires(settingArg != null); Contract.Requires(valueIndex == -1 || (valueIndex + 1) >= 0); Contract.Requires(valueIndex == -1 || (valueIndex + 1) <= arg.Length); if (valueIndex == -1) { consoleEnvironment.ErrWriter.WriteLine("Setting '{0}' is missing the value", settingArg.ArgName); return(2); } string settingValueStr = arg.Substring(valueIndex + 1); CommandLineParsingContext context = new CommandLineParsingContext(consoleEnvironment); context.ArgName = settingArg.ArgName; if (settingArg.ArgType == typeof(string)) { settingArg.StringValueAction(settingValueStr, context); } else if (settingArg.ArgType == typeof(int)) { int intValue; if (int.TryParse(settingValueStr, out intValue)) { settingArg.IntValueAction(intValue, context); } else { consoleEnvironment.ErrWriter.WriteLine("Setting '{0}' has an invalid value ('{1}') - it should be an integer", settingArg.ArgName, settingValueStr); return(2); } } else { throw new NotImplementedException(); } return(null); }