예제 #1
0
 internal OptionProcessingHelper(Options options, ArgumentDictionary arguments)
 {
     _parent    = options;
     _arguments = arguments;
 }
예제 #2
0
        private Options(ArgumentDictionary arguments)
        {
            OptionProcessingHelper optionProcessor = new OptionProcessingHelper(this, arguments);

            optionProcessor.ProcessArguments();
        }
        internal static ArgumentDictionary ParseCommand(string[] cmd, CommandSwitch[] switches)
        {
            ArgumentDictionary arguments;   //switches/values from cmd line

            string        arg;              //argument to test next
            CommandSwitch argSwitch;        //switch corresponding to that argument
            string        argValue;         //value corresponding to that argument
            int           delim;            //location of value delimiter (':' or '=')

            arguments = new ArgumentDictionary(cmd.Length);
            foreach (string s in cmd)
            {
                arg = s;
                bool argIsFlag = true;

                //if argument does not start with switch indicator, place into "default" arguments
                if (arg[0] != '-')
                {
                    arguments.Add(String.Empty, arg);
                    continue;
                }

                //if we have something which begins with '--' or '-', throw if nothing after it
                if (arg == "-" || arg == "--")
                {
                    throw new ArgumentException(SR.Format(SR.ErrSwitchMissing, arg));
                }

                //yank switch indicator ('--' or '-') off of command argument
                if (arg[1] != '-')
                {
                    arg = arg.Substring(1);
                }
                else
                {
                    arg = arg.Substring(2);
                }

                //check to make sure delimiter does not start off switch
                delim = arg.IndexOfAny(new char[] { ':', '=' });
                if (delim == 0)
                {
                    throw new ArgumentException(SR.Format(SR.ErrUnexpectedDelimiter));
                }

                //if there is no value, than create a null string
                if (delim == (-1))
                {
                    argValue = String.Empty;
                }
                else
                {
                    //assume valid argument now; must remove value attached to it
                    //must avoid copying delimeter into either arguments
                    argValue  = arg.Substring(delim + 1);
                    arg       = arg.Substring(0, delim);
                    argIsFlag = false;
                }

                //check if this switch exists in the list of possible switches
                //if no match found, then throw an exception
                argSwitch = CommandSwitch.FindSwitch(arg.ToLower(CultureInfo.InvariantCulture), switches);
                if (argSwitch == null)
                {
                    // Paths start with "/" on Unix, so the arg could potentially be a path.
                    // If we didn't find any matched option, check and see if it's a path.
                    string potentialPath = "/" + arg;
                    if (File.Exists(potentialPath))
                    {
                        arguments.Add(string.Empty, potentialPath);
                        continue;
                    }

                    throw new ArgumentException(SR.Format(SR.ErrUnknownSwitch, arg.ToLower(CultureInfo.InvariantCulture)));
                }

                //check if switch is allowed to have a value
                // if not and a value has been specified, then thrown an exception
                if (argSwitch.SwitchType == SwitchType.Flag)
                {
                    if (!argIsFlag)
                    {
                        throw new ArgumentException(SR.Format(SR.ErrUnexpectedValue, arg.ToLower(CultureInfo.InvariantCulture)));
                    }
                }
                else
                {
                    if (argIsFlag)
                    {
                        throw new ArgumentException(SR.Format(SR.ErrExpectedValue, arg.ToLower(CultureInfo.InvariantCulture)));
                    }
                }

                //check if switch is allowed to be specified multiple times
                // if not and it has already been specified and a new value has been paresd, throw an exception
                if (argSwitch.SwitchType != SwitchType.ValueList && arguments.ContainsArgument(argSwitch.Name))
                {
                    throw new ArgumentException(SR.Format(SR.ErrSingleUseSwitch, arg.ToLower(CultureInfo.InvariantCulture)));
                }
                else
                {
                    arguments.Add(argSwitch.Name, argValue);
                }
            }

            return(arguments);
        }