question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option to convert Arguments object back into args string

See original GitHub issue

Issue 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:closed
  • Created 6 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ericnewton76commented, Nov 4, 2017

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


It works now.

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

0reactions
moh-hassancommented, Oct 2, 2019

The class UnParserExtensions convert Arguments object back into args string

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found