GithubHelp home page GithubHelp logo

wallymathieu / isop Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 3.0 2.98 MB

Isop is the swedish name for hyssop. Like any spice it is intended to give flavor to the development of command line apps.

Home Page: http://assertfail.gewalli.se/2011/04/24/command-line-option-parsing.html

License: MIT License

C# 100.00%
cli cli-parser command-line-tool command-line object-oriented-programming clean-code

isop's Introduction

Isop Build Status Build status

The name

Isop is the swedish name for hyssop. Like any spice it is intended to give flavor to the development of command line apps in .net.

Goal

The goal is to be able to write code like:

someprogram.exe My Action --argument value

Or if you prefer:

someprogram.exe My Action /argument value

Isop will also figure out what you mean if you write with an equals sign between argument and value:

someprogram.exe My Action --argument=value

Or if you want to write it shorter you can skip the argument name:

someprogram.exe My Action value

So that the class with the name My or MyController and the method with the name Action gets invoked.

This library is intended to be like chocolate pudding mix. Not something that will replace your dinner, but rather something easy to make for dessert. A way of helping you build for instance the essential administrative apps. It's not a replacement for baking cake (building a full blown administrative interface in html).

When to use Isop

  • Early in your development life cycle (before having tools around for your business app)
  • You want an exe with many different commands organized into categories (here called controllers)

When not to use Isop

  • When your exe only has one command, then a simpler library like ndesc options is preferable.
  • You do not want any magic. Isop tries to help you with binding arguments to method arguments.
  • When you have a micro service solution together with some other authentication mechanism you can use Swagger to fill the same role.

License

MIT License

Nuget packages

Isop

Example

Having your own Main

You're hooking it up by writing something like:

static Task Main(string[] args)=>
    AppHostBuilder.Create()
       .Recognize(typeof(CustomerController))
       .BuildAppHost()
       .Parse(args)
       .TryInvokeAsync();

Where your controller looks something like this:

public class MyController
{
    private readonly CustomerRepository _repository;
    public MyController()
    {
        _repository = new CustomerRepository();
    }
    public IEnumerable<string> Add(string name)
    {
        yield return "Starting to insert customer";
        _repository.Insert( new Customer{ Name = name } );
        yield return "Customer inserted";  
    }
}

When invoked it will output two lines to the command prompt, the yielded lines above.

Handling errors and unrecognized parameters

class Program
{
    static async Task<int> Main(string[] args)
    {
        var appHost = AppHostBuilder
            .Create(new Configuration
            {
                CultureInfo = CultureInfo.GetCultureInfo("sv-SE")
            })
            .Recognize(typeof(CustomerController))
            .BuildAppHost();
        try
        {
            var parsedMethod = appHost.Parse(args);
            if (parsedMethod.Unrecognized.Any())//Warning:
            {
                Console.WriteLine($@"Unrecognized arguments: {string.Join(",", parsedMethod.Unrecognized.Select(arg => arg.Value).ToArray())}");
                return 1;
            }else
            {
                await parsedMethod.InvokeAsync(Console.Out);
                return 0;
            }
        }
        catch (TypeConversionFailedException ex)
        {
            Console.WriteLine(
                $"Could not convert argument {ex.Argument} with value {ex.Value} to type {ex.TargetType}");
            if (null!=ex.InnerException)
            {
                Console.WriteLine("Inner exception: ");
                Console.WriteLine(ex.InnerException.Message);
            }
            return 9;
        }
        catch (MissingArgumentException ex)
        {
            Console.WriteLine($"Missing argument(s): {String.Join(", ", ex.Arguments).ToArray()}");
            Console.WriteLine(appHost.Help());
            return 10;
        }
    }
}

Why all this code? Mostly it's because I want the programmer to be able to have as much freedom as possible to handle errors and show error messages as he/she sees fit.

Alternative

If you want to have something simpler for simple command line applications then I would recommend using ndesc options or commandline or perhaps .net command-line-api.

isop's People

Contributors

olleolleolle avatar wallymathieu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

2hdddg lundalogik

isop's Issues

Isop wpf: Helpcontroller

The gui should be able to access the helpcontroller somehow to extract the documentation/help texts for controllers and actions.

Help for action

Help for action reports the action among the parameters it accepts.

Server client architecture for WPF app

Right now the code assumes that you have it running on the same machine. It would be interesting to have a rest like layer between desktop app and the actual execution.

  • simple local only web server
  • represent commands and actions in html & JSON to begin with
  • refactor WPF app to use local host http
  • global parameters
  • MissingArgumentException using http status code 400 and json or html
  • wpf app missing argument

Correct way to run with StructureMap

What is now the correct way to run with structremap in the net core version?
The ObjectFactory method is no longer in the configuration object.

Isop wpf: When the execution of an action crashes

Right now the program Isop.Wpf.exe just crashes (uncaught exception). The simplest option would be to output message, stacktrace et.c. somewhere so that the user can mail this to the developer (or if it's a developer, see what's going wrong).

Isop.Server dispose

How should the lifecycle be handled? Isop.Server should probably dispose the isop configuration after each request. We want it to load the assembly with isop configuration only once (perhaps on startup).

Security and hosting for Isop.Server

  • Use api-key in Isop.Server (or perhaps oath)
  • Generate api-key on filesystem when app starts?
  • Localhost only when not using api-key
  • Possible to use windows auth?
  • Anti-CSRF Token?

Isop api for invoking method

Right now Isop.Server sends parameters by mapping to the command line string format. Perhaps there is a better way to enable this.

When using isop for making import tools

It would be sweet if Isop could look at the parameter and if it's of type string and with name filename (or something similar) then it would be sweet if it presents a file dialog in wpf.

Use <icon> element

The <iconUrl> element is deprecated. Consider using the <icon> element instead.

Need a way to output status or progress from an action

Right now the only way to output to console is to return a string from the action or calling Console.Write directly from the actions. It would be nice to have a standard way of doing it, like some interface for outputting stuff that is injected in the controllers.

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.