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

发布于 2024-07-26 22:00:18 字数 498 浏览 1 评论 0原文

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

现在,最关键的是,我通常有一个我希望传递的项目列表,用于一个参数。 在本例中,是文件列表。 我不确定执行此操作的最佳方法。

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

但是带空格的文件名又如何呢? 我应该用逗号分隔它吗?

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

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

或者

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

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

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..

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不及他 2024-08-02 22:00:18

对于您正在开发的程序来说可能有点晚了,但是您可以使用 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

调用语法为

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

你甚至可以放弃整个 files 变量而只处理剩下的作为文件,并将其命名为 myapp.exe "file 1" "file 2" "file 3" 并将文件从额外列表中取出。

Mono.Options 非常强大。 你甚至不必使用 --,看来你可以使用 /file=-file=

不幸的是,Windows 不会为你做通配符(除非你将选项传递给 powershell cmdlet),因此您必须自己执行此操作。 不过这很简单,这里一些代码我过去曾使用过扩展 *.foo"c:\temp\*.bar|c:\temp\*.txt"

另外,除非你将文件名包装在有声电影中,我可能不会用逗号分隔列表,因为逗号在文件名中是有效的。 你知道有一天它会咬你:-) Pipe 是一个不错的选择,但前提是你将整个表达式包裹在有声电影中,否则 Windows 会认为你正在做 Pipeline。 啊,命令行处理的乐趣:-)

Probably a bit late for the program you are working on, but here's how you could do it using 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"

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.

Mono.Options is pretty powerful. You don't even have to use --, it appears you can use /file= or -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"

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 :-)

感情洁癖 2024-08-02 22:00:18

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

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

失而复得 2024-08-02 22:00:18

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

一种不太常见的技术是允许 -f foo.txt -f bar.txt 形式的多个条目,但并非所有进行命令行解析的库都支持。 这对于命令行通配符来说效果不佳(与上面的解决方案不同)。

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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文