尝试读入控制台应用程序的一些命令行参数

时间:2021-02-06 23:17:16

i've got a pretty simple .NET console application. I wish to pass in some command line arguments ... nothing tough there.

我有一个非常简单的.NET控制台应用程序。我希望传递一些命令行参数......没有什么比这更难的了。

Now, the kicker is, i usually have a LIST of items i wish to pass in, for one argument. In this case, a list of files. I'm not sure of the best way to do this.

现在,踢球者,我通常有一个我希望传递的项目列表,一个参数。在这种情况下,文件列表。我不确定这样做的最好方法。

eg. myapp.exe files=aaa.txt,bbb.txt,ccc.txt 

but what about file names with spaces? Should i comma delimate it?

但是有空格的文件名怎么样?我可以用逗号去除它吗?

to make things interesting, i've got other args, which accept single values .. so i'm not sure if i should put all the arg data in double quotes..

为了让事情变得有趣,我有其他的args,它接受单个值..所以我不确定我是否应该将所有arg数据放在双引号中。

eg. myapp.exe files=aaa.txt,bbb.txt formatting=true foo=hello

or

eg myapp.exe files="aaa.txt","bbb.txt" formatting="true" foo="hello"

3 个解决方案

#1


Probably a bit late for the program you are working on, but here's how you could do it using Mono.Options

对于您正在处理的程序可能有点晚了,但是您可以使用Mono.Options来完成它

static void Main(string[] args)
{
    //let's try this mono.options thing shall we?
    string xapfile = null;
    var files = new List<string>();
    var p = new Mono.Options.OptionSet()
    {
        { "xap=", v => xapfile =v },
        { "file:", v => files.Add(v)}
    };
    List<string> extra = p.Parse(args);


    Console.WriteLine(@"
    xap = '{0}', 
    file(s)= {1}", 
         xapfile, 
         string.Join(",", files.ToArray())
    );
    Console.WriteLine(@"
    left overs: {0}", 
        string.Join(Environment.NewLine, extra.ToArray())
    );
    // rest of your Main here

Calling syntax would be

调用语法将是

myapp.exe --file="file 1" --file="file 2" --file="file 3"

myapp.exe --file =“file 1”--file =“file 2”--file =“file 3”

You could even forgo the whole files variable and just treat the left overs as the files, and call it like myapp.exe "file 1" "file 2" "file 3" and pluck the files out of the extra list.

您甚至可以放弃整个文件变量,只需将左侧视图作为文件处理,并将其称为myapp.exe“file 1”“file 2”“file 3”,并将文件从额外列表中删除。

Mono.Options is pretty powerful. You don't even have to use --, it appears you can use /file= or -file=

Mono.Options非常强大。您甚至不必使用 - ,它似乎可以使用/ file =或-file =

Unfortunately, Windows doesn't do globbing for you (unless you're passing the options to a powershell cmdlet), so you have to do it yourself. It's pretty easy though, here's a bit of code I've used in the past to expand *.foo or "c:\temp\*.bar|c:\temp\*.txt"

不幸的是,Windows不为你做通配(除非你将选项传递给powershell cmdlet),所以你必须自己做。这很简单,这里有一些我过去用来扩展* .foo或“c:\ temp \ * .bar | c:\ temp \ * .txt”的代码。

Also, unless you wrap the filenames in talkies, I probably wouldn't comma separate the list, as comma is valid in a filename. You know that one is going to bite you one day :-) Pipe makes a good choice, but only if you wrap the whole expression in talkies, otherwise windows thinks you're doing piping. Ah, the joys of command line processing :-)

此外,除非你将文件名包装在有声电影中,否则我可能不会用逗号分隔列表,因为逗号在文件名中有效。你知道有一天会有人咬你:-) Pipe是一个很好的选择,但只有你把整个表达包装在有声电影中,否则windows会认为你正在做管道。啊,命令行处理的乐趣:-)

#2


Each argument, if it contains any spaces must be enclosed in quotes. Otherwise I would leave quotes out of it.

每个参数,如果包含任何空格必须用引号括起来。否则我会留下引号。

#3


Given that your shell probably supports wild card globbing it is best to allow the input files to be a plain space separated list and to assume that if a file name contains spaces that it will have been escaped already either by surrounding with quotes or via \ characters. By plain I mean contain no --f or /f style switches.

