简体   繁体   English

在Mono.Options中获取实际的选项名称

[英]Get the actual option name in Mono.Options

I am using Mono.Options to parse command line flags in a CLI application. 我正在使用Mono.Options来解析CLI应用程序中的命令行标志。

For convenience, many options have several aliases, eg "o|opt|option" . 为了方便起见,许多选项具有多个别名,例如"o|opt|option" Furthermore, Mono.Options allows one to specify an option using different prefixes, eg -o , /opt or --option . 此外, Mono.Options允许使用不同的前缀来指定选项,例如-o/opt--option

Is there a reasonable way to retrieve the actual name that was supplied for an option during / after parsing? 有没有一种合理的方法来检索在解析过程中/之后为选项提供的实际名称? In my case, that would be "-o" , "/opt" or "--option" , depending on what the user has provided in the command line. 在我的情况下,这将是"-o""/opt""--option" ,具体取决于用户在命令行中提供的内容。

I did not find any reasonable way to do this via Mono.Options ' public interface. 我找不到通过Mono.Options的公共接口执行此操作的任何合理方法。 The best outcome would be to have the actual option name passed into the Action callback upon parsing, but it is just not there. 最好的结果是在解析时将实际的选项名称传递到Action回调中,但实际上不存在。

Mono.Options ' option prototype parsing mechanism is also non-public. Mono.Options的选项原型解析机制也是非公开的。

I ended up manually parsing the prototype and searching the command line arguments for any of the option aliases combined with all possible option prefixes ( - , -- , and / ): 我最终手动解析了原型,并在命令行参数中搜索任何选项别名以及所有可能的选项前缀( ---/ ):

public static string GetProvidedName(string flagPrototype, IEnumerable<string> commandLineArguments)
{
    IEnumerable<string> flagNames = flagPrototype
        .Split('|')
        .Select(value => value.Replace("=", string.Empty).Replace(":", string.Empty))
        .SelectMany(flagName => 
            new string[] 
            {
                $"-{flagName}",
                $"--{flagName}",
                $"/{flagName}"
            });

    return commandLineArguments.First(argument => flagNames.Contains(argument));
}

I hope it will be useful for someone looking to solve the same problem. 我希望这对希望解决相同问题的人有用。

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

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