Add your own alternative version
Stats
101.9K views 1.6K downloads 71 bookmarked
Posted
30 Aug 2003
|
Comments and Discussions
|
|
Hello ,
How are you today ? .
I hope you are fine and all is well with you, My name is miss glory i saw your profile at codeproject.com which really interest me and i decide to communicate with you, and it will please me if you will be my friend, Here is my direct email address (glory_west10@yahoo.in),so that i will tell you more about me and my picture hope to hear from you
Glory.
( glory_west10@yahoo.in)
|
|
|
|
|
If I run a command like "mycommand somearg" with no option switches, then I get zero handled or unhandled arguments, yet if I add an option switch, then the "somearg" becomes one of the unhandled arguments. How am I supposed to fetch non option arguments?
For instance if were to run "mycommand /sw1 /sw1 arg1 arg2 /sw3=value", how should I fetch arg1 and arg2 that are not option switches and not values of an option switch?
Thanks, Dave
Dave Kolb
http://dotnetcodeslingers.com
|
|
|
|
|
Here's how:
var parser = new ArgumentParser(...);
StringDictionary theArgs = parser.Parse(args);
string arg1 = theArgs["0"];
string arg2 = theArgs["1"];
|
|
|
|
|
Thanks Sebastian. I had thought that calling "parser.Parse" was optional and only needed if I wanted a StringDictionary and that if I called "parser.AutoSetMembers" that was sufficient.
Also, I found that the "parser = new ArgumentParser(ArgumentFormats.All, true)" form of the constructor failed with a null reference to flags and I had to use the form that included a flags argument even though it was an empty collection. Is that a minor bug?
Nice class btw!
Thanks much, Dave
Dave Kolb
http://dotnetcodeslingers.com
|
|
|
|
|
Well, I have not done it before, but my guess would that you can assign the argument name "0", "1", etc. to a member and so AutoSetMembers will work as expected.
Seems like the code version on CP is older than the one I got in my depot. That article is quite old (by internet standards ), so I will check if I should update it or not. But yes, seems like the version currently on CP has a bug
|
|
|
|
|
Another feedback is that running "myprogram /sw1 arg1" where /sw1 is not valid, leaves both sw1 and arg1 in the UnHandledArguments collection as well as in the dictionary returned by parser.Parse(args).
Ideally all parsed arguments and switches would be unique amongst HandledArguments, UnhandledArguments and the theArgs StringDictionary. That would make it easier to test for valid switches, invalid switches and the proper number of plain arguments I think.
Is there proper way to test for invalid switches and the proper number of plain arguemnts that are not switches that I missed?
If expecting one plain argument arg1, I would like would like to test theArgs.Count==1 and to check if there were any invlaid switches, I would like to check UnHnadledArguments.Count > 0. Since sw1 and arg1 appear both in the theArgs dictionary and in UnHandledArguments I cannot see a way to do either check currently. Hope that made sense.
Thanks, Dave
Dave Kolb
http://dotnetcodeslingers.com
|
|
|
|
|
Let me check the version I got first, it is the same principle, but it is heavily refactored. One learns many things over the years
|
|
|
|
|
Ok, I took a longer look at the code I got here. Actually, I have the version on CP and another newer one. I read my notes about the latter, and it appears it was a work in progress which I decided was leading nowhere, so I will correct the CP version instead.
I get your point about validating arguments, but I am not sure how I could figure out if "/sw1 arg1" is a switch with a parameter or it is a standalone switch and a plain argument. I think putting them in the wrong place is worse than putting them at two places. Let me know if you have any idea how to differentiate both cases.
I will wait for your comments before I post a new version of the code.
|
|
|
|
|
Thanks Sebastian. Since you only support : and = as assignment operators, then I should think it would not be a problem to determine if a switch or regular argument. Of course, one could no longer use a blank as an assignment operator as you suggested to one user, but that is just as well in my opinion, since it could only work for one blank and not more than one blank. So /sw1 arg1 would be a switch and a plain argument and /sw1=arg1 or /sw1:arg1 would be a switch with a value and no plain argument. Dave
Dave Kolb
http://dotnetcodeslingers.com
|
|
|
|
|
Hi... Nice work for sure! I like how simple it is to grab your code and use it with just a few lines of code. I was wondering if you can update the regex to accept this type of inputs "/i 2" instead of "/i:25" or "/i=25". I was not able to use your code for that type of user input.
Thanks.
|
|
|
|
|
Thank you for your comment I have currently no time to update the article, but you can do the following:
In the ArgumentParser class constructor, simply change
m_assignSymbols = new Char[] {'=', ':'};
for
m_assignSymbols = new Char[] {'=', ':', ' '};
|
|
|
|
|
Thanks. That does it! 
|
|
|
|
|
Hi,
how can use the same switch twice?
Example:
MyProg.Exe /if "C:\*.bmp" /if "C:\*.jpg" /of "C:\Temp\"
Best regards,
Armus
|
|
|
|
|
The switch is used as a key in the m_unhandled and m_handled dictionaries, so at this moment, your scenario is not supported. How about having something like
MyProg.Exe /if "C:\*.bmp","C:\*.jpg" /of "C:\Temp\" ?
|
|
|
|
|
Hi Sébastien Lorion,
I just installed your code on one of my projects. It seems
to parse well the parameters, but as I see, there is no
way of checking if an invalid argument was given. I tested
it with your xmove example and I didn't get any error. I
try this:
C:\xmove\xmove /badarg1 /badarg2 "_" "_"
0 file(s) found.
0 file(s) moved.
I'm right or there is some option I'm missing.
Regargs,
Josef
|
|
|
|
|
No automatic check is done, but you can check manually that no invalid arguments remain with ArgumentParser.UnhandledArguments property.
Sébastien
Intelligence shared is intelligence squared.
Homepage : http://sebastienlorion.com
|
|
|
|
|
Attribute arrays are not CLS complaint and can't be used from VB.NET. Could you have a third version of the attribute that simply accepts the switch name?
Best,
Mike
|
|
|
|
|
I totally agree with Mike... I have the same problem and can't find a way to work with the argument parser in VB.NET.
Too bad! I think we'll have to modify your code then...
Guy
|
|
|
|
|
This code seems to be GPL, so it can't be used in any project (even compled and used as a DLL) that isn't also GPL. Was this your intention?
|
|
|
|
|
It was a mistake but I thought I had updated the files on CP.
I am working on a new version more powerful and better designed, but I have many things going on at the same time so that might wait until january.
I will update files here, but in the mean time, just consider them licensed under LGPL.
Sébastien
Intelligence shared is intelligence squared.
Homepage : http://www.slorion.webhop.org
|
|
|
|
|
Looks like a great argument parser, I've been using the one Microsoft released but it's a bit of a let down in a few areas but this one makes up for it!
Just a comment about licensing, you might find it will be more useful to people released as LGPL and not GPL so they can use it in closed-source applications unless it is your intention for it to only be used in open-source ones.
|
|
|
|
|
Thank you Sometimes, one has to wonder why Microsoft haven't included basic stuff like this ...
As for switching to LGPL, I agree it would be less restrictive ... I will change it when publishing the new version (see answer to comment by
Heath Stewart).
Sébastien
Intelligence shared is intelligence squared.
Homepage : http://www.slorion.webhop.org
|
|
|
|
|
Great job. I've seen a lot of good attempts at this, but I just couldn't convince myself to start using any of them for whatever reason. I was afraid I'd have to write my own, but I'm glad you beat me to it!
Brian
|
|
|
|
|
Thank you ! I'm glad it does the job for you I love it when I can just take something almost as is so if I can make that happens to others, then that's cool
Sébastien
Intelligence shared is intelligence squared.
Homepage : http://www.slorion.webhop.org
|
|
|
|
|
This is one of the most complete command-line parsers I've seen for .NET, and you've done a good job with some seemless integration. One feature that would be nice - something I've seen with several PERL cmdline parser, is automatic documentation for cmdline parameters when ((-|/)?|(--?|/)h(elp)?) is specified. For instance, a Documentation property for the AutoSetMemberAttribute would be handy, as well as a way to set documentation for the collection of flags you keep (if applicable).
This is already a very nice utility, but this is just a suggestion that could possibly add value.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
|
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
|