简体   繁体   English

命令行参数解析

[英]Command line parameter parsing

I'm trying to create some kind of auto login for my application via the command line. 我正在尝试通过命令行为我的应用程序创建某种自动登录。 For this I've thought of the following way: 为此,我想到了以下方法:

myapp.exe /autologin -u "Username" -p "Password"

Now first of all: Is this the correct approach (in terms of naming and switches?) and second: how can I make the parsing of this? 现在首先:这是正确的方法(在命名和切换方面吗?),其次:我如何对此进行解析?

I've tried it like that, but failed (or at least I think I failed because I have to manually do string operations): 我已经尝试过了,但是失败了(或者至少我认为我失败了,因为我必须手动执行字符串操作):

    internal static void SetStartupArguments(List<string> arguments)
    {
        IsApplicationWarmup = arguments.Contains("/warmup");
        IsApplicationAutoLogin = arguments.Contains("/autologin");

        if (IsApplicationAutoLogin)
        {
            int autoLoginIndex = arguments.FindIndex(0, str => str == "/autologin");

            if (arguments.Count >= autoLoginIndex + 1)
            {
                AutoLoginUser = arguments[autoLoginIndex + 1];
                AutoLoginPassword = string.Empty;
            }
        }
    }

The other thing is, that this is quite error prone. 另一件事是,这很容易出错。 I can't tell if the -u or the -p switch comes first, so my fear is that I end up using the password as the username and vice-versa. 我无法确定-u或-p开关是第一个出现的,所以我担心我最终会使用密码作为用户名,反之亦然。

I would suggest using a 3rd party library for command-line argument parsing - it can make your life much simpler. 我建议使用第3方库进行命令行参数解析-它可以使您的生活变得更加简单。

For example, in Noda Time we use "Command Line Parser Library" , which we actually embed into the source rather than adding it as an assembly reference. 例如,在Noda Time中,我们使用“命令行解析器库” ,实际上将其嵌入到源代码中,而不是将其添加为程序集引用。 It's very simple to use - you just provide a class with attributes to say which command line option corresponds to which property. 使用非常简单-您只需提供一个带有属性的类即可说明哪个命令行选项与哪个属性相对应。 You could look at the options for our TZDB compiler as an example. 您可以以TZDB编译器选项为例。

Your needs may vary of course, but it's likely that there's a library which will satisfy them - and if there isn't, that suggests that perhaps your requirements are actually too complicated for a useful command line, and you may want some other way of configuring your application, eg via a file which is itself specified on the command line. 当然,您的需求可能会有所不同,但是可能有一个可以满足它们的库-如果没有,这表明您的要求实际上对于一个有用的命令行而言过于复杂,并且您可能希望采用其他方式配置您的应用程序,例如通过本身在命令行上指定的文件。

I'd use something like https://github.com/fschwiet/ManyConsole its an extension of NDesk.Options and really easy to use for parsing command lines. 我会使用类似https://github.com/fschwiet/ManyConsole的东西,它是NDesk.Options的扩展,非常易于使用,可以用于解析命令行。

Example: 例:

   string data = null;
   bool help   = false;
   int verbose = 0;
   var p = new OptionSet () {
    { "file=",      v => data = v },
    { "v|verbose",  v => { ++verbose } },
    { "h|?|help",   v => help = v != null },
   };
   List<string> extra = p.Parse (args);

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

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