如何使用不同的分隔符读取 ParamStr 的值? [英] How to read value of ParamStr with different deliminators?

查看:28
本文介绍了如何使用不同的分隔符读取 ParamStr 的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您阅读ParamStr() 时,它由每个参数之间的空格分隔.但是,我已经看到许多命令行参数接受参数名称与其配对值之间的空格,同时还接受等号 = 甚至没有分隔符(只是以参数名称为前缀)或没有价值.

When you read ParamStr(), it's deliminated by spaces between each parameter. However, I've seen many command-line arguments which accept a space between the parameter name and its paired value, while also accepting an equals = sign and even no deliminator (just prefixed with the param name) or no value.

以下是一些可能的参数字符串示例:

Here's some examples of possible param strings:

-name value
/name value
-name=value
/name=value
-namevalue
/namevalue
-name -nextname
/name /nextname
-name="value with spaces"
/name="value with spaces"

...等等

我想做的是两件事都相关...检查参数名称是否存在,并读取参数的值.例如...

What I would like to do is two things both related... Check if a parameter name exists, and read the value of the parameter. For example...

if ParamExists('ParamName') then
  SomeString:= ParamValue('ParamName')
else
  SomeString:= 'SomeOtherString';

Delphi 中有什么东西可以做到这一点吗?如果没有,我该怎么做?我在搜索时找到的所有内容都将我引向相同的基本示例:

Is there something in Delphi which can do this? If not, how do I do this? Everything I find when searching for this just leads me to the same basic example:

for i := 0 to ParamCount do
  ShowMessage(ParamStr(i));

它还需要区分大小写.我正在寻找特别像 OSQL 和类似命令行工具使用的东西,其中-s"可能与-S"不同.

It also needs to be case sensitive. I'm looking for something in particular like OSQL and similar command-line tools use where '-s' could be different from '-S'.

问题是,如果我使用空格作为分隔符,我不知道如何识别它何时是前一个参数的一部分,因为它用空格将它们分开.我该如何解决这个问题?

The problem is that if I use a space as a deliminator, I have no clue how to recognize when it's part of the previous parameter, because it splits them by spaces. How do I get around this?

我确定这也有一个标准术语,它是命令行参数的常见格式.但我不知道如何仅使用 ParamStr 正确读取它们.ParamStr 似乎没有达到它通常的用途.

I'm sure there's a standard term for this too, it's the common formatting of command-line arguments. But I don't know how to read them properly using just ParamStr. It seems ParamStr falls short of what it's usually used for.

明确地说,我不一定需要支持上述每个示例 - 这些只是我以前见过的示例.

To be clear, I don't necessarily need to support every above example - those are just examples I've seen before.

推荐答案

ParamStr()(因此 FindCmdLineSwitch())不够灵活,无法处理所有你展示的例子.您必须调用 Win32 API GetCommandLine() 函数并手动解析它.

ParamStr() (and consequently FindCmdLineSwitch()) is not flexible enough to handle all of the examples you have shown. You will have to call the Win32 API GetCommandLine() function and parse it manually.

这篇关于如何使用不同的分隔符读取 ParamStr 的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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