鉴于你的shell可能支持通配符通配符,最好允许输入文件是一个空格分隔的列表,并假设如果一个文件名包含空格,它将通过用引号或\ _字符包围来转义。简单来说,我的意思是不包含-f或/ f样式开关。

A slightly less common technique, but nor supported by all libraries doing command line parsing, is to allow multiple entries of the form -f foo.txt -f bar.txt. This does not play well with command line globbing (unlike the above solution).

一种稍微不太常见的技术,但是所有执行命令行解析的库都不支持,是允许多个条目形式-f foo.txt -f bar.txt。这与命令行通配不兼容(与上述解决方案不同)。

#1


Probably a bit late for the program you are working on, but here's how you could do it using Mono.Options

对于您正在处理的程序可能有点晚了,但是您可以使用Mono.Options来完成它

static void Main(string[] args)
{
    //let's try this mono.options thing shall we?
    string xapfile = null;
    var files = new List<string>();
    var p = new Mono.Options.OptionSet()
    {
        { "xap=", v => xapfile =v },
        { "file:", v => files.Add(v)}
    };
    List<string> extra = p.Parse(args);


    Console.WriteLine(@"
    xap = '{0}', 
    file(s)= {1}", 
         xapfile, 
         string.Join(",", files.ToArray())
    );
    Console.WriteLine(@"
    left overs: {0}", 
        string.Join(Environment.NewLine, extra.ToArray())
    );
    // rest of your Main here

Calling syntax would be

调用语法将是

myapp.exe --file="file 1" --file="file 2" --file="file 3"

myapp.exe --file =“file 1”--file =“file 2”--file =“file 3”

You could even forgo the whole files variable and just treat the left overs as the files, and call it like myapp.exe "file 1" "file 2" "file 3" and pluck the files out of the extra list.

您甚至可以放弃整个文件变量,只需将左侧视图作为文件处理,并将其称为myapp.exe“file 1”“file 2”“file 3”,并将文件从额外列表中删除。

Mono.Options is pretty powerful. You don't even have to use --, it appears you can use /file= or -file=

Mono.Options非常强大。您甚至不必使用 - ,它似乎可以使用/ file =或-file =

Unfortunately, Windows doesn't do globbing for you (unless you're passing the options to a powershell cmdlet), so you have to do it yourself. It's pretty easy though, here's a bit of code I've used in the past to expand *.foo or "c:\temp\*.bar|c:\temp\*.txt"

不幸的是,Windows不为你做通配(除非你将选项传递给powershell cmdlet),所以你必须自己做。这很简单,这里有一些我过去用来扩展* .foo或“c:\ temp \ * .bar | c:\ temp \ * .txt”的代码。

Also, unless you wrap the filenames in talkies, I probably wouldn't comma separate the list, as comma is valid in a filename. You know that one is going to bite you one day :-) Pipe makes a good choice, but only if you wrap the whole expression in talkies, otherwise windows thinks you're doing piping. Ah, the joys of command line processing :-)

此外,除非你将文件名包装在有声电影中,否则我可能不会用逗号分隔列表,因为逗号在文件名中有效。你知道有一天会有人咬你:-) Pipe是一个很好的选择,但只有你把整个表达包装在有声电影中,否则windows会认为你正在做管道。啊,命令行处理的乐趣:-)

#2


Each argument, if it contains any spaces must be enclosed in quotes. Otherwise I would leave quotes out of it.

每个参数,如果包含任何空格必须用引号括起来。否则我会留下引号。

#3


Given that your shell probably supports wild card globbing it is best to allow the input files to be a plain space separated list and to assume that if a file name contains spaces that it will have been escaped already either by surrounding with quotes or via \ characters. By plain I mean contain no --f or /f style switches.

鉴于你的shell可能支持通配符通配符,最好允许输入文件是一个空格分隔的列表,并假设如果一个文件名包含空格,它将通过用引号或\ _字符包围来转义。简单来说,我的意思是不包含-f或/ f样式开关。

A slightly less common technique, but nor supported by all libraries doing command line parsing, is to allow multiple entries of the form -f foo.txt -f bar.txt. This does not play well with command line globbing (unlike the above solution).

一种稍微不太常见的技术,但是所有执行命令行解析的库都不支持,是允许多个条目形式-f foo.txt -f bar.txt。这与命令行通配不兼容(与上述解决方案不同)。