Command line arguments
Brand new class Energy.Base.Command.Arguments appeared here to ease implement command line options for everybody.
This class is build with Named Parameter Idiom design pattern.
Argument parsing mechanism was inspired by excelent npm package yargs for JavaScript.
var argv = new Energy.Base.Command.Arguments(args)
.Switch("help")
.Switch("quiet")
.Parameter("input")
.Parameter("output")
.Alias("?", "help")
.Alias("q", "quiet")
.Alias("i", "input")
.Alias("o", "input")
.Help("input", "Input file")
.Help("output", "Output file")
.Parse();
if (!argv["help"].Empty)
{
Console.WriteLine("Help");
}
Constructor
Energy.Base.Command.Arguments(string[] args)
Energy.Base.Command.Arguments(string line)
You can still add argument line later using fluent helpers, even when you started with the parameterless constructor.
Energy.Base.Command.Arguments()
Add
Append arguments after construction.
Arguments Add(string[] args)
Arguments Add(string line)
Use the string overload when you have a single command-line string instead of an already tokenized array.
Parameter
Add command line parameter that will consume one or more trailing arguments and provide them as values.
If you set count to 0, it will become switch (flag).
Arguments Parameter(string name, int count)
Add command line parameter that will consume one next trailing argument and provide it as value.
Arguments Parameter(string name)
Special one.
Treat all unknown options as parametered.
Arguments Parameter()
Switch
Add single command line switch option, known also as flag. Like “-v”, or “–version”.
Arguments Switch(string name)
Special one.
Treat all unknown options as simple switches.
Arguments Switch()
Line
Add arguments from text line.
Arguments are divided by any whitespace character.
Arguments may use double quote (”) character to include whitespace, and multiple quoting is allowed within one argument.
For example: C:”Documents and settings””Program Files”
will be considered as one argument.
Arguments Line(string line)
Explode
Split command line text without mutating the builder. Useful if you just need tokenization.
static string[] Arguments.Explode(string line)
Skip
Skip first n entries when parsing arguments.
Skip(int skip)
Strict
Set strict mode.
When strict mode is set, exception will be thrown on unrecognized option name.
Arguments Strict(bool strict)
Slash
Allow usage of slash options (starting with “/”).
Allows to use DOS style options like “/?”.
It’s RSX-11 (and other similar DEC systems), through CP/M to MS-DOS legacy.
Should be avoided probably.
Arguments Slash(bool slash)
Short
Allow usage of short options (starting with “-”).
Turned on as default.
Arguments Short(bool enable)
Long
Allow usage of long options (starting with “–”).
Turned on as default.
Arguments Long(bool enable)
Alias
Add alias to parameter key.
Arguments Alias(string alias, string name)
Help
Add help description for parameter key.
Arguments Help(string name, string description)
Description
Add long description lines for a parameter. Call multiple times to append paragraphs; pass null to clear.
Arguments Description(string name, string text)
Note
Add help notes printed after option list. Multiple calls append new paragraphs; pass null to clear all notes.
Arguments Note(string text)
Repository
Add repository links printed near the footer. Multiple entries are supported; pass null to clear.
Arguments Repository(string text)
Copyright
Add copyright notices printed last. Multiple entries are supported; pass null to clear.
Arguments Copyright(string text)
License
Add license notes printed after copyright section. Multiple entries are supported; pass null to clear.
Arguments License(string text)
Title
Add optional program title to be printed.
Arguments Title(string title)
About
Add additional description to be printed after optional title.
Arguments About(string about)
Usage
Set usage information printed after optional title and optional about information.
Call multiple times to append additional paragraphs or variations. Pass null to clear the list before rebuilding it.
Arguments Usage(string title)
Greetings
Set greetings information to be printed at the end.
You may call it multiple times to provide multi-line acknowledgements. Passing null removes all previously set greetings.
Arguments Greetings(string title)
Example
Provide example values for an option. Use multiple calls to add more examples. Pass null to remove existing examples for the option.
Arguments Example(string name, string example)
Print
Return text to be printed for command line usage description.
var argv = new Energy.Base.Command.Arguments(args)
.Title("Program title")
.About("Short or longer program description")
.Switch("help")
.Switch("version")
.Alias("?", "help")
.Alias("V", "version")
.Help("help", "Show this information")
.Help("version", "Display version number")
.Parse();
if (argv["help"].IsTrue)
{
Console.Write(argv.Print());
return;
}
If --help or -? switch is used, program will print text description.
Program title
Short or longer program description
--help
-?
Show this information
--version
-V
Display version number
Parse
Probably most important function here.
Parse arguments and set values for options.
Arguments Parse()
Parameterless method will parse arguments set by constructor or modified later.
When calling with parameters, arguments to be parsed will only be taken from invoker.
Arguments Parse(string line)
Arguments Parse(string[] args)
Full example (PrintArgs)
The sample program below demonstrates most fluent helpers, including multi-line usage, notes, greetings, repositories and copyrights. It mirrors Energy.Core.Example.PrintArgs.
using System;
using System.Diagnostics;
namespace PrintArgs
{
class Program
{
static void Main(string[] args)
{
var argv = new Energy.Base.Command.Arguments(args)
.Title("Program title")
.Usage("Usage information part one.")
.Usage("Usage information part two.")
.Usage("Usage information part three.")
.About("This is information about the program.")
.Switch("help")
.Alias("h", "help")
.Description("help", "Show this help")
.Switch("version")
.Alias("V", "version")
.Description("version", "Show version number")
.Switch("info")
.Alias("i", "info")
.Description("info", "Show information")
.Parameter("output")
.Alias("o", "output")
.Description("output", "Output file name")
.Switch("overwrite")
.Description("overwrite", "Overwrite existing file")
.Alias("w", "overwrite")
.Switch("verbose")
.Alias("v", "verbose")
.Description("verbose", "Print additional information")
.Note("Note line one.")
.Note("Note line two.")
.Greetings("Greetings line one.")
.Greetings("Greetings line two.")
.Repository("https://github.com/repository")
.Author("First author")
.Author("Second author")
.Copyright("2024-2026 All rights reversed")
.Copyright("2024-2026 Another rights reversed")
.License("MIT License")
.License("Apache License 2.0")
.Parse();
Console.WriteLine(argv.Print().TrimEnd());
if (Debugger.IsAttached)
Console.ReadLine();
}
}
}