Giter VIP home page Giter VIP logo

yaclp's Introduction

#YACLP ##Yet Another Command-Line Parser

It's on NuGet:

Install-Package YACLP

Why another one?

Because there were a bunch out there but all of them focused more on the parsing than on being quick and easy to call.

I want my command-line parser to not only parse arguments (which it does, but isn't very flexible about) but to automatically generate a usage message so that I don't have to.

##Simple Usage

var options = DefaultParser.ParseOrExitWithUsageMessage<MyCommandLineParameters>(args);

##Recommendations I'd recommend using an IConfiguration or similar interface so that anything that depends on it doesn't need to know about command-line parameters.

Our main program would look like this:

public class Program
{
    private static void Main(string[] args)
    {
        var configuration = DefaultParser.ParseOrExitWithUsageMessage<CommandLineParameters>(args);

        new Greeter(configuration).SayHello();
    }
}

... and our Greeter like this:

public class Greeter
{
    private readonly IConfiguration _configuration;

    public Greeter(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void SayHello()
    {
        var message = string.IsNullOrEmpty(_configuration.Surname)
                          ? string.Format("Hi, {0}!", _configuration.FirstName)
                          : string.Format("Hello, Mr/Ms {0}", _configuration.Surname);

        Console.WriteLine(message);
    }
}

Note that our Greeter takes a dependency on an IConfiguration, which looks like this:

public interface IConfiguration
{
    string FirstName { get; set; }
    string Surname { get; set; }
}

... and that IConfiguration interface is implemented by our CommandLineParameters class:

public class CommandLineParameters : IConfiguration
{
    [ParameterDescription("The first name of the person using the program.")]
    public string FirstName { get; set; }

    [ParameterIsOptional]
    [ParameterDefault("Smith")]
    [ParameterDescription("The last name of the person using the program.")]
    public string Surname { get; set; }
}

The key point here is that our Greeter knows absolutely nothing about command-line parameters as everything is segregated using the IConfiguration interface.

##License

This project is licensed using the MIT license.

The MIT License (MIT) Copyright (c) 2016 Andrew Harcourt

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

yaclp's People

Contributors

caseymarcallen avatar dependabot-preview[bot] avatar stephengodbold avatar uglybugger avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

yaclp's Issues

Just a couple of thoughts

Hi!

I would have liked to contact you via mail or twitter, but I couldn't find any contact information, so I had to use this.

The overall design is really great. Especially the separation of things the app has to know of the parameters and the parsing is great stuff.

I have some things that need improvement though:

  1. Optional Parameters should be able to have "/" or "-" in front
  2. Parameters should be able to have a long name with "--" in front and a short (letter) with "-" or "/"
  3. Optional Parameters should not be required to have a ":value" part if they are only a switch
    Like -Verbose I don't like to have to write /Verbose:true
  4. The output of the usage function is getting garbled as soon as the description has more than
    one line. Please consider using " " (two spaces) to indent rather than a tab (8 spaces) and try to
    break and indent lines for the description.
  5. Parameters that only can have distinctive values like sorting (column name) or sort order
    (descending/ascending) should be able to have those values in an attribute to check against.
  6. For "debugging" purposes it would be great to have an output of all parameters and values
  7. Maybe an additional "validation" code would be advisable that is called after the parameters are
    populated and checks for things that exclude each other (like a parameter can only occur once,
    or a mode of operation that inhibts some other parameters to be used). Easiest way would be
    to have a method be called in the parameters class that can issue "warnings" and "Errors".
    Warnings are only output and the program goes on running, Errors print out a message and
    terminate like "Usage" output.

It'll be great to hear back from you what you think about those topics.

You can reach me via e-mail at akr(at)ntt-sw(dot)de
Or in Twitter via NTTAKR

Thanks for this awesome project.
With a little work it could become my default Commandline Parser.

Best regards,
Andreas

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.