摘要:本文将带你了解C#编程之程序处理命令行参数,希望本文对大家学C#/.Net有所帮助。
C#程序通过主函数的args参数接收命令行参数:static void Main(string[] args)
如果一个程序接收到的命令行参数非常多,那手工读取命令行参数无疑是件痛苦的事情。
为此我找到了一篇codeproject上的博客,里面介绍了一个C#程序命令行参数的转换工具类。
//www.codeproject.com/Articles/3111/C-NET-Command-Line-Arguments-Parser
using System;using System.Collections.Specialized;using System.Text.RegularExpressions;
namespace CommandLine.Utility
{
/// <summary>
/// Arguments class
/// </summary>
public class Arguments{
// Variables
private StringDictionary Parameters;
// Constructor
public Arguments(string[] Args)
{
Parameters = new StringDictionary();
Regex Spliter = new Regex(@"^-{1,2}|^/|=|:",
RegexOptions.IgnoreCase|RegexOptions.Compiled);
Regex Remover = new Regex(@"^['""]?(.*?)['""]?$",
RegexOptions.IgnoreCase|RegexOptions.Compiled);
string Parameter = null;
string[] Parts;
// Valid parameters forms:
// {-,/,--}param{ ,=,:}((",')value(",'))
// Examples:
// -param1 value1 --param2 /param3:"Test-:-work"
// /param4=happy -param5 '--=nice=--'
foreach(string Txt in Args)
{
// Look for new parameters (-,/ or --) and a
// possible enclosed value (=,:)
Parts = Spliter.Split(Txt,3);
switch(Parts.Length){
// Found a value (for the last parameter
// found (space separator))
case 1:
if(Parameter != null)
{
if(!Parameters.ContainsKey(Parameter))
{
Parts[0] =
Remover.Replace(Parts[0], "$1");
Parameters.Add(Parameter, Parts[0]);
}
Parameter=null;
}
// else Error: no parameter waiting for a value (skipped)
break;
// Found just a parameter
case 2:
// The last parameter is still waiting.
// With no value, set it to true.
if(Parameter!=null)
{
if(!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}
Parameter=Parts[1];
break;
// Parameter with enclosed value
case 3:
// The last parameter is still waiting.
// With no value, set it to true.
if(Parameter != null)
{
if(!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}
Parameter = Parts[1];
// Remove possible enclosing characters (",')
if(!Parameters.ContainsKey(Parameter))
{
Parts[2] = Remover.Replace(Parts[2], "$1");
Parameters.Add(Parameter, Parts[2]);
}
Parameter=null;
break;
}
}
// In case a parameter is still waiting
if(Parameter != null)
{
if(!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}
}
// Retrieve a parameter value if it exists
// (overriding C# indexer property)
public string this [string Param]
{
get
{
return(Parameters[Param]);
}
}
}
}
(PS:实际使用时命名空间和类名可根据自身项目实际情况修改)
为此我建立了一个工程,测试了这段代码。
测试参数如下:
-param0 -param1 value1 --param2 value2 /param3 value3 =param4 value4 :param5 value5 -param6=param6 -param7:param7 -param8.1 "1234" -param8.2 "1 2 3 4" -param9.1 '1234' -param9.2='1 2 3 4'
各参数项测试内容如下:
1. -param0,只有参数项
2. -param1 value1,有参数项,有参数值
3. --param2 value2,用--标记参数项开头
4. /param value3,用/标记参数项开头
5. =param4 value4,用=标记参数项开头
6. :param5 value5,用:标记参数项开头
7. -param6=param6,用=标记参数项与参数值的关系
8. -param7:param7,用:标记参数项与参数值的关系
9. -param8.1 "1234",用""指定参数
10. -param8.2 "1 2 3 4",用""指定参数(含空格)
11. -param9.1 '1234',用''指定参数
12. -param9.2='1 2 3 4',用''指定参数(含空格)
我的测试程序代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ArgumentTest
{
class Program
{
static void Main(string[] args)
{
Arguments arguments = new Arguments(args);
WriteLinePara(arguments, "param0");
WriteLinePara(arguments, "param1");
WriteLinePara(arguments, "param2");
WriteLinePara(arguments, "param3");
WriteLinePara(arguments, "param4");
WriteLinePara(arguments, "param5");
WriteLinePara(arguments, "param6");
WriteLinePara(arguments, "param7");
WriteLinePara(arguments, "param8.1");
WriteLinePara(arguments, "param8.2");
WriteLinePara(arguments, "param9.1");
WriteLinePara(arguments, "param9.2");
WriteLinePara(arguments, "param_");
Console.ReadLine();
}
static void WriteLinePara(Arguments arguments, string key)
{
if (arguments[key] != null)
{
Console.WriteLine(key + " value: " + arguments[key]);
}
else
{
Console.WriteLine(key + " not defined !");
}
}
}
}
运行结果如下:
param0 value: trueparam1 value: value1param2 value: value2param3 value: value3param4 value: value4param5 value: value5param6 value: param6param7 value: param7param8.1 value: 1234param8.2 value: 1 2 3 4param9.1 value: 1234param9.2 value: 1
param_ not defined !
对这段运行结果的说明如下:
1、判断命令行参数有无参数项,可用
if (arguments[key] != null)
{
Console.WriteLine(key + " value: " + arguments[key]);
}else
{
Console.WriteLine(key + " not defined !");
}
2、param0只有参数项无参数值,我们的代码会将其默认为true
3、param9.2接收到的参数是单引号包围的含空格的字符串,但它无法正确解析,原因是 -param9.2='1 2 3 4' 被当做了四个传入参数,即 -param9.2='1、2、3、4'
以上就介绍了C#.NET的相关知识,希望对C#.NET有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言C#.NET频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号