OptionsComponent.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Text;
  3. using Base;
  4. using CommandLine;
  5. namespace Model
  6. {
  7. public class Options
  8. {
  9. [Option("appType", Required = true, HelpText = "AppType: realm gate")]
  10. public string AppType { get; set; }
  11. [Option("name", Required = true, HelpText = "Name.")]
  12. public string Name { get; set; }
  13. [Option("protocol", Required = false, HelpText = "Protocol, tcp or udp.", DefaultValue = NetworkProtocol.UDP)]
  14. public NetworkProtocol Protocol { get; set; }
  15. [Option("host", Required = true, HelpText = "Host.")]
  16. public string Host { get; set; }
  17. [Option("port", Required = true, HelpText = "Port.")]
  18. public int Port { get; set; }
  19. [Option("gateHost", Required = false, HelpText = "GateHost.")]
  20. public string GateHost { get; set; }
  21. [Option("gatePort", Required = false, HelpText = "GatePort.")]
  22. public int GatePort { get; set; }
  23. [Option('v', HelpText = "Print details during execution.")]
  24. public bool Verbose { get; set; }
  25. [HelpOption]
  26. public string GetUsage()
  27. {
  28. // this without using CommandLine.Text
  29. StringBuilder usage = new StringBuilder();
  30. usage.AppendLine("Quickstart Application 1.0");
  31. usage.AppendLine("Read user manual for usage instructions...");
  32. return usage.ToString();
  33. }
  34. }
  35. [ObjectEvent]
  36. public class OptionsComponentEvent : ObjectEvent<OptionsComponent>, IAwake<string[]>
  37. {
  38. public void Awake(string[] args)
  39. {
  40. this.GetValue().Awake(args);
  41. }
  42. }
  43. public class OptionsComponent: Component
  44. {
  45. public Options Options = new Options();
  46. public void Awake(string[] args)
  47. {
  48. if (!Parser.Default.ParseArguments(args, this.Options))
  49. {
  50. throw new Exception($"命令行格式错误!");
  51. }
  52. }
  53. }
  54. }