NFlags 1.1.0-rc

This is a prerelease version of NFlags.
There is a newer version of this package available.
See the version list below for details.
dotnet add package NFlags --version 1.1.0-rc
NuGet\Install-Package NFlags -Version 1.1.0-rc
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NFlags" Version="1.1.0-rc" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NFlags --version 1.1.0-rc
#r "nuget: NFlags, 1.1.0-rc"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NFlags as a Cake Addin
#addin nuget:?package=NFlags&version=1.1.0-rc&prerelease

// Install NFlags as a Cake Tool
#tool nuget:?package=NFlags&version=1.1.0-rc&prerelease

NFlags

Simple library to made parsing CLI arguments easy. Library allows to print usage help "out of box".

For example of usage check Examples directory.

QuickStart

  1. Install NFLags from NuGet.
  2. Start new console project.
  3. Configure NFLags:
NFlags.Configure(configure => configure
    .SetDialect(Dialect.Gnu)
    .SetName("QuickStart")
    .SetDescription("This is NFlags")
).
Root(rc => rc.
    RegisterFlag("flag1", "f", "Flag description", false).
    RegisterOption("option", "o", "Option description", "optionDefaultValue").
    RegisterParam("param", "Param description", "ParamDefaultValue").
    RegisterSubcommand("subcommand", "Subcommand Description", sc => sc.
            SetExecute((commandArgs, output) => output("This is subcommand: " + commandArgs.Parameters["SubParameter"])).
            RegisterParam("SubParameter", "SubParameter description", "SubParameterValue")
    ).
    SetExecute((commandArgs, output) => output("This is root command: " + commandArgs.Parameters["param"]))
).
Run(args);

Run application and enjoy:

$> dotnet NFlags.QuickStart.dll
This is root command: ParamDefaultValue%
$> dotnet NFlags.QuickStart.dll xxx
This is root command: xxx
$> dotnet NFlags.QuickStart.dll --help
Usage:
        QuickStart [COMMAND] [FLAGS]... [OPTIONS]... [PARAMETERS]...

This is NFlags

        Commands:
        subcommand      Subcommand Description

        Flags:
        --flag1, -f     Flag description
        --help, -h      Prints this help

        Options:
        --option <option>, -o <option>  Option description

        Parameters:
        <param> Param description

$> dotnet NFlags.QuickStart.dll subcommand
This is subcommand: SubParameterValue
$> dotnet NFlags.QuickStart.dll subcommand yyy
This is subcommand: yyy
$> dotnet NFlags.QuickStart.dll subcommand --help
Usage:
        QuickStart [FLAGS]... [PARAMETERS]...

This is NFlags

        Flags:
        --help, -h      Prints this help

        Parameters:
        <SubParameter>  SubParameter description

$> 

Basics

Global NFlags Configuration

Set app name

Name set with folowing code, will be printed in help. By default AppDomain.CurrentDomain.FriendlyName is used.

NFlags.Configure(configurator => configurator.SetName("Custom Name"));
Set app description

Description set with folowing code, will be printed in help.

NFlags.Configure(configurator => configurator.SetDescription("App description"));
Set output

Output action set by this method is used to produce output. Default Console.Write is used.

NFlags.Configure(configurator => configurator.SetOutpu(Console.WriteLine));
Set dialect

Dialect defines how flags and options are prefixed and how option value follows option.

NFlags.Configure(configurator => configurator.SetDialect(Dialect.Gnu));

Command configuration

NFlags always has at least one root command

Set flag

Flag is an prefixed argument without value. It can be turned on or off. Flag abreviation can be also set. There is also default value which will be inversed when flag will be passed as argument.

NFlags.Configure(c => {}).Root(configurator => configurator.RegisterFlag("flag", "f", "Flag description", true));
NFlags.Configure(c => {}).Root(configurator => configurator.RegisterFlag("flag", "Flag description", false));

Set option

Option is an prefixed argument with value. Flag abreviation can be also set.

NFlags.Configure(c => {}).Root(configurator => configurator.RegisterOption("option", "o", "option description", "defaultOptionValue"));
NFlags.Configure(c => {}).Root(configurator => configurator.RegisterOption("option", "option description", "defaultOptionValue"));

Set parameter

Parameter is an unprefixed value argument. Parameters are read by registration order.

