[英]Getting parameters after argument in command line
Hi so I am trying to get the two values after user input --dimensions.嗨,所以我试图在用户输入 --dimensions 后获取这两个值。
For example when I do: dotnet app.dll --dimensions 1 2
(In command line)例如,当我这样做时: dotnet app.dll --dimensions 1 2
(在命令行中)
It prints它打印
1
2
Which is the result I'm after.这是我追求的结果。
But when I do: dotnet app.dll --test --dimensions 1 2
但是当我这样做时: dotnet app.dll --test --dimensions 1 2
It returns hi
, meaning that --test
worked.它返回hi
,这意味着--test
有效。 But returns an error for --dimensions
但为--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
这就是为什么我在--dimensions
之前添加了--test
I want it to work when the argument --dimensions
is placed anywhere.我希望它在参数--dimensions
放置在任何地方时工作。
For example dotnet app.dll --test --test --dimensions 1 2 --test
例如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谢谢
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
:要为“--dimensions”输出找到的参数,只需迭代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:就像在您的示例 1 和 2 中一样,或者只是 1 或 1、2 和 3,例如:
dotnet app.dll --test --test --dimensions 1 2 --test dotnet app.dll --test --test --dimensions 1 2 --test
dotnet app.dll --test --test --dimensions 1 --test dotnet app.dll --test --test --dimensions 1 --test
dotnet app.dll --test --test --dimensions 1 2 3 --test dotnet app.dll --test --test --dimensions 1 2 3 --test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.