Comments (6)
Comment by issacg
Wednesday Dec 18, 2013 at 16:09 GMT
It works now.
https://gist.github.com/issacg/8024973
from commandline.
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.
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.
Comment by allonhadaya
Wednesday Dec 18, 2013 at 15:54 GMT
Yeah, I think I just found the bug... A small typo.
from commandline.
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.
The class UnParserExtensions convert Arguments object back into args string
from commandline.
Related Issues (20)
- GetoptMode = true suppresses help.
- Looking to parse "default" value after initializing object with factory HOT 1
- Option are displayed in lowercase in help HOT 2
- Trying to use custom type for options: "is defined with a bad format." HOT 1
- Does Command Line Parser affect the program execution? HOT 1
- Parallel.ForEachAsync not working in Debug mode within the WithParsed method HOT 2
- Increase the number of verbs supported HOT 2
- bool value is not support HOT 3
- Change "Command Line Grammar" wiki page
- Backslash parsing problem in argument. HOT 1
- `VerbAttribute` has unnecessary breaking changes between 2.8.0 and 2.9.1 HOT 1
- Bad format doesn't contain a lot of info HOT 2
- Multi instance setting ignored when default verb is parsed.
- How to get help information as a variable? HOT 1
- Detecting switches HOT 1
- Handling the CS8618 warning (Non-nullable property) HOT 1
- Is the package being maintained? HOT 1
- VerbAttribute violates CS3016 code inspection
- Code crashes without any concrete error and no help documents
- Praser saying "Option parameter was provided without an option." need help
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from commandline.