Vb.net控制台应用程序解析命令行参数

编程入门 行业动态 更新时间:2024-10-27 13:30:06
本文介绍了Vb控制台应用程序解析命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在vb 2013中解析数据的知识非常有限,希望有更多经验的人可以提供建议: 概述 我想在我的控制台应用程序中添加三个命令行开关和值,然后将它们解析为变量,以便在我的程序中继续使用。命令行选项有望如下:

my.exe / d dateserial / p plaintext

my.exe / d dateserial / c cyphertext

这些选项取决于程序是加密还是取消加密任何给定的字符串。我还想加入一些基本的错误处理,比如if / c / p开关存在于同一命令行中,停止程序并显示用法。 我的项目目前看起来像这样:

Dim dtext As String = 字符串 .Empty ' 日期序列字符串 Dim ptext As String = 字符串 .Empty ' 纯文本字符串 Dim ctext As String = 字符串 .Empty ' cypher text string Dim clArgs() As String = Environment.GetCommandLineArgs() 如果 clArgs.Count()= 5 然后 对于 index 作为 整数 = 1 要 3 步骤 1 如果 clArgs(index)= d /; 然后 dtext = clArgs(索引+ 1 ) MsgBox(dtext)' 确保变量填充正确。 其他 Console.WriteLine( 用法:my.exe / d dateserial / p plaintext或my.exe / d dateserial / c cyphertext) 结束 如果 下一步 结束 如果

代码片段仅在上方部分完成任务,所以提前感谢任何可以建议更完整和更强大的解决方案的人。

解决方案

看看Regex.Split(): b $ b msdn.microsoft/en-us/librar y / system.text.regularexpressions.regex.split%28v = vs.110%29.aspx [ ^ ] 我认为这就是你需要的。 您可以执行以下操作:

Dim yourArgs() as String = Regex.Split(clArgs , /) 对于 每个 arg as String yourArgs if arg.Contains( c)然后 ' 此处的代码 结束 如果 下一步

这只是一个如何使用正则表达式的快速而肮脏的例子.split()和.Contains()。有更优雅的方法,你应该看看String.Substring()和String.Replace()。 - Pete

请参阅我的文章,以及我参考和推荐的另一篇文章:基于枚举的命令行实用程序 [ ^ ]。 我提供了非常易于使用的库。现在,以防万一:请不要告诉我这是C#,而不是VB.NET。首先,在.NET中,这绝对不重要;与VB.NET相比,C#总是更好,因为它是标准化的。此外,您可以自动将任何程序集转换为VB.NET,质量非常好(特别是使用ILSpy)。
-SA

谢谢你的想法,他们非常感谢。这是一个vb应用程序。然而,在进一步搜索和调整参数后,我使用以下代码实现了所需的结果。我还想整合一些基本的错误处理,因为我的控制台程序只有5个命令行参数并且需要修复这些字段,我想出了这个原始但有效的解决方案:

Dim dtext As String = 字符串 .Empty Dim ptext 作为 字符串 = 字符串 .Empty Dim ctext 作为 字符串 = 字符串 .Empty Dim clArgs() As String = Environment.GetCommandLineArgs() 如果 clArgs.Count()< ;> 5 然后 Console.WriteLine( 正确用法:/ p plaintext / d dateserial或) Console.WriteLine( 正确用法:/ c cyphertext / d dateserial) 结束 如果 如果 不(clArgs.Contains( / p)或 clArgs.Contains( / c))和(clArgs.Contains( / d)) 然后 退出 Sub 结束 如果 如果 clArgs.Contains( / p)和 clArgs.Contains( / d)然后 对于 index 作为 整数 = 1 3 步骤 2 如果 clArgs(index)= / p 然后 ptext = clArgs(index + 1 ) dtext = clArgs(index + 3 ) Console.WriteLine(ptext) Console.WriteLine(dtext) 结束 如果 下一步 结束 如果 如果 clArgs.Contains( / c)和 clArgs.Contains( / d)然后 对于 index 作为 整数 = 1 要 3 步骤 2 如果 clArgs(index)= / c 然后 ctext = clArgs(index + 1 ) dtext = clArgs(索引+ 3 ) Console.WriteLine(ctext) Console.WriteLine (dtext) 结束 如果 下一步 结束 如果

