Giter Site home page Giter Site logo

stephenrskinner / commandlineutils Goto Github PK

View Code? Open in Web Editor NEW

This project forked from natemcmaster/commandlineutils

0.0 0.0 0.0 5.2 MB

Command line parsing and utilities for .NET Core and .NET Framework.

Home Page: https://natemcmaster.github.io/CommandLineUtils/

License: Apache License 2.0

PowerShell 0.27% C# 99.73%

commandlineutils's Introduction

CommandLineUtils

Build Status

NuGet NuGet Downloads

This is a fork of Microsoft.Extensions.CommandLineUtils, which is no longer under active development. This fork, on the other hand, will continue to make improvements, release updates and take contributions.

The roadmap for this project can be found here.

Install

Install the NuGet package into your project.

PM> Install-Package McMaster.Extensions.CommandLineUtils
$ dotnet add package McMaster.Extensions.CommandLineUtils

Usage

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

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; }

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

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

Builder API

using System;
using McMaster.Extensions.CommandLineUtils;

public class Program
{
    public static int Main(string[] args)
    {
        var app = new CommandLineApplication();

        app.HelpOption();
        var optionSubject = app.Option("-s|--subject <SUBJECT>", "The subject", CommandOptionType.SingleValue);
        var optionRepeat = app.Option<int>("-n|--count <N>", "Repeat", CommandOptionType.SingleValue);

        app.OnExecute(() =>
        {
            var subject = optionSubject.HasValue()
                ? optionSubject.Value()
                : "world";

            var count = optionRepeat.HasValue() ? optionRepeat.ParsedValue : 1;
            for (var i = 0; i < count; i++)
            {
                Console.WriteLine($"Hello {subject}!");
            }
            return 0;
        });

        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. A few examples:
    // allows y/n responses
    Prompt.GetYesNo("Do you want to proceed?");
    
    // 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.

commandlineutils's People

Contributors

natemcmaster avatar pranavkm avatar ntaylormullen avatar dougbu avatar rynowak avatar brennanconroy avatar pakrym avatar ajaybhargavb avatar kichalla avatar theconstructor avatar analogrelay avatar davidfowl avatar juntaoluo avatar jbagga avatar troydai avatar benaadams avatar atruskie avatar bricelam avatar ryanbrandenburg avatar madbhatter avatar tratcher avatar haok avatar javiercn avatar henkmollema avatar fredrikhr avatar rmcc13 avatar mikeharder avatar jerriep avatar eilon avatar vpkopylov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.