-1

I have an application where I have to provide number of parameters in the format Name:Value

I provide the list of parameters through the Command line arguments value under "Debug" section of the project

So, it look something like that: "MyJobName" "0" "@FullFilePath:C:\MyFile.txt" "@FileType:MyFileType" "@FileDate:20200318" "@FileID:MyAppID"

One parameter is FilePath:C:\FileDir\MyFileTxt.txt

So, when the following logic is applied:

for (int i = 2; i <= args.GetLength(0) - 1; i++)
{
    L.Add(args[i].Split(':')[0], args[i].Split(':')[1]);
}

My Parameter looks like that: FilePath:C, ignoring the rest of the path.

The final parameter list that I need to pass to the Stored Procedure should have "Name:Value" format

How can I fix that?

gene
  • 2,098
  • 7
  • 40
  • 98

4 Answers4

0

Split lets you pass the maximum array length. See Split Split(Char[], Int32)

Splits a string into a maximum number of substrings based on the characters in an array. You also specify the maximum number of substrings to return.

Sample:

var keyValue = args[i].Split(new char[]{ ':' }, 2);
L.Add(keyValue[0], keyValue[1]);

This way only the first : is taken. The other : that come after it are ignored and will be part of the second item in the array.

But I honestly advise you to use a proper parameter parser, because your approach is very easy to break and very very fragile. https://github.com/commandlineparser/commandline

Legacy Code
  • 650
  • 6
  • 10
0

Have a look at dragonfruit and Systel.CommandLine

Instead of writing your arguments parser yourself.

It’s a way to have type safe arguments in your main method.

Scott Hanselman has a great blog post about it.

The great part being your XML comments are used to generate a help message.

Preben Huybrechts
  • 5,853
  • 2
  • 27
  • 63
0

The moment you use Split, you exclude the delimiter from being a valid character without having to add the extra overloads to it. So if you absolutely must use a colon as your delimiter, you can either use the Split with overload as suggested above, or write extra code to address it;below is how I would parse it.

Of course, a much easier alternative (if possible) would be to change your delimiter to something you know it would never use, something like a pipe or a tilde or a backtick (|, ~, ). Then Split would work cleanly.

"@FullFilePath:C:\MyFile.txt" "@FileType:MyFileType" "@FileDate:20200318" "@FileID:MyAppID"

If your parameters always have the format @ParameterName:ParameterValue, your best bet is to parse the command line args like so:

var argumentsList = new Dictionary<string,object>();
for (int i=2; i < args.Length; i++) 
{
  int colonIndex = args[i].IndexOf(":");
  string parameterName = args[i].Substring(0, colonIndex - 1);
  string parameterValue = args[i].Substring(colonIndex + 1);
  argumentsList[parameterName] = parameterValue;
}

The scope of your question centers around how to get around the colon, so however you choose to store the parameter values is up to you, I just used the dictionary as an example to help wrap up the code.

DWF
  • 290
  • 2
  • 11
-1

This will skip FilePath and give you C:\FileDir\MyFileTxt.txt

string.Join(":", args[i].Split(':').Skip(1));
  • This will not give you the file path. It will give you the `C` from this string `@FullFilePath:C:\MyFile.txt` – Legacy Code Jul 23 '20 at 19:17
  • What? No it wont. If args[i] = @FullFilePath:C:\MyFile.txt it will give you C:\MyFile.txt. Try it before you down vote – Eric Sjöström Jul 23 '20 at 19:25
  • It will give you an IEnumeration with 2 elements. Technically not a string. You have to join them `string.Join(":", bla)` to get the required result. – Legacy Code Jul 23 '20 at 19:33
  • That is exactly what i wrote, but you save args[i].Split(':').Skip(1) in a variable first. But it's exactly the same code – Eric Sjöström Jul 23 '20 at 19:35
  • Try this Console.Log Genius `Console.WriteLine(string.Join(":", @"FullFilePath:C:\MyFile.txt".Split(':').Skip(1)));` – Eric Sjöström Jul 23 '20 at 19:42
  • Sorry I might have not seen the `string.Join` in the first place, but the fact that you split the string to then join them is a bit ineffective. It's like using a json deserializer to clone an object. – Legacy Code Jul 23 '20 at 19:43
  • It's the best one liner I know to solve this problem to do it dynamic. If the text would contain more ":". Something like this "FullFilePath:C:\My:File.txt:test" and you only want to remove the first :. Sure there might exist some more effective way. But this will never affect performance – Eric Sjöström Jul 23 '20 at 19:49
  • `Split(Char[], Int32)` – Legacy Code Jul 23 '20 at 19:55