Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork261
Command line parsing and utilities for .NET
License
natemcmaster/CommandLineUtils
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This project helps you create command line applications using .NET.It simplifies parsing arguments provided on the command line, validatinguser inputs, and generating help text.
Theroadmap for this project ispinned to the top of the issue list.
Seedocumentation for API reference, samples, and tutorials.See alsodocs/samples/ for more examples, such as:
- Hello world
- Async console apps
- Structuring an app with subcommands
- Defining options with attributes
- Interactive console prompts
- Required options and arguments
This project is available as aNuGet package.
$ dotnet add package McMaster.Extensions.CommandLineUtils
CommandLineApplication
is the main entry point for most console apps parsing. There are two primary ways to use this API, using the builder pattern and attributes.
usingSystem;usingMcMaster.Extensions.CommandLineUtils;publicclassProgram{publicstaticintMain(string[]args)=>CommandLineApplication.Execute<Program>(args);[Option(Description="The subject")]publicstringSubject{get;}="world";[Option(ShortName="n")]publicintCount{get;}=1;privatevoidOnExecute(){for(vari=0;i<Count;i++){Console.WriteLine($"Hello{Subject}!");}}}
usingSystem;usingMcMaster.Extensions.CommandLineUtils;varapp=newCommandLineApplication();app.HelpOption();varsubject=app.Option("-s|--subject <SUBJECT>","The subject",CommandOptionType.SingleValue);subject.DefaultValue="world";varrepeat=app.Option<int>("-n|--count <N>","Repeat",CommandOptionType.SingleValue);repeat.DefaultValue=1;app.OnExecute(()=>{for(vari=0;i<repeat.ParsedValue;i++){Console.WriteLine($"Hello{subject.Value()}!");}});returnapp.Execute(args);
The library also includes other utilities for interaction with the console. These include:
ArgumentEscaper
- use to escape arguments when starting a new command line process.varargs=new[]{"Arg1","arg with space","args ' with\" quotes"};Process.Start("echo",ArgumentEscaper.EscapeAndConcatenate(args));
Prompt
- for getting feedback from users with a default answer.A few examples:// allows y/n responses, will return false by default in this case.// You may optionally change the prompt foreground and background color for// the message.Prompt.GetYesNo("Do you want to proceed?",false);// masks input as '*'Prompt.GetPassword("Password: ");
DotNetExe
- finds the path to the dotnet.exe file used to start a .NET Core processProcess.Start(DotNetExe.FullPathOrDefault(),"run");
And more! See thedocumentation for more API, such asIConsole
,IReporter
, and others.
If you need help with this project, please ...
- read the documentation (https://natemcmaster.github.io/CommandLineUtils/),
- look at the samples (https://github.com/natemcmaster/CommandLineUtils/tree/main/docs/samples),
- review existing questions (many were answered already) (https://github.com/natemcmaster/CommandLineUtils/issues?q=label%3Aquestion+),
- or use a programming Q&A forum such as StackOverflow.com
This is a fork ofMicrosoft.Extensions.CommandLineUtils, which wascompletely abandoned by Microsoft. This projectforked in 2017 and continued to make improvements. From 2017 to 2021, over 30 contributors added new features and fixed bugs. As of 2022, the project has entered maintenance mode, so no major changes are planned.See this issue for details on latest project status. This project is not abandoned -- I believe this library provides a stable API and rich feature set good enough for most developers to create command line apps in .NET -- but only the most critical of bugs will be fixed (such as security issues).
About
Command line parsing and utilities for .NET
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.