4

I'm reading a shortcut using the COM "Windows Script Host Object Model" IWshRuntimeLibrary like this:

IWshShell wsh = new WshShellClass();
IWshShortcut sc = (IWshShortcut)wsh.CreateShortcut(shortcutFile);
string arguments = sc.Arguments;

This gives me the arguments from the shortcut, say it looks like:

-a "c:\temp\a 1.txt" -b "b.exe -c \"c:\temp\c -2.txt\" -d d.txt" -e test

Which is not as a contrived example as you may think... How can I do the same as GetCommandLineArgs and get:

args {string[6]}
[0] = "-a"
[1] = "c:\\temp\\a 1.txt"
[2] = "-b"
[3] = "b.exe -c \"c:\\temp\\c -2.txt\" -d d.txt"
[4] = "-e"
[5] = "test"

I'm thinking Regex, but this is beyond my bracketing.

[edit] I'd prefer a regex, but I'll look at the code and classes mentioned.

The rules seem to be:

  • Split on spaces,
  • unless the space is within double quotes (").
  • Strings within double quotes may contain double quotes escaped with a backslash (\").
  • Other backslashes may appear on their own but should be treated as a normal character

The arguments and array in the previous two code sections would be an appropriate test.

[edit] https://stackoverflow.com/a/749653/350188 has answered my question. It calls the native system function and gives me exactly the same results as GetCommandLineArgs() with:

string[] splitArguments = CommandLineToArgs(arguments);
Community
  • 1
  • 1
Stephen Turner
  • 7,125
  • 4
  • 51
  • 68
  • Do you understand how the command line parser works? (I can write a regex to parse if I know how it parses the string). – nhahtdh Feb 19 '13 at 15:25
  • On first sight, I think that you won't get around regex... Our you split it on whitespaces and put it together again... – bash.d Feb 19 '13 at 15:28
  • 2
    There's a long-time class available for handling this, let me look. For starts, [here's one](http://commandline.codeplex.com/) and [another](http://www.codeproject.com/Articles/3852/Automatic-Command-Line-Parsing-in-C). – Grant Thomas Feb 19 '13 at 15:29
  • 1
    You might want to look at http://stackoverflow.com/questions/491595/best-way-to-parse-command-line-arguments-in-c – Jon Feb 19 '13 at 15:29
  • Check out [this answer](http://stackoverflow.com/questions/14727065/regular-expressions-matching-irc-like-parameters/14735486#14735486) (you can test right away in the browser). If you think it fits what you need, then I will post it as an answer (with modified regex, since regex in .NET is more powerful). – nhahtdh Feb 19 '13 at 15:29
  • may be this library can help http://commandline.codeplex.com/ – dan_l Feb 19 '13 at 15:32
  • A regex would be ideal, @nhahtdh if it could do the above that would be enough for my problem. – Stephen Turner Feb 19 '13 at 15:55
  • @webturner: I can write a regex, but unless I know the specific format, the regex may not work as expected. If you yourself don't know the specific format, then I suggest that you use an existing solution. – nhahtdh Feb 19 '13 at 15:57
  • I can thing of a really bad, but reliable way; launching a process, and then remoting back the args ;p – leppie Feb 19 '13 at 15:59
  • possible duplicate of [Split string containing command-line parameters into string\[\] in C#](http://stackoverflow.com/questions/298830/split-string-containing-command-line-parameters-into-string-in-c-sharp) –  Feb 19 '13 at 17:46
  • 1
    @hvd Yes, I've tried http://stackoverflow.com/a/749653/350188 and it seems the best. I've voted to close as a dupe of that Q. – Stephen Turner Feb 20 '13 at 08:04

3 Answers3

1

There's nothing really tricky about this. You can use basic C# syntax to examine the individual characters are parse out what you need.

For example, you can String.IndexOf() to locate specific characters you are looking for. And you can use String.Substring() to extract a portion of a string.

I've posted the code I did to parse a command line in the article A C# Command-Line Parser.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • http://meta.stackexchange.com/questions/92505/should-i-flag-answers-which-contain-only-a-link-as-not-an-answer – nhahtdh Feb 19 '13 at 15:34
  • 1
    @nhahtdh but Jonathan wrote that article himself. If the link would get rotten, wouldn't he also be the one to replace it? Also, there is a lot of code in the linked example. Should he post all of it here? – default Feb 19 '13 at 15:50
  • This does not appear to parse strings the way asked in the question. –  Feb 19 '13 at 15:51
  • @Default: This should probably be raised on meta. (I only point out that this post might get flagged and deleted in this state. I don't know a better alternative, so I didn't downvote or flag). – nhahtdh Feb 19 '13 at 15:54
  • @hvd: Right, it doesn't seem to support escaped quote `\"`. – nhahtdh Feb 19 '13 at 15:55
0

The easiest and robust solution will be to use some implementation of parser, for example, this one or library like Command Line Parser Library depends on what exactly do you need in your application.

kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
-3

If you simply want the arguments in an array like GetCommandLineArgs produces, you can use string.Split:

string[] args = arguments.Split(' ');
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48