private static string[] getSuggestions(string commandLine, IList <string> tokens) { CIterator <string> iter = new CIterator <string>(tokens); CCommand cmd = CRegistery.FindCommand(iter.Next()); if (cmd == null) { return(EMPTY_SUGGESTIONS); // no command found } while (iter.HasNext()) { string token = iter.Next(); int iterPos = iter.Position; // store position to revert for the case if option skip fails // first try to parse options if (token.StartsWith("--")) // long option { string optionName = token.Substring(2); if (SkipOption(iter, cmd, optionName)) { continue; } iter.Position = iterPos; return(getSuggestedOptions(iter, cmd, optionName, "--")); } else if (token.StartsWith("-") && !CStringUtils.IsNumeric(token)) // short option { string optionName = token.Substring(1); if (SkipOption(iter, cmd, optionName)) { continue; } iter.Position = iterPos; return(getSuggestedOptions(iter, cmd, optionName, "-")); } if (iter.HasNext()) { return(EMPTY_SUGGESTIONS); // TODO: add multiple args suggestion support } return(getSuggestedArgs(commandLine, cmd, token)); } return(getSuggestedArgs(commandLine, cmd, "")); }
private static bool SkipOption(CIterator <string> iter, Option opt) { Type type = opt.Target.FieldType; if (type.IsArray) { Array arr = (Array)opt.Target.GetValue(opt); if (arr != null) { int index; int length = arr.Length; for (index = 0; index < length && iter.HasNext(); ++index) { string value = iter.Next(); if (!IsValidOptionString(value)) { return(false); } } return(index == length); } return(false); } if (type == typeof(int) || type == typeof(float) || type == typeof(string)) { if (iter.HasNext()) { string value = iter.Next(); return(IsValidOptionString(value)); } return(false); } if (type == typeof(bool)) { return(true); } return(false); }
static void Main(string[] args) { note note = new note("Naslov", "text, tijelo poruke"); note.Show(); StringBuilder sbTitle = new StringBuilder("Naslov p"); StringBuilder sbBody = new StringBuilder("Text pp0"); Notebook notebook = new Notebook(); for (int i = 0; i < 8; i++) { sbTitle.Append('w', i / 2); sbBody.Append('b', i / 3); notebook.AddNote(new note(sbTitle.ToString(), sbBody.ToString())); } CIterator iterator = new CIterator(notebook); iterator.First().Show(); while (iterator.Next() != null) { iterator.Current.Show(); } notebook.Clear(); CareTaker careTaker = new CareTaker(); ToDoItem tdi1 = new ToDoItem("naslov1", "text1", DateTime.Now); ToDoItem tdi2 = new ToDoItem("naslov12", "text12", DateTime.Now); ToDoItem tdi3 = new ToDoItem("naslov123", "text123", DateTime.MinValue); ToDoItem tdi4 = new ToDoItem("naslov1234", "text1234", DateTime.MaxValue); tdi1.AddStateCT(careTaker); tdi2.AddStateCT(careTaker); tdi3.AddStateCT(careTaker); tdi4.AddStateCT(careTaker); ToDoItem getStateTdi = new ToDoItem("", "", DateTime.MinValue); getStateTdi.RestoreState(careTaker.GetByTitle("naslov12")); Console.WriteLine(getStateTdi.ToString()); HomeThermostat termostat1 = new HomeThermostat(); HomeThermostat termostat2 = new HomeThermostat(); CarThermostat termostatAuto = new CarThermostat(); WeatherStation station = new WeatherStation(44); station.AddObserver(termostat1); station.AddObserver(termostat2); station.AddObserver(termostatAuto); station.SetTemperature(22); station.SetTemperature(2); }
private static string[] getSuggestedOptions(CIterator <string> iter, CCommand cmd, string optNameToken, string prefix) { List <Option> optionsList = new List <Option>(); // TODO: reuse list // list options bool useShort = prefix.Equals("-"); if (useShort) { cmd.ListShortOptions(optionsList, optNameToken); optionsList.Sort(delegate(Option op1, Option op2) { return(op1.ShortName.CompareTo(op2.ShortName)); }); } else { cmd.ListOptions(optionsList, optNameToken); optionsList.Sort(delegate(Option op1, Option op2) { return(op1.Name.CompareTo(op2.Name)); }); } if (optionsList.Count > 1) // multiple options available { return(getSuggestedOptions(optionsList, useShort)); } if (optionsList.Count == 1) // single option available { Option opt = optionsList[0]; if (isOptionNameMatch(opt, optNameToken, useShort)) // option name already matched - try values { if (opt.HasValues()) // option has predefined values? { if (iter.HasNext()) // has value token? { return(opt.ListValues(iter.Next())); } return(opt.Values); } if (iter.HasNext()) { return(EMPTY_SUGGESTIONS); // don't suggest option value } } return(singleSuggestion(getSuggestedOption(opt, useShort))); // suggest option`s name } return(EMPTY_SUGGESTIONS); }
public void TestSkipMultiple() { List <string> list = new List <string>(); list.Add("1"); list.Add("2"); list.Add("3"); CIterator <string> iter = new CIterator <string>(list); iter.Skip(2); Assert.IsTrue(iter.HasNext()); Assert.AreEqual(list[2], iter.Next()); }
public void TestForwardIteration() { List <string> list = new List <string>(); list.Add("1"); list.Add("2"); list.Add("3"); int index = 0; CIterator <string> iter = new CIterator <string>(list); while (iter.HasNext()) { Assert.AreEqual(list[index++], iter.Next()); } Assert.AreEqual(list.Count, index); }
public void TestSkipSomeElements() { List <string> list = new List <string>(); list.Add("1"); list.Add("2"); list.Add("3"); CIterator <string> iter = new CIterator <string>(list); Assert.IsTrue(iter.HasNext()); iter.Skip(); Assert.IsTrue(iter.HasNext()); Assert.AreEqual(list [1], iter.Next()); Assert.IsTrue(iter.HasNext()); iter.Skip(); Assert.IsFalse(iter.HasNext()); }
private bool SkipOption(CIterator <string> iter, Option opt) { Type type = opt.Target.FieldType; if (type.IsArray) { Array arr = (Array)opt.Target.GetValue(this); if (arr != null) { int length = arr.Length; Type elementType = arr.GetType().GetElementType(); int index = 0; if (iter.HasNext() && index++ < length) { string value = iter.Next(); if (!Option.IsValidValue(elementType, value)) { if (!iter.HasNext()) // this was the last arg { return(false); } throw new CCommandParseException("'{0}' is an invalid value for the array option '{1}'", value, opt.Name); } } return(index == length); } return(false); } if (type == typeof(int) || type == typeof(float) || type == typeof(string)) { if (iter.HasNext()) { string value = iter.Next(); if (!opt.IsValidValue(value)) { if (!iter.HasNext()) // this was the last arg { return(false); } throw new CCommandParseException("'{0}' is an invalid value for the option '{1}'", value, opt.Name); } return(true); } return(false); } if (type == typeof(bool)) { return(true); } return(false); }
private bool ExecuteGuarded(IList <string> tokens, string commandLine = null) { ResetOptions(); CIterator <string> iter = new CIterator <string>(tokens); iter.Next(); // first token is a command name if (this.IsManualMode) { PrintPrompt(commandLine); } if (this.IsPlayModeOnly && !CRuntime.IsPlaying) { PrintError("Command is available in the play mode only"); return(false); } List <string> argsList = new List <string>(); while (iter.HasNext()) { string token = CStringUtils.UnArg(iter.Next()); // first, try to parse options if (!TryParseOption(iter, token)) { // consume the rest of the args argsList.Add(token); while (iter.HasNext()) { token = CStringUtils.UnArg(iter.Next()); argsList.Add(token); } break; } } if (m_values != null) { if (argsList.Count != 1) { PrintError("Unexpected arguments count {0}", argsList.Count); PrintUsage(); return(false); } string arg = argsList[0]; if (Array.IndexOf(m_values, arg) == -1) { PrintError("Unexpected argument '{0}'", arg); PrintUsage(); return(false); } } if (m_options != null) { for (int i = 0; i < m_options.Count; ++i) { Option opt = m_options[i]; if (opt.IsRequired && !opt.IsHandled) { PrintError("Missing required option --{0}{1}", opt.Name, opt.ShortName != null ? "(-" + opt.ShortName + ")" : ""); PrintUsage(); return(false); } } } string[] args = argsList.Count > 0 ? argsList.ToArray() : EMPTY_COMMAND_ARGS; MethodInfo executeMethod = resolveExecuteMethod(args); if (executeMethod == null) { PrintError("Wrong arguments"); PrintUsage(); return(false); } return(CCommandUtils.Invoke(this, executeMethod, args)); }