为什么C#不一致地处理命令行参数? [英] Why does C# handle command line args inconsistently?

查看:115
本文介绍了为什么C#不一致地处理命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,直接从Main()获取命令行参数会忽略exe名称,这与C的传统相反.

In C#, getting command line arguments directly from Main() omits the exe name, contrary to the tradition of C.

通过Environment.GetCommandLineArgs包含相同的命令行参数.

Getting those same command line args via Environment.GetCommandLineArgs includes it.

由于这种明显的不一致,我是否缺少一些合理的逻辑理由?

Is there some good logical reason I'm missing for this apparent inconsistency?

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(string.Format("args.Length = {0}", args.Length));

        foreach(string arg in args)
        {
            Console.WriteLine(string.Format("args = {0}", arg));
        }

        Console.WriteLine("");

        string[] Eargs = Environment.GetCommandLineArgs();
        Console.WriteLine(string.Format("Eargs.Length = {0}", Eargs.Length));
        foreach (string arg in Eargs)
        {
            Console.WriteLine(string.Format("Eargs = {0}", arg));
        }

    }
}

输出:

C:\\ConsoleApplication1\ConsoleApplication1\bin\Debug>consoleapplication1 xx zz aa 
args.Length = 3 
args = xx
args = zz 
args = aa
Eargs.Length = 4 
Eargs = consoleapplication1 
Eargs = xx 
Eargs = zz 
Eargs = aa

推荐答案

因为它不是C,因此与它的约定无关.需要exe名称几乎是一个极端情况.与其他args相比,我需要这样做的次数很少(与其他args相比),IMO则决定省略它.

Because it isn't C and thus isn't tied to it's conventions. Needing the exe name is pretty much an edge case; the small number of times I've needed this (compared to the other args) IMO justifies the decision to omit it.

规范(ECMA334v4,§10.1)中另外要求; (剪到相关部分):

This is additionally demanded in the spec (ECMA334v4, §10.1); (snipping to relevant parts):

10.基本概念

10.1应用程序启动

...

此进入点方法始终命名为Main,并且应具有以下一种: 以下签名:

This entry point method is always named Main, and shall have one of the following signatures:

static void Main() {…} 
static void Main(string[] args) {…} 
static int Main() {…} 
static int Main(string[] args) {…} 

...

•假设args为参数名称.如果args指定的数组的长度大于 零,则数组成员args[0]args[args.Length-1](包括两端)将引用字符串, 称为 应用程序参数 ,这些参数由主机环境提供了实现定义的值 在应用程序启动之前.目的是向应用程序提供之前确定的信息. 从托管环境中其他位置启动应用程序.如果主机环境不具备 提供带有大写和小写字母的字符串,实现应确保 字符串以小写形式接收. [注意:在支持命令行,应用程序,应用程序参数的系统上 对应于通常称为命令行参数的内容. 尾注]

• Let args be the name of the parameter. If the length of the array designated by args is greater than zero, the array members args[0] through args[args.Length-1], inclusive, shall refer to strings, called application parameters, which are given implementation-defined values by the host environment prior to application startup. The intent is to supply to the application information determined prior to application startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase. [Note: On systems supporting a command line, application, application parameters correspond to what are generally known as command-line arguments. end note]

这篇关于为什么C#不一致地处理命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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