Add.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace TodoList
  7. {
  8. [Command(
  9. Name: "add", // Todo: Support synonyms.
  10. ShortDescription: "Add a new todo task.",
  11. ErrorText: "",
  12. LongHelpText: "Add a new todo task, with the passed description. The description is not optional. The new task will be a child of the root."
  13. )]
  14. internal class Add : ICommand
  15. {
  16. [DefaultSwitch(0), GreedyArgument] public String desc = null;
  17. public UInt32 sub = 0;
  18. [SwitchDocumentation("Path to task file.")]
  19. public string file = "todo.txt";
  20. public void Invoke(Dictionary<String, Object> PipedArguments)
  21. {
  22. if (String.IsNullOrEmpty(file))
  23. {
  24. Console.WriteLine("No file specified. How did you manage that? It defaults to todo.txt");
  25. return;
  26. }
  27. if (String.IsNullOrEmpty(desc))
  28. throw new InvalidOperationException("You need to specify what you're adding dumbass.");
  29. var list = EntryList.LoadFile(file, true);
  30. var parent = list.Root.FindChildWithID(sub);
  31. if (parent == null)
  32. {
  33. Console.WriteLine("No entry with id {0} found.", sub);
  34. return;
  35. }
  36. var entry = new Entry
  37. {
  38. ID = list.NextID,
  39. Status = "-",
  40. Priority = 0,
  41. Description = desc
  42. };
  43. parent.Children.Add(entry);
  44. PipedArguments["id"] = entry.ID;
  45. list.NextID += 1;
  46. EntryList.SaveFile(file, list);
  47. Presentation.OutputEntry(entry, null, 0);
  48. }
  49. }
  50. }