简体   繁体   English

传递枚举命令行参数

[英]Passing enum command line arguments

So I have this switch in order to set a wait time 所以我有这个开关来设置一个等待时间

public int Option(string arg)
{
    switch (arg)
    {
        case "/t:Max":
                return 0;
        case "/t:Med":
                return 1000;
        case "/t:Min":
               return 2000;
        default: return 0;
    }
}

How can I use enum for /t:Min, /t:Med, /t.Max in order to replace the switch? 如何使用枚举/ t:Min,/ t:Med,/ t.Max来替换交换机?

Indeed something like: 的确如下:

enum t
{
    /t:Min,
    /t:Med,
    /t:Max
};

You should use Enum.Parse of TryParse . 你应该使用TryParse Enum.Parse Also see: http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx 另请参阅: http//msdn.microsoft.com/en-us/library/system.enum.parse.aspx

But still you would have to retrieve the string from the command line arguments (strip the '/t:') and parse it into your Enum . 但是你仍然需要从命令行参数中检索字符串(去掉'/ t:')并将其解析为你的Enum

Your enumeration should be like this: 你的枚举应该是这样的:

public enum WaitTime 
{
     Min, Max, Med
}

and convert your switch to this: 并将您的开关转换为:

switch ((WaitTime)Enum.Parse(typeof(WaitTime), arg.Replace("/:", string.Empty)))
{
    case WaitTime.Max:
            return 0;
    case WaitTime.Med:
            return 1000;
    case WaitTime.Min:
           return 2000;
    default:
           return 0;
}

Try this: 试试这个:

public enum t
{
  [Description("/t:Min")]  // <<---- set the string equivalent of the enum HERE.
  min = 0,  // <<---- set the return value of the enum HERE.

  [Description("/t:Med")]
  med = 1000,

  [Description("/t:Max")]
  max = 2000
}

You'll also need this handy little class: 你还需要这个方便的小班:

public static class EnumHelper
{
    public static list_of_t = Enum.GetValues(typeof(t)).Cast<t>().ToList();

    // returns the string description of the enum.
    public static string GetEnumDescription(this Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fi.GetCustomAttributes(
            typeof(DescriptionAttribute),
            false);

        if (attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }
}

You can use the decorated enum like this to get what you need: 你可以使用这样的装饰枚举来获得你需要的东西:

public int Option(string arg)
{
  // get the matching enum for the "/t:" switch here.
  var foundItem = 
      EnumHelper.list_of_t.SingleOrDefault(c => c.GetEnumDescription() == arg);

  if (foundItem != null)
  {
    return (int)foundItem;  // <<-- this will return 0, 1000, or 2000 in this example.
  }
  else
  {
    return 0;
  }
}

HTH... HTH ...

Code: 码:

enum YourEnum
{
    Min,
    Med,
    Max
};

void Main(string[] args)
{
    var arg0 = args[0]; // "/t:Min"
    var arg0arr = arg0.Split(':') // { "/t", "Min" }
    var arg0val = arg0arr[1]; // "Min"
    var e = Enum.Parse(typeof(YourEnum), arg0val);
}

Call app: 致电app:

app.exe /t:Min

Correct me if i'm wrong, but isn't the most simple way just to make a dictionary with arg as key and time as value, and use .TryGetValue() and if that fails just return 0? 如果我错了,请纠正我,但这不是最简单的方法,只需用arg作为键和时间作为值来制作字典,并使用.TryGetValue(),如果失败则只返回0?

Someting like this : The arguments.Add could be happening in the constructor, dosn't need to be in the method. 喜欢这样:arguments.Add可能在构造函数中发生,不需要在方法中。

    private static Dictionary<string, int> arguments = new Dictionary<string, int>(5);

    public int Option(string arg)
    {
        arguments.Add("Max", 0);
        arguments.Add("Med", 1000);
        arguments.Add("Min", 2000);

        int value;
        if (arguments.TryGetValue(arg.Replace("/:", string.Empty), out value))
            return value;
        //default
        return 0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM