Vb.net控制台应用程序解析命令行参数 [英] Vb.net Console Application Parsing Command Line Parameters

查看:106
本文介绍了Vb.net控制台应用程序解析命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在vb.net 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)< span class =code-comment>'
确保变量填充正确。
其他
Console.WriteLine( 用法:my.exe / d dateserial / p plaintext或my.exe / d dateserial / c cyphertext
结束 如果
下一步
结束 如果





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

解决方案

看看Regex.Split():< br $> b $ b

https://msdn.microsoft.com/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.net应用程序。然而,在进一步搜索和调整参数后,我使用以下代码实现了所需的结果。我还想整合一些基本的错误处理,因为我的控制台程序只有5个命令行参数并且需要修复这些字段,我想出了这个原始但有效的解决方案:

  Dim  dtext  As   String  = 字符串 .Empty 
Dim ptext 作为 字符串 = 字符串 .Empty
Dim ctext 作为 字符串 = 字符串 .Empty

Dim clArgs() As < span class =code-keyword> 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.net 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():

https://msdn.microsoft.com/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.net 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控制台应用程序解析命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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