c# - 传递参数不必担心命令exe c#
问题描述
尝试传递参数而不必考虑参数的顺序。第一行是我想要做的,下面是我能做的。
我想要什么都应该给出相同的结果
test.exe -word1 hello -word2 bye
test.exe -word2 bye -word1 hello
我有的
test.exe hello bye
static void Main(string[] args)
{
console.line(args[0])
console.line(args[1])
}
解决方案
您可以像这样使用字符串输入:
test.exe -word1=hello -word2=bye
test.exe -word2=bye -word1=hello
然后你可以做这样的事情:
static void Main(string[] args)
{
// Single if the param is needed, else SingleOrDefault (to get null if the param. was not found
var word1 = args.Single(c => c.startsWith("-"+nameof(word1))
.Split(new char[] {'='})
.Last();
var word2 = args.Single(c => c.startsWith("-"+nameof(word2))
.Split(new char[] {'='})
.Last();
}
这是伪代码,我没有运行它 - 只是为了给你一个例子。
我什至不会打扰,只是拍拍:https ://github.com/Tyrrrz/CliFx
推荐阅读
- python - aws s3上传的视频文件无法播放,自动下载
- php - 使用共享网络路径将文件上传到目录
- python - 如何单击 pygame 窗口中的特定位置?
- javascript - 由于 Dropzone 溢出隐藏,JQuery Draggable + Droppable 与拖动的对象剪辑
- sql - 我想嵌套两个 sql 命令。第一个 t 依赖于先前查询的结果
- php - 使用循环的多mysql删除功能
- kubernetes - 如果 Kubernetes 命名空间不存在,如何创建它?
- javascript - 提醒用户每个数组值是否为数字
- vue.js - Django rest 框架在 400 Bad request 期间从响应中获取错误消息
- c# - 无限期地等待进程终止或某个状态