PowerArgs 4.0.1
See the version list below for details.
dotnet add package PowerArgs --version 4.0.1
NuGet\Install-Package PowerArgs -Version 4.0.1
<PackageReference Include="PowerArgs" Version="4.0.1" />
paket add PowerArgs --version 4.0.1
#r "nuget: PowerArgs, 4.0.1"
// Install PowerArgs as a Cake Addin #addin nuget:?package=PowerArgs&version=4.0.1 // Install PowerArgs as a Cake Tool #tool nuget:?package=PowerArgs&version=4.0.1
Binary
PowerArgs is available at the Official NuGet Gallery.
To build command line applications with text-based graphics try out klooie.
Overview
PowerArgs converts command line arguments into .NET objects that are easy to work with. It also provides a ton of additional, optional capabilities that you can try such as argument validation, auto generated usage, tab completion, and plenty of extensibility. You can even build a full-blown, interactive text UI application like this one.
It can also orchestrate the execution of your program. Giving you the following benefits:
- Consistent and natural user error handling
- Invoking the correct code based on an action (e.g. 'git push' vs. 'git pull')
- Focus on writing your code
Here's a simple example that just uses the parsing capabilities of PowerArgs. The command line arguments are parsed, but you still have to handle exceptions and ultimately do something with the result.
// A class that describes the command line arguments for this program
public class MyArgs
{
// This argument is required and if not specified the user will
// be prompted.
[ArgRequired(PromptIfMissing=true)]
public string StringArg { get; set; }
// This argument is not required, but if specified must be >= 0 and <= 60
[ArgRange(0,60)]
public int IntArg {get;set; }
}
class Program
{
static void Main(string[] args)
{
try
{
var parsed = Args.Parse<MyArgs>(args);
Console.WriteLine("You entered string '{0}' and int '{1}'", parsed.StringArg, parsed.IntArg);
}
catch (ArgException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ArgUsage.GenerateUsageFromTemplate<MyArgs>());
}
}
}
Here's the same example that lets PowerArgs do a little more for you. The application logic is factored out of the Program class and user exceptions are handled automatically. The way exceptions are handled is that any exception deriving from ArgException will be treated as user error. PowerArgs' built in validation system always throws these type of exceptions when a validation error occurs. PowerArgs will display the message as well as auto-generated usage documentation for your program. All other exceptions will still bubble up and need to be handled by your code.
// A class that describes the command line arguments for this program
[ArgExceptionBehavior(ArgExceptionPolicy.StandardExceptionHandling)]
public class MyArgs
{
// This argument is required and if not specified the user will
// be prompted.
[ArgRequired(PromptIfMissing=true)]
public string StringArg { get; set; }
// This argument is not required, but if specified must be >= 0 and <= 60
[ArgRange(0,60)]
public int IntArg {get;set; }
// This non-static Main method will be called and it will be able to access the parsed and populated instance level properties.
public void Main()
{
Console.WriteLine("You entered string '{0}' and int '{1}'", this.StringArg, this.IntArg);
}
}
class Program
{
static void Main(string[] args)
{
Args.InvokeMain<MyArgs>(args);
}
}
Then there are more complicated programs that support multiple actions. For example, the 'git' program that we all use supports several actions such as 'push' and 'pull'. As a simpler example, let's say you wanted to build a calculator program that has 4 actions; add, subtract, multiply, and divide. Here's how PowerArgs makes that easy.
[ArgExceptionBehavior(ArgExceptionPolicy.StandardExceptionHandling)]
public class CalculatorProgram
{
[HelpHook, ArgShortcut("-?"), ArgDescription("Shows this help")]
public bool Help { get; set; }
[ArgActionMethod, ArgDescription("Adds the two operands")]
public void Add(TwoOperandArgs args)
{
Console.WriteLine(args.Value1 + args.Value2);
}
[ArgActionMethod, ArgDescription("Subtracts the two operands")]
public void Subtract(TwoOperandArgs args)
{
Console.WriteLine(args.Value1 - args.Value2);
}
[ArgActionMethod, ArgDescription("Multiplies the two operands")]
public void Multiply(TwoOperandArgs args)
{
Console.WriteLine(args.Value1 * args.Value2);
}
[ArgActionMethod, ArgDescription("Divides the two operands")]
public void Divide(TwoOperandArgs args)
{
Console.WriteLine(args.Value1 / args.Value2);
}
}
public class TwoOperandArgs
{
[ArgRequired, ArgDescription("The first operand to process"), ArgPosition(1)]
public double Value1 { get; set; }
[ArgRequired, ArgDescription("The second operand to process"), ArgPosition(2)]
public double Value2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
Args.InvokeAction<CalculatorProgram>(args);
}
}
Again, the Main method in your program class is just one line of code. PowerArgs will automatically call the right method in the CalculatorProgram class based on the first argument passed on the command line. If the user doesn't specify a valid action then they get a friendly error. If different actions take different arguments then PowerArgs will handle the validation on a per action basis, just as you would expect.
Here are some valid ways that an end user could call this program:
Calculator.exe add -Value1 1 -Value2 5
outputs '6'Calculator.exe multiply /Value1:2 /Value2:5
outputs '10'Calculator.exe add 1 4
outputs '5' - Since the [ArgPosition] attribute is specified on the Value1 and Value2 properties, PowerArgs knows how to map these arguments.
If you wanted to, your action method could accept loose parameters in each action method. I find this is useful for small, simple programs where the input parameters don't need to be reused across many actions.
[ArgActionMethod, ArgDescription("Adds the two operands")]
public void Add(
[ArgRequired][ArgDescription("The first value to add"), ArgPosition(1)] double value1,
[ArgRequired][ArgDescription("The second value to add"), ArgPosition(2)] double value2)
{
Console.WriteLine(value1 + value2);
}
You can't mix and match though. An action method needs to be formatted in one of three ways:
- No parameters - Meaning the action takes no additional arguments except for the action name (i.e. '> myprogram.exe myaction').
- A single parameter of a complex type whose own properties describe the action's arguments, validation, and other metadata. The first calculator example used this pattern.
- One or more 'loose' parameters that are individually revivable, meaning that one command line parameter maps to one property in your class. The second calculator example showed a variation of the Add method that uses this pattern.
Metadata Attributes
These attributes can be specified on argument properties. PowerArgs uses this metadata to influence how the parser behaves.
[ArgPosition(0)]
This argument can be specified by position (no need for -propName)[ArgShortcut("n")]
Lets the user specify -n[ArgDescription("Description of the argument")]
[ArgExample("example text", "Example description")]
[HelpHook]
Put this on a boolean property and when the user specifies that boolean. PowerArgs will display the help info and stop processing any additional work. If the user is in the context of an action (e.g. myprogram myaction -help) then help is shown for the action in context only.[ArgDefaultValue("SomeDefault")]
Specify the default value[ArgIgnore]
Don't populate this property as an arg[StickyArg]
Use the last used value if not specified. This is preserved across sessions. Data is stored in <User>/AppData/Roaming/PowerArgs by default.[TabCompletion]
Enable tab completion for parameter names (see documentation below)
Validator Attributes
These attributes can be specified on argument properties. You can create custom validators by implementing classes that derive from ArgValidator.
[ArgRequired(PromptIfMissing=bool)]
This argument is required. There is also support for conditionally being required.[ArgExistingFile]
The value must match the path to an existing file[ArgExistingDirectory]
The value must match the path to an existing directory[ArgRange(from, to)]
The value must be a numeric value in the given range.[ArgRegex("MyRegex")]
Apply a regular expression validation rule[UsPhoneNumber]
A good example of how to create a reuable, custom validator.
Custom Revivers
Revivers are used to convert command line strings into their proper .NET types. By default, many of the simple types such as int, DateTime, Guid, string, char, and bool are supported.
If you need to support a different type or want to support custom syntax to populate a complex object then you can create a custom reviver.
This example converts strings in the format "x,y" into a Point object that has properties "X" and "Y".
[ArgReviverType]
public class CustomReviverExample
{
// By default, PowerArgs does not know what a 'Point' is. So it will
// automatically search your assembly for arg revivers that meet the
// following criteria:
// - Live in a class or struct with an [ArgReviverType] attribute
// - Are a public, static method with an [ArgReviver] attribute
// - Accepts exactly two string parameters
// - The return value matches the type that is needed
public Point Point { get; set; }
// This ArgReviver matches the criteria for a "Point" reviver
// so it will be called when PowerArgs finds any Point argument.
//
// ArgRevivers should throw ArgException with a friendly message
// if the string could not be revived due to user error.
[ArgReviver]
public static Point Revive(string key, string val)
{
var match = Regex.Match(val, @"(\d*),(\d*)");
if (match.Success == false)
{
throw new ArgException("Not a valid point: " + val);
}
else
{
Point ret = new Point();
ret.X = int.Parse(match.Groups[1].Value);
ret.Y = int.Parse(match.Groups[2].Value);
return ret;
}
}
}
Generate usage documentation from templates (built in or custom)
PowerArgs has always provided auto-generated usage documentation via the ArgUsage class. However, the format was hard coded, and gave very little flexibility in terms of the output format. With the latest release of PowerArgs usage documentation can be fully customized via templates. A template is just a piece of text that represents the documentation structure you want along with some placeholders that will be replaced with the actual information about your command line application. There are built in templates designed for the console and a web browser, and you can also create your own.
In its latest release, PowerArgs adds a new method called ArgUsage.GenerateUsageFromTemplate(). The method has several overloads, most of which are documented via XML intellisense comments. The part that needs a little more explanation is the template format. To start, let's talk about the built in templates.
The first one, the default, is designed to create general purpose command line usage documentation that is similar to the older usage documentation that PowerArgs generated. You can see what that template looks like here.
The second one is designed to create documentation that looks good in a browser. You can see what that template looks like here.
Here is an example of what the built in browser usage looks like.
You can create your own templates from scratch, or modify these default templates to suit your needs. The templating engine was built specifically for PowerArgs. It has quite a bit of functionality and extensibility, but for now I'm only going to document the basics.
Most of you probably use the class and attributes model when using PowerArgs, but under the hood there's a pretty extensive object model that gets generated from the classes you build. That model is what is bound to the template. If you're not familiar with the object model you can explore the code here.
You can see from the built in templates that there is placeholder syntax that lets you insert information from the model into template. For example, if your program is called 'myprogram' then the following text in the template would be replaced with 'myprogram'.
{{ExeName !}} is the best
// outputs - 'myprogram is the best'
Additionally, you can add a parameter to the replacement tag that indicates the color to use when printed on the command line as a ConsoleString. You can use any ConsoleColor as a parameter.
{{ExeName Cyan !}}
You can also choose to conditionally include portions of a template based on a property. Here's an example from the default template:
{{if HasActions}}Global options!{{if}}{{ifnot HasActions}}Options!{{ifnot}}:
In this case, if the HasActions property on the CommandLineArgumentsDefinition object is true then the usage will output 'Global options'. Otherwise it will output 'Options'. This flexibility is important since some command line programs have only simple options while others expose multiple commands within the same executable (e.g. git pull and git push).
Another thing you can do is to enumerate over a collection to include multiple template fragments in your output. Take this example.
{{each action in Actions}}
{{action.DefaultAlias!}} - {{action.Description!}}
!{{each}}
If your program has 3 actions defined then you'd get output like this.
action1 - action 1 description here
action2 - action 2 description here
action3 - action 3 description here
When referring to a part of your data model you can also navigate objects using dot '.' notation. Notice in the example above I was able to express {{ action.DefaultAlias !}}. You could go even deeper. For example {{ someObj.SomeProperty.DeeperProperty !}}. More advanced expressions like function calling with parameters are not supported.
PS
I'm pretty happy with the templating solution. In fact, hidden in PowerArgs is a general purpose template rendering engine that I've found useful in other projects for things like code generation. You can actually bind any string template to any plain old .NET object (dynamic objects not supported). Here's a basic sample:
var renderer = new DocumentRenderer();
var document = renderer.Render("Hi {{ Name !}}", new { Name = "Adam" });
// outputs 'Hi Adam'
Ambient Args
Access your parsed command line arguments from anywhere in your application.
MyArgs parsed = Args.GetAmbientArgs<MyArgs>();
This will get the most recent insance of type MyArgs that was parsed on the current thread. That way, you have access to things like global options without having to pass the result all throughout your code.
Secure String Arguments
Support for secure strings such as passwords where you don't want your users' input to be visible on the command line.
Just add a property of type SecureStringArgument.
public class TestArgs
{
public SecureStringArgument Password { get; set; }
}
Then when you parse the args you can access the value in one of two ways. First there's the secure way.
TestArgs parsed = Args.Parse<TestArgs>();
SecureString secure = parsed.Password.SecureString; // This line causes the user to be prompted
Then there's the less secure way, but at least your users' input won't be visible on the command line.
TestArgs parsed = Args.Parse<TestArgs>();
string notSecure = parsed.Password.ConvertToNonsecureString(); // This line causes the user to be prompted
Tab Completion
Get tab completion for your command line arguments. Just add the TabCompletion attribute and when your users run the program from the command line with no arguments they will get an enhanced prompt (should turn blue) where they can have tab completion for command line argument names.
[TabCompletion]
public class TestArgs
{
[ArgRequired]
public string SomeParam { get; set; }
public int AnotherParam { get; set; }
}
Sample usage:
someapp -some <-- after typing "-some" you can press tab and have it fill in the rest of "-someparam"
You can even add your own tab completion logic in one of two ways. First there's the really easy way. Derive from SimpleTabCompletionSource and provide a list of words you want to be completable.
public class MyCompletionSource : SimpleTabCompletionSource
{
public MyCompletionSource() : base(MyCompletionSource.GetWords()) {}
private static IEnumerable<string> GetWords()
{
return new string[] { "SomeLongWordThatYouWantToEnableCompletionFor", "SomeOtherWordToEnableCompletionFor" };
}
}
Then just tell the [TabCompletion] attribute where to find your class.
[TabCompletion(typeof(MyCompletionSource))]
public class TestArgs
{
[ArgRequired]
public string SomeParam { get; set; }
public int AnotherParam { get; set; }
}
There's also the easy, but not really easy way if you want custom tab completion logic. Let's say you wanted to load your auto completions from a text file. You would implement ITabCompletionSource.
public class TextFileTabCompletionSource : ITabCompletionSource
{
string[] words;
public TextFileTabCompletionSource(string file)
{
words = File.ReadAllLines(file);
}
public bool TryComplete(bool shift, string soFar, out string completion)
{
var match = from w in words where w.StartsWith(soFar) select w;
if (match.Count() == 1)
{
completion = match.Single();
return true;
}
else
{
completion = null;
return false;
}
}
}
If you expect your users to sometimes use the command line and sometimes run from a script then you can specify an indicator string. If you do this then only users who specify the indicator as the only argument will get the prompt.
[TabCompletion("$")]
public class TestArgs
{
[ArgRequired]
public string SomeParam { get; set; }
public int AnotherParam { get; set; }
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- No dependencies.
NuGet packages (6)
Showing the top 5 NuGet packages that depend on PowerArgs:
Package | Downloads |
---|---|
Microsoft.Sbom.Api
Highly scalable, extensible and enterprise ready API to create SBOMs for any variety of artifacts. |
|
maxbl4.RfidDotNet.GenericSerial.Demo
Package Description |
|
UserSettings
A library and command line tool to save user settings to the cloud in a scalable way |
|
klooie
A framework for building GUI applications within any command line that runs .NET. Klooie provides all the things you would expect from a UX Framework. |
|
klooie.Windows
Audio playback support for klooie applications on Windows. |
GitHub repositories (16)
Showing the top 5 popular GitHub repositories that depend on PowerArgs:
Repository | Stars |
---|---|
mayuki/Cocona
Micro-framework for .NET console application. Cocona makes it easy and fast to build console applications on .NET.
|
|
Cysharp/ConsoleAppFramework
Zero Dependency, Zero Overhead, Zero Reflection, Zero Allocation, AOT Safe CLI Framework powered by C# Source Generator.
|
|
microsoft/sbom-tool
The SBOM tool is a highly scalable and enterprise ready tool to create SPDX 2.2 compatible SBOMs for any variety of artifacts.
|
|
Tyrrrz/CliFx
Class-first framework for building command-line interfaces
|
|
microsoft/artifacts-credprovider
The Azure Artifacts Credential Provider enables dotnet, NuGet.exe, and MSBuild to interactively acquire credentials for Azure Artifacts feeds.
|
Version | Downloads | Last updated |
---|---|---|
4.0.3 | 182,195 | 9/23/2023 |
4.0.2 | 112,964 | 3/19/2023 |
4.0.1 | 2,200 | 3/11/2023 |
4.0.0 | 96,368 | 10/29/2022 |
3.6.0 | 1,215,722 | 2/6/2019 |
3.5.0 | 65,342 | 7/21/2018 |
3.0.0 | 1,525,025 | 8/27/2017 |
2.8.1 | 45,905 | 4/30/2017 |
2.8.0.2 | 17,691 | 4/4/2017 |
2.8.0.1 | 2,665 | 4/4/2017 |
2.8.0 | 36,980 | 1/14/2017 |
2.7.2 | 12,526 | 9/6/2016 |
2.7.1 | 159,259 | 2/15/2016 |
2.7.0 | 24,775 | 8/29/2015 |
2.6.0.1 | 75,417 | 3/5/2015 |
2.6.0 | 3,064 | 3/5/2015 |
2.5.11 | 3,928 | 2/28/2015 |
2.5.10.1 | 3,329 | 2/13/2015 |
2.5.10 | 2,952 | 2/13/2015 |
2.5.9.2 | 3,028 | 2/7/2015 |
2.5.9.1 | 2,831 | 2/6/2015 |
2.5.9-preview | 2,748 | 2/4/2015 |
2.5.8-preview | 2,652 | 2/4/2015 |
2.5.7-preview | 2,390 | 1/30/2015 |
2.5.6-preview | 2,728 | 1/30/2015 |
2.5.5-preview | 2,329 | 1/30/2015 |
2.5.4-preview | 2,292 | 1/30/2015 |
2.5.3-preview | 2,303 | 1/28/2015 |
2.5.2-preview | 2,627 | 1/10/2015 |
2.5.1-preview | 2,547 | 1/6/2015 |
2.5.0 | 4,503 | 12/12/2014 |
2.4.1 | 2,747 | 12/10/2014 |
2.4.0 | 2,761 | 12/10/2014 |
2.3.2 | 3,118 | 12/3/2014 |
2.3.1 | 27,603 | 10/7/2014 |
2.3.0 | 2,638 | 10/3/2014 |
2.2.0 | 2,610 | 10/2/2014 |
2.1.2-preview | 2,436 | 7/2/2014 |
2.1.1-preview | 2,406 | 7/1/2014 |
2.1.0-preview | 2,491 | 6/11/2014 |
2.0.6.1 | 5,547 | 6/4/2014 |
2.0.6 | 4,818 | 5/30/2014 |
2.0.5.1 | 3,219 | 5/12/2014 |
2.0.5 | 20,401 | 3/30/2014 |
2.0.4-preview | 2,916 | 11/16/2013 |
2.0.3 | 10,151 | 10/15/2013 |
2.0.2 | 3,097 | 10/2/2013 |
2.0.1 | 2,732 | 9/30/2013 |
2.0.0 | 2,736 | 9/30/2013 |
1.8.0 | 5,758 | 6/24/2013 |
1.7.0 | 2,824 | 6/8/2013 |
1.6.0 | 5,254 | 5/10/2013 |
1.5.1 | 3,041 | 4/19/2013 |
1.5.0 | 18,039 | 4/11/2013 |
1.4.1 | 2,663 | 4/9/2013 |
1.4.0 | 2,899 | 3/28/2013 |
1.3.1 | 2,885 | 3/19/2013 |
1.2.0.1 | 2,924 | 3/11/2013 |
1.2.0 | 2,647 | 3/4/2013 |
1.1.14 | 4,769 | 8/6/2012 |
1.1.13 | 2,831 | 8/5/2012 |
1.1.11 | 3,031 | 7/30/2012 |
1.1.10 | 2,744 | 7/14/2012 |
1.1.9 | 2,914 | 5/6/2012 |
1.1.8.1 | 2,702 | 5/5/2012 |
1.1.8 | 2,709 | 5/5/2012 |
1.1.7 | 2,684 | 4/29/2012 |
1.1.1 | 2,805 | 4/22/2012 |
1.1.0 | 2,842 | 4/15/2012 |
1.0.0.7 | 2,770 | 4/13/2012 |
1.0.0.4 | 2,844 | 4/12/2012 |
1.0.0.3 | 2,713 | 4/7/2012 |
1.0.0.2 | 2,828 | 4/7/2012 |
1.0.0.1 | 2,759 | 4/7/2012 |
1.0.0 | 3,136 | 4/7/2012 |
Updated to .Net 6, some minor fixes and improvements