• Stars
    star
    2,092
  • Rank 22,081 (Top 0.5 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Command line parsing and utilities for .NET

CommandLineUtils

Build Status Code Coverage NuGet NuGet Downloads

This project helps you create command line applications using .NET. It simplifies parsing arguments provided on the command line, validating user inputs, and generating help text.

The roadmap for this project is pinned to the top of the issue list.

Usage

See documentation for API reference, samples, and tutorials. See also docs/samples/ for more examples, such as:

Installing the library

This project is available as a NuGet package.

$ dotnet add package McMaster.Extensions.CommandLineUtils

Code

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.

Attribute API

using System;
using McMaster.Extensions.CommandLineUtils;

public class Program
{
    public static int Main(string[] args)
        => CommandLineApplication.Execute<Program>(args);

    [Option(Description = "The subject")]
    public string Subject { get; } = "world";

    [Option(ShortName = "n")]
    public int Count { get; } = 1;

    private void OnExecute()
    {
        for (var i = 0; i < Count; i++)
        {
            Console.WriteLine($"Hello {Subject}!");
        }
    }
}

Builder API

using System;
using McMaster.Extensions.CommandLineUtils;

var app = new CommandLineApplication();

app.HelpOption();

var subject = app.Option("-s|--subject <SUBJECT>", "The subject", CommandOptionType.SingleValue);
subject.DefaultValue = "world";

var repeat = app.Option<int>("-n|--count <N>", "Repeat", CommandOptionType.SingleValue);
repeat.DefaultValue = 1;

app.OnExecute(() =>
{
    for (var i = 0; i < repeat.ParsedValue; i++)
    {
        Console.WriteLine($"Hello {subject.Value()}!");
    }
});

return app.Execute(args);

Utilities

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.
     var args = 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 process
    Process.Start(DotNetExe.FullPathOrDefault(), "run");

And more! See the documentation for more API, such as IConsole, IReporter, and others.

Getting help

If you need help with this project, please ...

Project origin and status

This is a fork of Microsoft.Extensions.CommandLineUtils, which was completely abandoned by Microsoft. This project forked 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).

More Repositories

1

dotnet-tools

A list of tools to extend the .NET Core command line (dotnet)
1,514
star
2

DotNetCorePlugins

.NET Core library for dynamically loading code
C#
1,435
star
3

LettuceEncrypt

Free, automatic HTTPS certificate generation for ASP.NET Core web apps
C#
1,396
star
4

dotnet-serve

Simple command-line HTTPS server for the .NET Core CLI
C#
663
star
5

Yarn.MSBuild

MSBuild integration for the Yarn package manager.
C#
70
star
6

msbuild-tasks

Sample MSBuild tasks for .NET Core and NuGet. (Issues: https://github.com/natemcmaster/blog)
C#
65
star
7

aspnetcore-webpack-hmr-demo

Demo of ASP.NET Core + webpack + hmr, and a few other things (Open issues on https://github.com/natemcmaster/blog)
C#
57
star
8

dotnet-globaltool-templates

Template for a .NET Core global tool project (please open issues in https://github.com/natemcmaster/blog)
PowerShell
24
star
9

xunit-extensions

Making XUnit.NET test projects even better
C#
23
star
10

xunit-cli

A global .NET Core command line tool for running XUnit tests
C#
9
star
11

EntityFrameworkCore.Samples

C#
7
star
12

ef-docker-talk

This repo contains the slides, demos, and code used for a talk I gave in 2016 about Docker, .NET Core, and Entity Framework Core
C#
7
star
13

dnvm

The .NET Core version manager (prototype, no longer under development). Use https://github.com/natemcmaster/blog for issues
C#
4
star
14

catan

Settlers of Catan for BYU CS 340
JavaScript
3
star
15

NuGetNetBuildTools

C#
3
star
16

srihash-cli

A command-line utility for generating the SRI hash
C#
2
star
17

msbuild-demos

Small demos of how MSBuild works
C#
2
star
18

blog

Yet-another Jekyll blog
HTML
2
star
19

alfred-sublime-text-workspace

See all recent Sublime Text workspaces in Alfred.
Python
2
star
20

dotnet-command

Extension for .NET Core CLI to run commands on a project
C#
2
star
21

.github

Default special GitHub config files
1
star
22

nuget-cli

C#
1
star
23

bbq-js

A light JS framework that imitates Angular.
JavaScript
1
star
24

libsqlite3-package

sqlite3 build automation and packaging. [Archived - see README]
Shell
1
star
25

catan_source

Settlers of Catan, view layer, BYU CS 340 Winter 2014
JavaScript
1
star
26

historyexplorer

An interactive webpage for exploring Western history from the Renaissance to present day
JavaScript
1
star