示例#1
0
        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);
        }
示例#2
0
        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);
        }