-2

Hi so I am trying to get the two values after user input --dimensions.

For example when I do: dotnet app.dll --dimensions 1 2 (In command line)

It prints

1
2

Which is the result I'm after.

But when I do: dotnet app.dll --test --dimensions 1 2

It returns hi, meaning that --test worked. But returns an error for --dimensions

hi
Unhandled exception. System.FormatException: Input string was not in a correct format.
   at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
   at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
   at System.Convert.ToInt32(Object value)
   at MainClass.Main(String[] args)

I don't want the argument to only work if its the first argument given. That is why I added --test before --dimensions

I want it to work when the argument --dimensions is placed anywhere.

For example dotnet app.dll --test --test --dimensions 1 2 --test

Which should return

hi
hi
1
2
hi

Im new with this as well :( Sorry.

Code:

using System;

class MainClass
{
    private static int value1, value2;
    static void Main(string[] args)
    {
        foreach (string value in args)
        {
            int for_dimensions = 0;

            string testing = "--test";

            if (value == "--dimensions" && args.Length >= for_dimensions + 2)
            {
                object test1 = args.GetValue(for_dimensions+1);
                value1 = Convert.ToInt32(test1);
                    
                object test2 = args.GetValue(for_dimensions + 2);
                value2 = Convert.ToInt32(test2);

                Console.WriteLine(value1);
                Console.WriteLine(value2);
            }

            if (value == testing)
            {
                Console.WriteLine("hi");
            }
            
        }
        
    }
}

Thank you

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Already asked a few hours ago and oriented to a question about commandline parsers... Deleted and reposted. Know that doing that is annoying, espacially for whose taked time to help. You should improve your original question and/or wait for more help if needed, if you want help. –  Sep 18 '20 at 13:15
  • Well sorry, but https://stackoverflow.com/questions/491595/best-way-to-parse-command-line-arguments-in-c this did not help me much as it is too complex to what I am learning so far, as I said before, I am new and this concepts are very hard for me to understand. –  Sep 18 '20 at 13:22
  • You should use a standardized https://www.google.com/search?q=github+c%23+commandline+parser especially for complicated arguments. –  Sep 18 '20 at 13:24
  • What if I create a function that will take the command line argument as input, but how will I return those values? –  Sep 18 '20 at 13:34

1 Answers1

1

If you don't want to use an existing command line parser framework (which I highly recommend due to higher flexibility) for example this one, you can achieve your desired output like this:

// Find array index of "--dimensions"
var argIndex = Array.IndexOf(args, "--dimensions");

// Get remaining array size (all items after "--dimensions")
var subArraySize = args.Length - argIndex - 1;
string[] subArray = new string[subArraySize];

// Put all remaining items (all items after "--dimensions") into new array
Array.Copy(args, argIndex + 1, subArray, 0, subArraySize);

// Iterate over all items after "--dimensions"
var results = new List<int>();
foreach(var entry in subArray)
{
    // Check if the item is another command, if so we are finished
    if(entry.StartsWith('-'))
        break;
    
    // Item isn't a command, try to parse it to int and if successful add to result set
    if(int.TryParse(entry, out var numEntry))
        results.Add(numEntry);
}

To output the found parameters for "--dimensions" just iterate over results:

foreach(var result in results)
{
    Console.WriteLine(result);
}

With this implementation you can process a variable amount of dimension inputs. Either like in your example 1 and 2, or just 1 or 1, 2 and 3 for example:

dotnet app.dll --test --test --dimensions 1 2 --test
dotnet app.dll --test --test --dimensions 1 --test
dotnet app.dll --test --test --dimensions 1 2 3 --test

croxy
  • 4,082
  • 9
  • 28
  • 46
  • Thank you so much, I will try this one :). and we were told not to use an existing command line parser framework (I did not even know those existed). –  Sep 18 '20 at 13:42
  • Hi, may I be able to ask two more questions, sorry for the hassle, Im just really not good with this. How can I limit it to only two entries after `--dimensions`? How can I access or store those two entries for later use? Thank you again and sorry if I'm being annoying. –  Sep 18 '20 at 14:00
  • @j_m To store the entries you could assign them to your already existing variables `value1` and `value2` like this: `value1 = results[0]` and `value2 = results[1]`. When accessing values with the indexer `[]` you have to make sure the item at the index position you want to access does exist, otherwise an exception will be thrown. Saving the entries like this also makes limiting the result set to two entries unnecessary. – croxy Sep 18 '20 at 14:11