Coder Social home page Coder Social logo

Comments (6)

ericnewton76 avatar ericnewton76 commented on June 29, 2024 1

Comment by issacg
Wednesday Dec 18, 2013 at 16:09 GMT


It works now.

https://gist.github.com/issacg/8024973

from commandline.

ericnewton76 avatar ericnewton76 commented on June 29, 2024

Comment by allonhadaya
Wednesday Dec 18, 2013 at 15:43 GMT


Nice! Not sure about how this all fits into the library, but I played around with your code and came up with this. It uses some more linq and extracts the functionality into an abstract base class.

using System;
using System.Linq;
using CommandLine;

/// <summary>
/// A base class for options that can be turned back into an argument string.
/// </summary>
public abstract class ReversableOptions
{
    public override string ToString()
    {
        return string.Concat(this
            .GetType()
            .GetProperties()
            .Where(p => Attribute.IsDefined(p, typeof(OptionAttribute)))
            // for all properties with option attributes
            .SelectMany(p => p
                .GetCustomAttributes(typeof(OptionAttribute), inherit: true)
                .Select(a => (OptionAttribute)a)
                // get the type, name, and data
                .Select(a => new {
                    type = p.GetType(),
                    name = a.LongName,
                    data = p.GetValue(this, null)
                })
                // where data is not default
                .Where(v => v.data != ((v.type.IsValueType) ? Activator.CreateInstance(v.type) : null))
                // make an argument string
                .Select(v => string.Format("--{0} {1} ", v.name, v.data))));
    }
}

I was kicking around the idea of making a tag interface with a single Reverse extension, but every implementer would have to do something like this:

class MyOptions : ISomeTag
{
    public override string ToString()
    {
        return this.Reverse();
    }
}

which seems a bit crufty.

Anyways, hope this helps.

from commandline.

ericnewton76 avatar ericnewton76 commented on June 29, 2024

Comment by issacg
Wednesday Dec 18, 2013 at 15:52 GMT


Yeah, the Reverse tag would be a bit sucky. I don't really have a better idea, though. Maybe just leave the ToString code as a Gist and mention it in the docs...

I tried using your ToString as a drop-in replacement to mine, and it actually did not seem to work - I'll try to find some time tomorrow to look into it

from commandline.

ericnewton76 avatar ericnewton76 commented on June 29, 2024

Comment by allonhadaya
Wednesday Dec 18, 2013 at 15:54 GMT


Yeah, I think I just found the bug... A small typo.

from commandline.

ericnewton76 avatar ericnewton76 commented on June 29, 2024

Comment by allonhadaya
Wednesday Dec 18, 2013 at 16:00 GMT


Sorry about that, just updated my earlier comment with the fix. I think a gist would be a good place to start.

from commandline.

moh-hassan avatar moh-hassan commented on June 29, 2024

The class UnParserExtensions convert Arguments object back into args string

from commandline.

Related Issues (20)

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.