写个C#命令行参数解析的小工具
最近测试工作做的比较多因此时常要创建一些控制台类型的应用程序。因为程序有不同的参数开关,需要在程序启动的时候通过命令行来给程序传递各种开关和参数。直接操作args有些不方便,所以就写了个解析参数的小工具来处理各种参数。
参数实体:
public class CommandLineArgument
{
List<CommandLineArgument> _arguments; int _index; string _argumentText; public CommandLineArgument Next
{
get {
if (_index < _arguments.Count - ) {
return _arguments[_index + ];
} return null;
}
}
public CommandLineArgument Previous
{
get {
if (_index > )
{
return _arguments[_index - ];
} return null;
}
}
internal CommandLineArgument(List<CommandLineArgument> args, int index, string argument)
{
_arguments = args;
_index = index;
_argumentText = argument;
} public CommandLineArgument Take() {
return Next;
} public IEnumerable<CommandLineArgument> Take(int count)
{
var list = new List<CommandLineArgument>();
var parent = this;
for (int i = ; i < count; i++)
{
var next = parent.Next;
if (next == null)
break; list.Add(next); parent = next;
} return list;
} public static implicit operator string(CommandLineArgument argument)
{
return argument._argumentText;
} public override string ToString()
{
return _argumentText;
}
}
参数解析器:
public class CommandLineArgumentParser
{ List<CommandLineArgument> _arguments;
public static CommandLineArgumentParser Parse(string[] args) {
return new CommandLineArgumentParser(args);
} public CommandLineArgumentParser(string[] args)
{
_arguments = new List<CommandLineArgument>(); for (int i = ; i < args.Length; i++)
{
_arguments.Add(new CommandLineArgument(_arguments,i,args[i]));
} } public CommandLineArgument Get(string argumentName)
{
return _arguments.FirstOrDefault(p => p == argumentName);
} public bool Has(string argumentName) {
return _arguments.Count(p=>p==argumentName)>;
}
}
在项目中引入这两个类就可以在Main函数里对args做相应的解析和操作了。
例如有控制台应用Example,在命令行中输入:
Example.exe -u MrJson -p admin123
在Example的Main函数里处理args:
static void Main(string[] args)
{
var arguments = CommandLineArgumentParser.Parse(args); if (arguments.Has("-u"))
{
Console.WriteLine("用户名:{0}", arguments.Get("-u").Next);
} if (arguments.Has("-p"))
{
Console.WriteLine("密码:{0}", arguments.Get("-p").Next);
}
}
如果参数后面要传多个值,例如下面这个例子,-chpwd参数需要两个参数:
Example.exe -chpwd admin888 admin999
那么,就可以这样处理:
if(arguments.Has("-chpwd"))
{
var arg = arguments.Get("-chpwd");
var oldPwd = arg.Take();
var newPwd = arg.Take().Take();
// 或者
var pwds = arg.Take();
oldPwd = pwds.First();
newPwd = pwds.Last();
Console.WriteLine("原密码:{0} 新密码:{1}", oldPwd, newPwd);
}
That's all.
写个C#命令行参数解析的小工具的更多相关文章
- python命令行参数解析模块argparse和docopt
http://blog.csdn.net/pipisorry/article/details/53046471 还有其他两个模块实现这一功能,getopt(等同于C语言中的getopt())和弃用的o ...
- golang-flag - 命令行参数解析
flag - 命令行参数解析 在写命令行程序(工具.server)时,对命令参数进行解析是常见的需求.各种语言一般都会提供解析命令行参数的方法或库,以方便程序员使用.如果命令行参数纯粹自己写代码解析, ...
- Python 中命令行参数解析工具 docopt 安装和应用
什么是 docopt? 1.docopt 是一种 Python 编写的命令行执行脚本的交互语言. 它是一种语言! 它是一种语言! 它是一种语言! 2.使用这种语言可以在自己的脚本中,添加一些规则限制. ...
- gflags命令行参数解析
gflags库是google开源的命令行参数解析工具. 安装 官方没有提供二进制库,但是Debian/Ubuntu平台本身提供了二进制库,可以直接git clone https://github.co ...
- [Go] 命令行参数解析包(flag 包)使用详解
Go 的 flag 包可以解析命令行的参数. 一.命令行语法 命令行语法主要有以下几种形式: cmd -flag // 只支持bool类型 cmd -flag=xxx cmd -flag ...
- $命令行参数解析模块argparse的用法
argparse是python内置的命令行参数解析模块,可以用来为程序配置功能丰富的命令行参数,方便使用,本文总结一下其基本用法. 测试脚本 把以下脚本存在argtest.py文件中: # codin ...
- Google开源命令行参数解析库gflags
Google开源命令行参数解析库gflags http://blog.csdn.net/lming_08/article/details/25072899 CMDLINE的解析 http://blog ...
- PHP 命令行参数解析工具类
<?php/** * 命令行参数解析工具类 * @author guolinchao * @email luoyecb@163.com */class CommandLine{ // store ...
- Python命令行参数解析模块getopt使用实例
Python命令行参数解析模块getopt使用实例 这篇文章主要介绍了Python命令行参数解析模块getopt使用实例,本文讲解了使用语法格式.短选项参数实例.长选项参数实例等内容,需要的朋友可以参 ...
随机推荐
- Finger Trees: A Simple General-purpose Data Structure
http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...
- ASP.Net MVC开发基础学习笔记:二、HtmlHelper与扩展方法
一.一个功能强大的页面开发辅助类—HtmlHelper初步了解 1.1 有失必有得 在ASP.Net MVC中微软并没有提供类似服务器端控件那种开发方式,毕竟微软的MVC就是传统的请求处理响应的回归. ...
- 《Node web开发》笔记
还是因为学习kibana,才开始了解node. Node是一种基于事件驱动的异步系统,基于Chrome的引擎V8. Node中由于大量的使用模块,因此出现了很多开源模块,有点像java社区的样子. 笔 ...
- The currently selected variant "arm-debug" uses split APKs, but none of the 1 split apks are compatible with the current device with density "213" and ABIs "x86".
出现这种错误一般是在电脑上用模拟器运行APK的吧. 可以在build.gradle中这样配置下: android{ ... defaultConfig { applicationId "XX ...
- HTML5新增标签与属性
目录 一.HTML5新增属性 1.1.contextmenu 1.2.contentEditable 1.3.hidden 1.4.draggable 1.5.data-* 1.6.placehold ...
- fix orphaned user
orphan user是某个数据库的user,只有user name而没有login,即,在存在于sys.database_principals 中, 而不存在于 sys.server_princip ...
- Execute Sql Task 的Result DataSet如何返回
Execute Sql Task的Result DataSet 主要有以下四种,当Execute Sql Task返回结果之后,需要使用SSIS Variable 来接收数据. 例子中使用的数据表代码 ...
- Sql Server系列:索引基础
1 索引概念 索引用于快速查找在某个列中某个特定值的行,不使用索引,数据库必须从第1条记录开始读完整个表,知道找出需要的行.表越大,查询数据所花费的时间越多.如果表中查询的列有索引,数据库能快速到达一 ...
- jQuery源码分析系列(38) : 队列操作
Queue队列,如同data数据缓存与Deferred异步模型一样,都是jQuery库的内部实现的基础设施 Queue队列是animate动画依赖的基础设施,整个jQuery中队列仅供给动画使用 Qu ...
- Android数据存储之Sqlite的介绍及使用
前言: 本来没有打算整理有关Sqlite数据库文章的,最近一直在研究ContentProvider的使用,所有觉得还是先对Sqlite进行一个简单的回顾,也方便研究学习ContentProvider. ...