Option to convert Arguments object back into args string
See original GitHub issueIssue by issacg Wednesday Dec 18, 2013 at 10:09 GMT _Originally opened as https://github.com/gsscoder/commandline/issues/114_
Hi
In my use-case, I needed to (occasionally) construct command line parameters for another instance of my process, and overloaded my class’ ToString method to do so. I’d love to contribute the code back, but not sure if it’s something that’s needed, or where the best place in the library would be to put it.
I’ve included the code here, and if there is interest, and someone would give me some basic guidance as to where the correct place to add this to the library would be, I’d be happy to make a pull request (time permitting 😉)
class MyArguments {
// Add some option-properties
public override string ToString()
{
string res = "";
// Get a list of all properties with OptionAttribute
var props = this.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(OptionAttribute)));
// Now search the list to extract the attribute data
foreach (PropertyInfo prop in props)
{
object[] attrs = prop.GetCustomAttributes(true);
foreach (object attr in attrs)
{
OptionAttribute oa = attr as OptionAttribute;
if (oa != null)
{
// We found an OptionAttribute!
Type t = prop.GetType();
var data = prop.GetValue(this, null);
// Check if value is "default(t)" or not
if (data != (t.IsValueType ? Activator.CreateInstance(t) : null))
{
// Only print out non-empty parameters
res += "--" + oa.LongName + " " + data.ToString() + " ";
}
}
}
}
return res;
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
How can I convert the "arguments" object to an array in ...
Here is benchmark of several methods converting arguments into array. Object. values( ) will return the values of an object as an array,...
Read more >How to convert arguments object into an array in JavaScript
This method can be used to convert the arguments object by binding this method to the object. The binding is done using the...
Read more >How to Convert Arguments Object to an Array
Read this JavaScript tutorial and learn information about the useful methods that are used for converting array-like arguments object to an Array easily....
Read more >The arguments object - JavaScript - MDN Web Docs - Mozilla
arguments is an array-like object, which means that arguments has a length property and properties indexed from zero, but it doesn't have Array ......
Read more >argparse — Parser for command-line options, arguments ...
ArgumentParser parses arguments through the parse_args() method. This will inspect the command line, convert each argument to the appropriate type and then ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Comment by issacg Wednesday Dec 18, 2013 at 16:09 GMT
It works now.
https://gist.github.com/issacg/8024973
The class UnParserExtensions convert Arguments object back into args string