Sagittarius 1.0.1
Sagittarius was deprecated, it was reworked and further optimized in Sharpify.CommandLineInterface, the new library will provide a more complete entire framework for creating clis, better performing and easier to use apis and more. To provide all the features the design of the usage is very different, so migrating large packages might not be very easy but still highly encouraged.
See the version list below for details.
dotnet add package Sagittarius --version 1.0.1
NuGet\Install-Package Sagittarius -Version 1.0.1
<PackageReference Include="Sagittarius" Version="1.0.1" />
paket add Sagittarius --version 1.0.1
#r "nuget: Sagittarius, 1.0.1"
// Install Sagittarius as a Cake Addin #addin nuget:?package=Sagittarius&version=1.0.1 // Install Sagittarius as a Cake Tool #tool nuget:?package=Sagittarius&version=1.0.1
Sagittarius ðđ
Sagittarius is a package created with the purpose of making it easier to parse command line arguments without using reflection. While there are many other packages that parse arguments, I have yet to see an easy to use one that doesn't use reflection. This feature is crucial when you want to be able to compile your cli's to native code, as we are now able with .NET 7.
Features
- ð High performance, Low memory usage and allocation
- ðŠķ Very lightweight (No external dependencies)
- ð Plug and play (most of the time you don't need to change your code much)
- â Trimming friendly (no reflection uses at all)
- Supports all platforms (Windows, Linux, Mac)
⎠Installation
Usage
Parser.Split()
is a static method that allows splitting all components of the arguments into a collection of string.
This is different from string.Split() as it is both more efficient, and respects quotes which allows you to put everything
in between quotes in a single argument.
Parser.ParseArguments()
is a static method that allows parsing the arguments into an Arguments
object that is a wrapper
over a Dictionary<string, string>
that allows very convenient access to the arguments.
Features of the Arguments
object
- Access to named arguments by name
- Access to positional arguments by index as int e.g: "0" for the first positional argument
- Conversion of arguments to
int
,double
ordecimal
with validation. - Validation of any value of an argument with custom
Func<string, bool> validationFunc
- Access to underlying
Dictionary<string, string>
withArguments.GetInnerDictionary()
- An option to lower the index of all positional arguments by 1 with
Arguments.ForwardPositionalArguments
which means "1" will now become "0" and what was "0" will be removed, this is very useful for commands that have sub-commands, and you want each sub-command to be implemented purely. - A validation method does not do anything if the argument is valid, but throws an exception if it is not.
- A conversion or retrieval method such as
Arguments.GetValue(key)
orArguments.GetValueAsInteger(key)
have an optional parameter namedthrowIfUnable
which is set tofalse
by default. when it is set tofalse
if any error arises, the value returned will be null. but if set totrue
, an exception will be thrown if any error arises, but if the value is guaranteed to be not null.
Usage tips and tricks
- If you want to a boolean arguments, as it is argument only but without value, such as
command --verbose
, you don't need to retrieve the value rather just useArguments.Validate("verbose")
this will throw an exception if the argument is not present. - If you want something that is optional and has a default value, you can use the
Arguments.GetValue(key)
without thethrowIfUnable
parameter to get the value only if present and valid, otherwise a null. Which means you can do something like this:var val = Arguments.GetValue("key") ?? "default";
. Same goes for all conversion variants ofGetValue
likeGetValueAsInteger
etc. - If you have required arguments, make sure you put at least the parsing or retrieving method in a try catch block, and handle the exception.
- If you get your args from the main method such like this:
Main(string[] args)
you can pass the args parameter directly toParser.ParseArguments(args)
. But make sure it is not empty. The result ofParser.ParseArguments(args)
can be nullable and that is only if the arguments that were passed are empty.
Source Code
from the point of public release, the master branch will only contain stable and tested code, so to get the source code you can clone the master branch.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.