Esempio n. 1
0
        public CommandLine(String[] aArgs, String[] aValidOpts)
        {
            int i, iArg, iOpt;

            // Keep a list of valid option names.
            m_sValidOptions = new Abbrevs(aValidOpts);

            // Temporary lists of raw arguments and options and their
            // associated values.
            String[] aArgList = new String[aArgs.Length];
            Option[] aOptList = new Option[aArgs.Length];

            // Reset counters of raw arguments and option/value pairs found
            // so far.
            iArg = 0;
            iOpt = 0;

            // Iterate through words of command line.
            for (i = 0; i < aArgs.Length; i++)
            {
                // Check for option or raw argument.
                if (aArgs[i].StartsWith("/") ||
                    aArgs[i].StartsWith("-"))
                {
                    String strOpt;
                    String strVal = null;
                    bool   bRequiresValue;
                    bool   bCanHaveValue;

                    // It's an option. Strip leading '/' or '-' and
                    // anything after a value separator (':' or
                    // '=').
                    int iColon = aArgs[i].IndexOfAny(new char[] { ':', '=' });
                    if (iColon == -1)
                    {
                        strOpt = aArgs[i].Substring(1);
                    }
                    else
                    {
                        strOpt = aArgs[i].Substring(1, iColon - 1);
                    }

                    // Look it up in the table of valid options (to
                    // check it exists, get the full option name and
                    // to see if an associated value is expected).
                    strOpt = m_sValidOptions.Lookup(strOpt, out bRequiresValue, out bCanHaveValue);

                    // Check that the user hasn't specified a value separator for an option
                    // that doesn't take a value.
                    if (!bCanHaveValue && (iColon != -1))
                    {
                        throw new TlbImpGeneralException(Resource.FormatString("Err_NoValueRequired", strOpt), ErrorCode.Err_NoValueRequired, true);
                    }

                    // Check that the user has put a colon if the option requires a value.
                    if (bRequiresValue && (iColon == -1))
                    {
                        throw new TlbImpGeneralException(Resource.FormatString("Err_ValueRequired", strOpt), ErrorCode.Err_ValueRequired, true);
                    }

                    // Go look for a value if there is one.
                    if (bCanHaveValue && iColon != -1)
                    {
                        if (iColon == (aArgs[i].Length - 1))
                        {
                            // No value separator, or
                            // separator is at end of
                            // option; look for value in
                            // next command line arg.
                            if (i + 1 == aArgs.Length)
                            {
                                throw new TlbImpGeneralException(Resource.FormatString("Err_ValueRequired", strOpt), ErrorCode.Err_ValueRequired, true);
                            }
                            else
                            {
                                if ((aArgs[i + 1].StartsWith("/") || aArgs[i + 1].StartsWith("-")))
                                {
                                    throw new TlbImpGeneralException(Resource.FormatString("Err_ValueRequired", strOpt), ErrorCode.Err_ValueRequired, true);
                                }

                                strVal = aArgs[i + 1];
                                i++;
                            }
                        }
                        else
                        {
                            // Value is in same command line
                            // arg as the option, substring
                            // it out.
                            strVal = aArgs[i].Substring(iColon + 1);
                        }
                    }

                    // Build the option value pair.
                    aOptList[iOpt++] = new Option(strOpt, strVal);
                }
                else
                {
                    // Command line word is a raw argument.
                    aArgList[iArg++] = aArgs[i];
                }
            }

            // Allocate the non-temporary arg and option lists at exactly
            // the right size.
            m_aArgList = new String[iArg];
            m_aOptList = new Option[iOpt];

            // Copy in the values we've calculated.
            Array.Copy(aArgList, m_aArgList, iArg);
            Array.Copy(aOptList, m_aOptList, iOpt);

            // Reset enumeration cursors to start of lists.
            m_iArgCursor = 0;
            m_iOptCursor = 0;
        }
Esempio n. 2
0
        public CommandLine(String[] aArgs, String[] aValidOpts)
        {
            // Keep a list of valid option names.
            m_sValidOptions = new Abbrevs(aValidOpts);

            // Temporary lists of raw arguments and options and their
            // associated values.
            var aArgList = new String[aArgs.Length];
            var aOptList = new Option[aArgs.Length];

            // Reset counters of raw arguments and option/value pairs found
            // so far.
            int iArg = 0;
            int iOpt = 0;

            // Iterate through words of command line.
            for (int i = 0; i < aArgs.Length; i++)
            {
            // Check for option or raw argument.
            if (aArgs[i].StartsWith("/") ||
                aArgs[i].StartsWith("-"))
            {
                // It's an option. Strip leading '/' or '-' and
                // anything after a value separator (':' or
                // '=').
                int iColon = aArgs[i].IndexOfAny(new char[] {':', '='});
                String strOpt;
                if (iColon == -1)
                        strOpt = aArgs[i].Substring(1);
                else
                        strOpt = aArgs[i].Substring(1, iColon - 1);

                // Look it up in the table of valid options (to
                // check it exists, get the full option name and
                // to see if an associated value is expected).
                bool bRequiresValue;
                bool bCanHaveValue;
                strOpt = m_sValidOptions.Lookup(strOpt, out bRequiresValue, out bCanHaveValue);

                // Check that the user hasn't specified a value separator for an option
                // that doesn't take a value.
                if (!bCanHaveValue && (iColon != -1))
                    throw new TlbImpGeneralException(Resource.FormatString("Err_NoValueRequired", strOpt), ErrorCode.Err_NoValueRequired, true);

                // Check that the user has put a colon if the option requires a value.
                if (bRequiresValue && (iColon == -1))
                    throw new TlbImpGeneralException(Resource.FormatString("Err_ValueRequired", strOpt), ErrorCode.Err_ValueRequired, true);

                String strVal = null;
                // Go look for a value if there is one.
                if (bCanHaveValue && iColon != -1)
                {
                    if (iColon == (aArgs[i].Length - 1))
                    {
                        // No value separator, or
                        // separator is at end of
                        // option; look for value in
                        // next command line arg.
                        if (i + 1 == aArgs.Length)
                        {
                            throw new TlbImpGeneralException(Resource.FormatString("Err_ValueRequired", strOpt), ErrorCode.Err_ValueRequired, true);
                        }
                        else
                        {
                            if ((aArgs[i + 1].StartsWith( "/" ) || aArgs[i + 1].StartsWith( "-" )))
                                throw new TlbImpGeneralException(Resource.FormatString("Err_ValueRequired", strOpt), ErrorCode.Err_ValueRequired, true);

                            strVal = aArgs[i+1];
                            i++;
                        }
                    }
                    else
                    {
                        // Value is in same command line
                        // arg as the option, substring
                        // it out.
                        strVal = aArgs[i].Substring(iColon + 1);
                    }
                }

                // Build the option value pair.
                aOptList[iOpt++] = new Option(strOpt, strVal);
            }
            else
            {
                // Command line word is a raw argument.
                aArgList[iArg++] = aArgs[i];
            }
            }

            // Allocate the non-temporary arg and option lists at exactly
            // the right size.
            m_aArgList = new String[iArg];
            m_aOptList = new Option[iOpt];

            // Copy in the values we've calculated.
            Array.Copy(aArgList, m_aArgList, iArg);
            Array.Copy(aOptList, m_aOptList, iOpt);

            // Reset enumeration cursors to start of lists.
            m_iArgCursor = 0;
            m_iOptCursor = 0;
        }