Hi, I have limited very limited knowledge on parsing data in vb 2013 and hope someone with more experience can advise: Overview I would like to add three command line switches and values to my console application and then parse them into variables, for onward use within my program. The command line options would hopefully look like this:

my.exe /d dateserial /p plaintext

or

my.exe /d dateserial /c cyphertext

These options depend on whether the program was encrypting or de-crypting any given string. I would also like in incorporate some basic error handling, like if /c /p switches exist on the same command line, stop the program and display usage. My project currently looks like this:

Dim dtext As String = String.Empty 'date serial string Dim ptext As String = String.Empty 'plain text string Dim ctext As String = String.Empty 'cypher text string Dim clArgs() As String = Environment.GetCommandLineArgs() If clArgs.Count() = 5 Then For index As Integer = 1 To 3 Step 1 If clArgs(index) = "d/"; Then dtext = clArgs(index + 1) MsgBox(dtext) ' To ensure variables get populated correctly. Else Console.WriteLine("Usage: my.exe /d dateserial /p plaintext or my.exe /d dateserial /c cyphertext") End If Next End If

The code snip-it above only partly completes the task, so thanks in advance to anyone that can advise on a more complete and robust solution.

解决方案

Have a look at Regex.Split(): msdn.microsoft/en-us/library/system.text.regularexpressions.regex.split%28v=vs.110%29.aspx[^] I think that's what you'll need. You can do stuff like:

Dim yourArgs() as String = Regex.Split(clArgs, "/") For Each arg as String in yourArgs if arg.Contains("c ") Then ' Your code here End If Next

This is just a quick and dirty example of how to use regex.split() and .Contains(). There are more elegant ways of doing this, and you should also have a look at String.Substring() and String.Replace(). - Pete

Please see my article, and also another article I reference and recommend in it: Enumeration-based Command Line Utility[^]. I provide extremely easy-to-use library. Now, just in case: please don't tell me "this is C#, not VB.NET". First, in .NET, this is absolutely not important; C# is always better because it is standardized, in contrast to VB.NET. Besides, you can automatically translate any assembly to VB.NET with very good quality (use ILSpy, in particular).
—SA

Thank you for the ideas guys, they were very much appreciated. This is a vb app. However after further googling and tweaking of the parameters, I achieved the desired result with the following code. I also wanted to incorporate some basic error handling, so as my console program would only ever have 5 command line args and these fields needed to be fixed, I came up with this primitive, but effective solution:

Dim dtext As String = String.Empty Dim ptext As String = String.Empty Dim ctext As String = String.Empty Dim clArgs() As String = Environment.GetCommandLineArgs() If clArgs.Count() <> 5 Then Console.WriteLine("Correct usage:""/p plaintext /d dateserial""or") Console.WriteLine("Correct usage:""/c cyphertext /d dateserial") End If If Not (clArgs.Contains("/p") Or clArgs.Contains("/c")) And (clArgs.Contains("/d")) Then Exit Sub End If If clArgs.Contains("/p") And clArgs.Contains("/d") Then For index As Integer = 1 To 3 Step 2 If clArgs(index) = "/p" Then ptext = clArgs(index + 1) dtext = clArgs(index + 3) Console.WriteLine(ptext) Console.WriteLine(dtext) End If Next End If If clArgs.Contains("/c") And clArgs.Contains("/d") Then For index As Integer = 1 To 3 Step 2 If clArgs(index) = "/c" Then ctext = clArgs(index + 1) dtext = clArgs(index + 3) Console.WriteLine(ctext) Console.WriteLine(dtext) End If Next End If

更多推荐

Vb.net控制台应用程序解析命令行参数

本文发布于:2023-11-13 21:54:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   命令行   应用程序   参数   Vb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!