NFlags.Configure(c => {}).Root(configurator => configurator.RegisterParam("param", "Param description", "paramDefaultValue"));

Attach code to execution

To attach code to execution by command, simply call SetExecution method of command configurator and pass Action<CommandArgs, Action<string>> callback. First argument of action contains all registered Flags, Options and Parameters with default or given values. The second one is callback to print output from command.

NFlags.Configure(c => {}).Root(configurator => configurator.SetExecute((commandArgs, output) => output("This is command output: " + commandArgs.Parameters["param"]));

Attach subcommands

To attach subcommand, call RegisterSubcommand method of command configurator. The third parameter is a configurator for the subcommand and can be used in the same Way as the one for root command..

NFlags.Configure(c => {}).
    Root(configurator => configurator.
        RegisterSubcommand("subcommand", "Subcommand Description", sc => sc.
                SetExecute((commandArgs, output) => output("This is subcommand: " + commandArgs.Parameters["SubParameter"])).
                RegisterParam("SubParameter", "SubParameter description", "SubParameterValue")
        )
    );

Parsing arguments

To parse arguments and call requested command call:

NFlags.Configure(c => {}).Root(configurator => {}).Run(args);

Dialects

Dialect defines how flags and options are prefixed and how option value follows option. By default 2 dialect are suported: Gnu, Win

Dialect can be set (default is Win) using:

NFlags.Configure(configurator => configurator.SetDialect(Dialect.Gnu));

Dialects can be easly extended. To create new dialect simply create new class inherited from Dialect class.

    public class CustomDialect : Dialect
    {      
        public override string Prefix => "x";

        public override string AbrPrefix => "a";

        public override OptionSeparator OptionSeparator => OptionSeparator.ArgSeparator;
    }

and configure NFlags using it

NFlags.Configure(configurator => configurator.SetDialect(new CustomDialect()));

Win dialect

Win dialect, follows MS Windows standards for definig console app arguments:

  • Flags and flags abreviations are prefixed by '/'
  • Options and options abreviations are prefixed by '/'
  • Value follow option after '='

Gnu dialect

Gnu dialect, follows Gnu Linux standards for definig console app arguments:

  • Flags are prefixed by '--'
  • Flags abreviations are prefixed by '-'
  • Options are prefixed by '--'
  • Options abreviations are prefixed by '-'
  • Value follow option as next argument after option

Help

Help generation is suported "out of box" and it follows dialect rules. To print help use:

Console.WriteLine(NFlags.Configure(configurator => {}).PrintHelp());

Example help for Win dialect:

Usage:
        NFlags.Win [FLAGS]... [OPTIONS]... [PARAMETERS]...

Application description

        Flags:
        /help, /h       Print this help
        /verbose, /v    Verbose description
        /clear  Clear description

        Options:
        /option1=<option1>, /o1=<option1>       Option 1 description
        /option2=<option2>      Option 2 description

        Parameters:
        <param1>        Parameter 1 description

and for Gnu dialect:

Usage:
        NFlags.Gnu [FLAGS]... [OPTIONS]... [PARAMETERS]...

Application description

        Flags:
        --help, -h      Print this help
        --verbose, -v   Verbose description
        --clear Clear description

        Options:
        --option1 <option1>, -o1 <option1>      Option 1 description
        --option2 <option2>     Option 2 description

        Parameters:
        <param1>        Parameter 1 description

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on NFlags:

Package Downloads
NetMicro.MongoDB.NFlags

Package Description

NetMicro.RabbitMQ.NFlags

Package Description

NetMicro.Monitoring.Prometheus.NFlags

Package Description

NetMicro.Auth.Jwt.NFlags

Package Description

NetMicro.Consul.NFlags

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.10.0 915 12/20/2021
1.9.0 460 11/15/2020
1.8.0 672 3/15/2020
1.7.2 1,879 11/3/2019
1.7.1 5,082 6/27/2019
1.7.0 703 4/7/2019
1.6.0 730 2/18/2019
1.5.0 753 2/2/2019
1.4.0.1 803 11/16/2018
1.4.0 789 11/14/2018
1.3.1 811 11/9/2018
1.3.0 788 11/2/2018
1.2.1 856 8/22/2018
1.1.1 1,133 12/15/2017
1.1.0-rc 1,337 11/3/2017
1.0.0 1,315 11/1/2017