项目作者: NtreevSoft

项目描述 :
Command Line Parser for .Net
高级语言: C#
项目地址: git://github.com/NtreevSoft/CommandLineParser.git
创建时间: 2011-09-28T11:01:13Z
项目社区:https://github.com/NtreevSoft/CommandLineParser

开源协议:Other

下载


===========================================

NuGet version (Ntreev.Library.Commands)

Example

Parse

명령구문을 분석해 미리 정의된 속성에 값을 설정하는 기능입니다.

명령구문에 포함시킬 속성은 CommandPropertyAttribute로 설정합니다.

이름은 kebab-case (spinal-case, Train-Case, Lisp-case) 형태로 설정되며 CommandPropertyAttribute 생성자에 이름을 명시적으로 설정할 수 있습니다.

해당 속성에 대한 요약은 SummaryAttribute를 작성할 수 있고 좀더 자세한 설명이 필요할때는 DescriptionAttribute를 통해서 작성할 수 있습니다.

명령 구문에 특정 속성이 반드시 필요할때는 IsRequired를 true로 설정하면 됩니다.

IsRequired 가 설정된 속성은 명령구문에서 스위치값을 생략할 수 있으며 그 외에 속성은 —[이름] [변수] 또는 -[짧은이름] [변수] 형태여야 합니다.

  1. using System;
  2. using Ntreev.Library.Commands;
  3. using System.ComponentModel;
  4. namespace ConsoleApp
  5. {
  6. class Settings
  7. {
  8. [CommandProperty("param1", IsRequired = true)]
  9. [Description("parameter1 description")]
  10. public string Parameter1
  11. {
  12. get; set;
  13. }
  14. [CommandProperty("param2", IsRequired = true)]
  15. [Description("parameter2 description")]
  16. public int Parameter2
  17. {
  18. get; set;
  19. }
  20. [CommandProperty('o')]
  21. [Description("option1 description")]
  22. public bool Option1
  23. {
  24. get; set;
  25. }
  26. [CommandProperty("text-option")]
  27. [Description("option2 description")]
  28. public string Option2
  29. {
  30. get; set;
  31. }
  32. }
  33. class Program
  34. {
  35. static void Main(string[] args)
  36. {
  37. var settings = new Settings();
  38. var parser = new CommandLineParser(settings);
  39. try
  40. {
  41. if (parser.Parse(Environment.CommandLine) == false)
  42. {
  43. Environment.Exit(1);
  44. }
  45. // todo
  46. }
  47. catch (Exception e)
  48. {
  49. Console.Error.WriteLine(e);
  50. Environment.Exit(2);
  51. }
  52. }
  53. }
  54. }

You can call like this in console:

  1. Example.exe help
  2. Example.exe --version
  3. Example.exe text1 123
  4. Example.exe text1 123 -o --text-option "text string"

Invoke

명령구문을 분석해 미리 정의된 메소드를 호출하는 기능입니다.

명령구문에 포함시킬 메소드는 CommandMethodAttribute로 설정합니다.

설명 작성에 대한 부분은 Parsing 부분에서 다룬것과 동일합니다.

기본적으로 메소드의 인자는 IsRequierd가 자동으로 설정되며 추가적으로 선택 인자를 사용할때는 CommandMethodPropertyAttribute를 사용해 속성의 이름을 설정하면 됩니다.

  1. using System;
  2. using Ntreev.Library.Commands;
  3. using System.ComponentModel;
  4. namespace ConsoleApp
  5. {
  6. class Commands
  7. {
  8. [CommandMethod("method1")]
  9. public void Method1(string arg)
  10. {
  11. }
  12. [CommandMethod]
  13. public void Method2(string arg)
  14. {
  15. }
  16. [CommandMethod]
  17. [CommandMethodProperty(nameof(Message))]
  18. public void Method3(string arg0, string arg1 = null)
  19. {
  20. }
  21. [CommandProperty('m')]
  22. [DefaultValue("")]
  23. public string Message
  24. {
  25. get; set;
  26. }
  27. }
  28. class Program
  29. {
  30. static void Main(string[] args)
  31. {
  32. var commands = new Commands();
  33. var parser = new CommandLineParser(commands);
  34. try
  35. {
  36. if (parser.Invoke(Environment.CommandLine) == true)
  37. {
  38. Environment.Exit(1);
  39. }
  40. // todo
  41. }
  42. catch (Exception e)
  43. {
  44. Console.Error.WriteLine(e.Message);
  45. Environment.Exit(2);
  46. }
  47. }
  48. }
  49. }

You can call like this in console:

  1. Example.exe help
  2. Example.exe --version
  3. Example.exe method1 123
  4. Example.exe method2 123
  5. Example.exe method3 1
  6. or
  7. Example.exe method3 1 2
  8. or
  9. Example.exe method3 1 2 -m "message"

그 밖의 기능

  • 공통으로 사용할 수 있는 static property
  • 공통으로 사용할 수 있는 static method
  • parse 과 invoke 기능을 합쳐 종합 적인 명령 체계를 나타내는 CommandContext
  • 런타임상에서 사용할 수 있는 터미널

License

Ntreev CommandLineParser for .Net
https://github.com/NtreevSoft/CommandLineParser

Released under the MIT License.

Copyright (c) 2010 Ntreev Soft co., Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.