/** * Check for duplicates of an option. It is an error to have duplicates * unless appropriate flags is set in descriptor. * * @param arguments * the arguments */ private void CheckIncompatibilities(List <CLOption> arguments) { int size = arguments.Count; for (int i = 0; i < size; i++) { CLOption option = arguments[i]; int id = option.GetDescriptor().GetId(); CLOptionDescriptor descriptor = GetDescriptorFor(id); // this occurs when id == 0 and user has not supplied a descriptor // for arguments if (null == descriptor) { continue; } int[] incompatible = descriptor.GetIncompatible(); CheckIncompatible(arguments, incompatible, i); } }
private void CheckIncompatible(List <CLOption> arguments, int[] incompatible, int original) { int size = arguments.Count; for (int i = 0; i < size; i++) { if (original == i) { continue; } CLOption option = arguments[i]; int id = option.GetDescriptor().GetId(); for (int j = 0; j < incompatible.Length; j++) { if (id == incompatible[j]) { CLOption originalOption = arguments[original]; int originalId = originalOption.GetDescriptor().GetId(); String message = null; if (id == originalId) { message = "Duplicate options for " + DescribeDualOption(originalId) + " found."; } else { message = "Incompatible options -" + DescribeDualOption(id) + " and " + DescribeDualOption(originalId) + " found."; } throw new Exception(message); } } } }
private void AddOption(CLOption option) { m_options.Add(option); m_lastOptionId = option.GetDescriptor().GetId(); m_option = null; }
/** * Actually parse arguments */ private void Parse() { if (0 == m_args.Length) { return; } m_stringLength = m_args[m_argIndex].Length; while (true) { m_ch = PeekAtChar(); if (m_argIndex >= m_args.Length) { break; } if (null != m_control && m_control.isFinished(m_lastOptionId)) { // this may need mangling due to peeks m_unparsedArgs = SubArray(m_args, m_argIndex, m_stringIndex); return; } if (STATE_OPTION_MODE == m_state) { // if get to an arg barrier then return to normal mode // else continue accumulating options if (0 == m_ch) { GetChar(); // strip the null m_state = STATE_NORMAL; } else { ParseShortOption(); } } else if (STATE_NORMAL == m_state) { ParseNormal(); } else if (STATE_NO_OPTIONS == m_state) { // should never get to here when stringIndex != 0 AddOption(new CLOption(m_args[m_argIndex++])); } else { ParseArguments(); } } // Reached end of input arguments - perform final processing if (m_option != null) { if (STATE_OPTIONAL_ARG == m_state) { m_options.Add(m_option); } else if (STATE_REQUIRE_ARG == m_state) { CLOptionDescriptor descriptor = GetDescriptorFor(m_option.GetDescriptor().GetId()); String message = "Missing argument to option " + GetOptionDescription(descriptor); throw new Exception(message); } else if (STATE_REQUIRE_2ARGS == m_state) { if (1 == m_option.GetArgumentCount()) { m_option.AddArgument(""); m_options.Add(m_option); } else { CLOptionDescriptor descriptor = GetDescriptorFor(m_option.GetDescriptor().GetId()); String message = "Missing argument to option " + GetOptionDescription(descriptor); throw new Exception(message); } } else { throw new Exception("IllegalState " + m_state + ": " + m_option); } } }