项目作者: sunriax

项目描述 :
Library to read arguments from command line
高级语言: C#
项目地址: git://github.com/sunriax/argument.git
创建时间: 2020-10-09T07:53:41Z
项目社区:https://github.com/sunriax/argument

开源协议:GNU General Public License v3.0

下载


Version: 1.0 Release NuGet Build Status codecov License: GPL v3

Argument Reader

Description:

With Argument Reader command line arguments can be passed into a .net standard/core application. The standard project can handle 4 types of arguments:

  • Boolean
  • Strings (\)*
  • Integers (#)
  • Doubles (##)

Own argument types can be build with own classes. They need to inherit from the MarshalerLib. Libraries are getting loaded dynamically on startup. It is not necessary to recompile the complete solution if a new marshaler is added.


Installation

To install ArgumentReader it is possible to download necessary libraries [zip | tar.gz] or install the library via nuget.

  1. PM> Install-Package RaGae.Argument

After adding/installing the ArgumentsLib in a project it is necessary to add the required marshalers to a directory in your project or install via nuget. For installing the marshalers manually, it is possible to use the copy scripts in this repository to download the marshalers. Each file can also be downloaded directly (see Available Marshalers)).

Installation with download script

Windows

  1. C:\Users\...\Solution\Project> mkdir Marshaler
  2. # It is necessary to move the copy.bat script in that directory
  3. C:\Users\...\Solution\Project> mkdir Marshaler
  4. C:\Users\...\Solution\Project> cd Marshaler
  5. C:\Users\...\Solution\Project> copy.bat
  6. # ...
  7. #BooleanMarshalerLib
  8. Download [Y/N]? y
  9. #IntegerMarshalerLib
  10. Download [Y/N]? y
  11. #StringMarshalerLib
  12. Download [Y/N]? y
  13. #DoubleMarshalerLib
  14. Download [Y/N]? y
  15. #End of downloading
  16. C:\Users\...\Solution\Project>

Linux

  1. ~/Solution/Project/: mkdir Marshaler
  2. # It is necessary to move the copy.sh script in that directory
  3. ~/Solution/Project $: cd Marshaler
  4. ~/Solution/Project $: chmod 0700 ./copy.sh
  5. ~/Solution/Project $: ./copy.sh
  6. # ...
  7. #BooleanMarshalerLib
  8. Download [Y/N]? y
  9. #IntegerMarshalerLib
  10. Download [Y/N]? y
  11. #StringMarshalerLib
  12. Download [Y/N]? y
  13. #DoubleMarshalerLib
  14. Download [Y/N]? y
  15. #End of downloading
  16. ~/Solution/Project $:

Installed Marshalers

To copy the Marshalers to output folder setup the *.csproj file.

`.csproj`*

  1. <Project Sdk="Microsoft.NET.Sdk">
  2. // ...
  3. <ItemGroup>
  4. <LibraryFiles Include="$(ProjectDir)Marshaler\*" ></LibraryFiles>
  5. </ItemGroup>
  6. <Target Name="PostBuild" AfterTargets="PostBuildEvent">
  7. <Copy SourceFiles="@(LibraryFiles)" DestinationFolder="$(TargetDir)Marshaler" SkipUnchangedFiles="true" ></Copy>
  8. </Target>
  9. // ...
  10. </Project>

Directly download Marshalers (Standard)

Configuration setup after download or manual installation

  1. {
  2. "ReflectionConfig": [
  3. {
  4. "ReflectionPath": "Marshaler",
  5. "FileSpecifier": "*MarshalerLib.dll"
  6. }
  7. ]
  8. }

NuGet installation

Marshaler Downloads
Boolean Marshaler NuGet
String Marshaler NuGet
Integer Marshaler NuGet
Double Marshaler NuGet
  1. PM> Install-Package RaGae.Argument.BooleanMarshaler
  2. PM> Install-Package RaGae.Argument.StringMarshaler
  3. PM> Install-Package RaGae.Argument.IntegerMarshaler
  4. PM> Install-Package RaGae.Argument.DoubleMarshaler

Configuration setup after nuget installation

  1. {
  2. "ReflectionConfig": [
  3. {
  4. "Files": [
  5. "RaGae.ArgumentLib.BooleanMarshalerLib.dll",
  6. "RaGae.ArgumentLib.StringMarshalerLib.dll",
  7. "RaGae.ArgumentLib.IntegerMarshalerLib.dll",
  8. "RaGae.ArgumentLib.DoubleMarshalerLib.dll"
  9. ]
  10. }
  11. ]
  12. }

An example project howto use the ArgumentReader can be found within this repository in the ReadArgument project


Structure

Initialize with schema in constructor

  1. IEnumerable<ArgumentSchema> schema = new List<ArgumentSchema>()
  2. {
  3. new ArgumentSchema()
  4. {
  5. Argument = new List<string>()
  6. {
  7. "string",
  8. "text",
  9. "data"
  10. },
  11. Marshaler = "*",
  12. Required = true
  13. }
  14. // ...
  15. };
  16. Argument argument = new Argument("ArgumentLib.json", "Arguments from command line", schema);

ArgumentLib.json

  1. {
  2. "ReflectionConfig": [
  3. {
  4. "ReflectionPath": "Marshaler",
  5. "FileSpecifier": "*MarshalerLib.dll"
  6. }
  7. ],
  8. "ArgumentConfig": {
  9. "Delimiter": "-:/"
  10. }
  11. }

Initialize with schema in *.json file

  1. Argument argument = new Argument("ArgumentLib.json", "Arguments from command line");

ArgumentsLib.json

  1. {
  2. "ReflectionConfig": [
  3. {
  4. "ReflectionPath": "Marshaler",
  5. "FileSpecifier": "*MarshalerLib.dll"
  6. }
  7. ],
  8. "ArgumentConfig": {
  9. "Schema": [
  10. {
  11. "Argument": [
  12. "string",
  13. "text",
  14. "data"
  15. ],
  16. "Marshaler": "*",
  17. "Required": true
  18. }
  19. ],
  20. "Delimiter": "-:/"
  21. }
  22. }

Arguments parameter

ConfigFile

Path to *.json file where the necessary parameters can be changed.

  1. Argument argument = new Argument("ArgumentLib.json", "...", "...");

Args[] from CLI

Arguments that are passed from the command line as array

  1. Program.exe -StRiNg "Test string" -string2 "Test string2" -InT 1234 -number2 5678 -DoUbLe 123,456 -decimal2 456,123 -BoOl
  1. string[] args = {
  2. "-StRiNg",
  3. "Test string"
  4. "-string2",
  5. "Test string2",
  6. "-InT",
  7. "1234",
  8. "-number2",
  9. "5678",
  10. "-DoUbLe",
  11. "123,456",
  12. "-decimal2",
  13. "456,123",
  14. "-BoOl"
  15. };
  1. Argument argument = new Argument("...", args, "...");
  2. // or
  3. Argument argument = new Argument("...", args);

Schema

This parameter can be null.

  1. IEnumerable<ArgumentSchema> schema = new List<ArgumentSchema>()
  2. {
  3. new ArgumentSchema()
  4. {
  5. Argument = new List<string>()
  6. {
  7. "string",
  8. "text",
  9. "data"
  10. },
  11. Marshaler = "*",
  12. Required = true
  13. }
  14. // ...
  15. };
  16. Argument argument = new Argument("...", "...", schema);

If null configuration is necessary in ArgumentsLib.json file

  1. Argument argument = new Argument("...", "...");

*ArgumentsLib.json*

  1. {
  2. "ArgumentConfig": {
  3. "Schema": [
  4. {
  5. "Argument": [
  6. "string",
  7. "text",
  8. "data"
  9. ],
  10. "Marshaler": "*",
  11. "Required": true
  12. }
  13. ]
  14. }
  15. }

Parse Arguments


Build your own Marshaler

  1. Create a new VisualStudio .NET Standard Classlibrary (??MarshalerLib)
  2. Link a new project reference to RaGae.ArgumentLib.MarshalerLib.dll (in this repository) or install as nuget (see below)
  3. Write Marshaler (See example code below)
  4. Copy the TestMarshalerLib.dll to Marshaler directory in your executeable project
  5. Implement the ? in your schema
  1. PM> Install-Package RaGae.Argument.Marshaler
  1. using System;
  2. using RaGae.ArgumentLib.MarshalerLib;
  3. namespace RaGae.ArgumentLib.TestMarshalerLib
  4. {
  5. public class TestMarshalerLib : Marshaler
  6. {
  7. // Only schemas allowed that are not used (string.Empty, *, #, ## are already used from standard marshalers)
  8. public override string Schema => "?";
  9. public override void Set(Iterator<string> currentArgument)
  10. {
  11. try
  12. {
  13. // If implementation should use an argument behind the command (e.g. -a "??"),
  14. // it is necessary to move the Iterator to the next position.
  15. Value = currentArgument.Next();
  16. }
  17. catch (ArgumentOutOfRangeException)
  18. {
  19. throw new TestMarshalerException(ErrorCode.MISSING);
  20. }
  21. // If no argument behind the command is used just add your value
  22. Value = "This is my personal marshaler";
  23. }
  24. public class TestMarshalerException : BaseArgumentException
  25. {
  26. public TestMarshalerException(ErrorCode errorCode) : base(errorCode) { }
  27. public TestMarshalerException(ErrorCode errorCode, string message) : base(errorCode, message) { }
  28. public override string ErrorMessage()
  29. {
  30. switch (ErrorCode)
  31. {
  32. case ErrorCode.MISSING:
  33. return $"Could not find test parameter for -{base.ErrorArgumentId}";
  34. default:
  35. return string.Empty;
  36. }
  37. }
  38. }
  39. }
  40. }

References

The original Argument Marshaler was written in Java and published by Robert C. Martin in his book Clean Code. This project adapt his implementations and extends it dynamically.


R. Gächter