10

Salutations,

I have what seems to be a simple problem but is very much not. I want to take a string and split it into command line arguments. I have been Googling this question for weeks and can't find anything that suits my needs.

For example, the line: --foo=bar -foo="bar test" --foo "bar \"test\"" --foo bar

Would split into: (in this order)

  • --foo=bar
  • -foo=bar test
  • --foo
  • bar "test"
  • --foo
  • bar

EDIT

Yes I realize --foo is use more that once. This is splitting/tokenizing. Not parsing, that's the next step. I don't care if that would error when i go to parse. What i want to do RIGHT NOW is get the string into an array state that i can then feed into Mono.Options

EDIT 2

Read the example. That is what I am trying to accomplish. JUST that.

Cole
  • 165
  • 2
  • 11
  • 1
    Did you create an MCVE? – Der Kommissar Sep 29 '16 at 07:05
  • 1
    Why do you want to split the args at all? This is usually automatically done when you execute your app using the console. However for parsing args there are many tools out there, for instance CommandLine-parser. – MakePeaceGreatAgain Sep 29 '16 at 07:06
  • @BugFinder that answer is for parsing. Not for tokenizing/spiltting – Cole Sep 29 '16 at 07:13
  • There are many nuget packages that deal with command line arguments, see https://twitter.com/JamesNK/status/778088889588281345 – Ian Mercer Sep 29 '16 at 07:14
  • Take a look at [Environment.GetCommandLineArgs](https://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs(v=vs.110).aspx) if you simply want to get the arguments your program was start with, or the duplicate otherwise. – Manfred Radlwimmer Sep 29 '16 at 07:14
  • Command line parsing is *NOT* done automatically. That's why there are libraries like NDesk, CommandLineParse and now, .NET's own System.CommandLine – Panagiotis Kanavos Sep 29 '16 at 07:14
  • 2
    @HimBromBeere this is not a one shot application. Command strings will be supplied from a socket connection – Cole Sep 29 '16 at 07:14
  • 1
    use a real command-line parser, like https://github.com/gsscoder/commandline – porges Sep 29 '16 at 07:18
  • 2
    @Cole - better be clear inside the question that you don't get this from the actual command line but want to duplicate the parsing the OS does. – H H Sep 29 '16 at 07:20
  • @HenkHolterman the OS doesn't do any parsing. Parsing is a lot more than just splitting individual tokens. .NET even has it's own [System.CommandLine](https://github.com/dotnet/corefxlab/tree/master/src/System.CommandLine) library now, for exactly this reason. A command line parser should return a `string[4]` array for `foo` given this command line – Panagiotis Kanavos Sep 29 '16 at 07:22
  • @PanagiotisKanavos The commandline-tools all just get information from an array of stings into something meaningfull. However as far as I understand OP he needs to get this *array*. So the question is not on interpreting the information stored in an array, but to build it. Thus the duplicate and all solutions mentioning external tools won´t fit here. I´m voting to close this question as unclear. – MakePeaceGreatAgain Sep 29 '16 at 07:25
  • 1
    @PanagiotisKanavos - but the OP just wants to duplicate the splitting, with the processing of (escaped) quotes. Maybe then he needs an options parser but that's not in scope. – H H Sep 29 '16 at 07:25
  • 2
    @Cole check [this question](http://stackoverflow.com/questions/298830/split-string-containing-command-line-parameters-into-string-in-c-sharp) Tokenizing a command line isn't trivial using String.Split or regular expressions. A character-based parser is far easier. Windows also has a Win32 API function for this, `CommandLineToArgvW` – Panagiotis Kanavos Sep 29 '16 at 07:41
  • @PanagiotisKanavos tried that already. Unfortunately doesn't work with equal signs `--foo="bar hello"` becomes `--foo="bar`, `hello` – Cole Sep 29 '16 at 07:43
  • @PanagiotisKanavos Your last link is a good duplicate-candidate. Unfortunately I already voted to close this question. – MakePeaceGreatAgain Sep 29 '16 at 07:45
  • @Cole you should be able to modify that parser according to your requirements, since it allows you to specify in code the split condition – Panagiotis Kanavos Sep 29 '16 at 07:47
  • @HimBromBeere voted to close THIS question? – Cole Sep 29 '16 at 07:47
  • you might want to look at this: `Utils.StartArgs.cs` @ https://github.com/BananaAcid/Selfcontained-C-Sharp-WPF-compatible-utility-classes/blob/master/Utils.StartArgs.cs ... it does split all kinds of comandline argument styles - quotes are handled by windows. – BananaAcid Oct 09 '18 at 13:33

2 Answers2

-4

If you are running a console application, the command line arguments are passed to Main method (String[] args).

Anyway, if you want to split your arguments, assuming that arguments are in one string variable, you can do this:

var arguments = "--foo=bar -foo=\"bar test\" --foo \"bar \"test\" --foo bar";

var options = arguments.Split(new String[] { "-", "--" }, StringSplitOptions.RemoveEmptyEntries);

// Output
// [0]: "foo=bar "
// [1]: "foo=\"bar test\" "
// [2]: "foo \"bar \"test\" "
// [3]: "foo bar"

enter image description here

H. Herzl
  • 3,030
  • 1
  • 14
  • 19
  • 1
    This would also split "bar test\" – Pepernoot Sep 29 '16 at 07:12
  • You need to review overloads for Split method, with this line of code: var array = arguments.Split(new String[] { "-", "--" }, StringSplitOptions.RemoveEmptyEntries); you get 4 arguments – H. Herzl Sep 29 '16 at 07:17
  • 1
    Parsing a command line is a bit trickier than simply splitting a string. For example, this command line contains *four* values for the single parameter `foo`. Besides, a good parser should be able to handle verbs, verb arguments and different parameter identifiers lie `/`, `-` and `--`. It should be able to handle quoted values, which a Split can't do. Even a regex would have trouble with this – Panagiotis Kanavos Sep 29 '16 at 07:17
  • 1
    And all that should have an easy to use API, which is why command line parsing is done with libraries like .NET's System.CommandLine, NDesk, or CommandLineParser – Panagiotis Kanavos Sep 29 '16 at 07:19
  • @H.Herzl Your method would split `-foo="bar hello"` into `-foo="hello` **AND** `hello"` – Cole Sep 29 '16 at 07:21
  • Please review my answer, I attached one screenshot; let me know if is useful. – H. Herzl Sep 29 '16 at 07:25
  • The point is this: you can split your string with one array string and not only with one char or string – H. Herzl Sep 29 '16 at 07:27
  • I believe that you should try to design the command line arguments for one of your applications first, before you realize why parsing means more than string splitting. So you got some strings. What do these mean? What is the parameter's name? Will you split again? Even then, how are you going to assign values to actual variables or method parameters? – Panagiotis Kanavos Sep 29 '16 at 07:28
  • Your edit comes closer but still not good enough. You will have to split on whitespace and deal with escaped quotes somehow. – H H Sep 29 '16 at 07:28
  • @PanagiotisKanavos - you really should read the question again. – H H Sep 29 '16 at 07:29
  • @HenkHolterman I had to create command line argumens for several utilities so I understand that parsing is much more than just getting at the tokens – Panagiotis Kanavos Sep 29 '16 at 07:30
-5

Just simply use Environment.GetCommandLineArgs().

This will give you a array of strings which represent the command line arguments

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
Radinator
  • 1,048
  • 18
  • 58