添加"--help" C#控制台应用程序的参数 [英] Adding "--help" parameter to C# console application

查看:316
本文介绍了添加"--help" C#控制台应用程序的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过命令行使用的几乎所有.exe都有一个"--help"命令调用的帮助功能.

Almost all the .exe's I use via command line have a help function that gets called by the "--help" command.

如何在C#中实现此功能?就像检查args []中的参数是否为字符串"--help"一样简单吗?

How do I implement this in C#? Is it as simple as checking whether or not the parameter in args[] is the string "--help"??

推荐答案

一个用于处理多种文化的命令行的C#代码段是...

A C# snippet for processing the command line across multiple cultures is...

        string[] args = Environment.GetCommandLineArgs();
        if (args.Length == 2)
        {
            if (args[1].ToLower(CultureInfo.InvariantCulture).IndexOf("help", System.StringComparison.Ordinal) >= 0)
            {
                // give help
            }
        }

检测逻辑可以与?"组合或者 "/?"或涵盖所有预期情况的任何其他组合.

The detection logic can be combined with "?" or "/?" or any other combination that covers all expected cases.

注意:当您从环境中获取参数时,加载器将填充arg [0].第一个用户"参数在arg [1]中.

NOTE: when you get the arguments from the Environment, arg[0] is populated by the loader. The first 'user' argument is in arg[1].

这篇关于添加"--help" C#控制台应用程序的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