Modify.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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: "mod",
  10. ShortDescription: "Change the description of a todo task.",
  11. ErrorText: "",
  12. LongHelpText: "Replaces the description of a todo task with the new description provided."
  13. )]
  14. internal class Modify : ICommand
  15. {
  16. [SwitchDocumentation("The ID of the task to modify.")]
  17. [DefaultSwitch(0)] public UInt32 id = 0;
  18. [DefaultSwitch(1), GreedyArgument] public String desc = null;
  19. [SwitchDocumentation("Path to task file.")]
  20. public string file = "todo.txt";
  21. public void Invoke(Dictionary<String, Object> PipedArguments)
  22. {
  23. if (String.IsNullOrEmpty(file))
  24. {
  25. Console.WriteLine("No file specified. How did you manage that? It defaults to todo.txt");
  26. return;
  27. }
  28. if (id == 0)
  29. {
  30. Console.WriteLine("You need to specify the entry you're editing.");
  31. return;
  32. }
  33. var list = EntryList.LoadFile(file, true);
  34. var entry = list.Root.FindChildWithID(id);
  35. if (entry == null)
  36. {
  37. Console.WriteLine("Could not find entry with ID{0}.", id);
  38. return;
  39. }
  40. if (String.IsNullOrEmpty(desc))
  41. throw new InvalidOperationException("You need to specify what you're changing it to dumbass.");
  42. entry.Description = desc;
  43. EntryList.SaveFile(file, list);
  44. Presentation.OutputEntry(entry, null, 0);
  45. }
  46. }
  47. }