Skip to content

Easy argument parsing for .Net applications. Handles options (arguments with parameters) and flags (simple switches), with the facility to show automatically generated usage details and errors. Compatible with NetStandard 2.0 for use in Core 3 or later (tested to Net 8).

License

Notifications You must be signed in to change notification settings

kcartlidge/ArgsParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ArgsParser

Easy argument parsing for .Net Core applications.

Available as a nuget package.

Example usage

var parser = new ArgsParser.ArgsParser(args[0])
    .SupportsOption<int>("port", "Port to start the dev server on", 1337)
    .RequiresOption<string>("read", "Folder to read the site from", "site")
    .RequiresOption<string>("write", "Folder to write the result to")
    .HasFlag("serve", "Start the site going in a dev server")
    .HasFlag("force", "Overwrite any destination content")
    .Help();

parser.Parse();

Console.Log($"Errors: {parser.Errors.Count}");
Console.Log($"  Port: {parser.GetOption<int>("port")}");  // method 1 returns as the type
Console.Log($"  Port: {parser.ParsedOptions["port"]}")    // method 2 returns as an object

Example autogenerated help info (.Help())

Required options come first, then optional options, then flags. Each of those three blocks is further sorted alphabetically.

-read  <value>   Folder to read the site from (required)
-write <value>   Folder to write the result to (required)
-port  <value>   Port to start the dev server on

-force           Overwrite any destination content
-serve           Start the site going in a dev server

Example input

mycommand -run data "Site Title" --serve -ignore -port 3000

Example errors from the above

Errors are keyed by either the name of the option/flag if there is an issue with that specific one, or by "N/A" for things like unexpected (and therefore orphaned) values.

if (parser.HasErrors)
    foreach (var key in parser.Errors.Keys)
        parser.Errors[key].ForEach(x => Console.WriteLine($"{key} => {x}"));

This gives:

ignore => Unknown flag: ignore
N/A => Unexpected value: Site Title
read => Option missing: read
run => Unknown option: run
write => Option missing: write

In addition:

  • parser.ParsedFlags will contain two entries, "serve" and "ignore"
  • parser.ParsedOptions["run"] will contain "data" (string)
  • parser.ParsedOptions["port"] will contain 3000 (int)
    • As will parser.GetOption<int>("port")

Supported

  • Display help showing supported flags/options
  • Required named option/values
  • Optional named option/values
  • Optional named flags
  • Parsing generates simple collections
    • A collection of all provided options (keys and their typed values)
    • A collection of all provided flags (array of named)
  • Provides a collection of error messages
    • Missing required options
    • Unknown flags or options
    • Values not attached to options
  • Option types support any IConvertable, including int, bool, DateTime
  • Accepts either - or -- prefixes

Copyright K Cartlidge 2020. Licensed as GNU AGPLv3.

See the CHANGELOG for current status.

About

Easy argument parsing for .Net applications. Handles options (arguments with parameters) and flags (simple switches), with the facility to show automatically generated usage details and errors. Compatible with NetStandard 2.0 for use in Core 3 or later (tested to Net 8).

Topics

Resources

License

Stars

Watchers

Forks

Languages