Command line parser

本文介绍了一个用于解析命令行参数的自定义类 CCmdLineParser,支持多种输入格式,包括键值对、长命令行和 Unicode 字符串。通过实例演示了如何使用该类进行参数解析,并提供了定制化选项,如忽略大小写、指定分隔符等。



Introduction

Getting list of arguments from command line is a common task which is required by a lot of applications. However, there is no standard solution (as far as I know ;). So I wrote class CCmdLineParser, which can parse arguments fromcommand line, if they are typed in folowing form: 

  • /Key 
  • /KeyWithValue:Value 
  • /KeyWithComplexValue:"Some really complex value of /KeyWithComplexValue"

Of course, multiple keys, Unicode and long (up to 32Kb) command lines are supported.

This implementation requires the MFC or ATL CString class, or some clone with similar interface as well as the STL class map.

Usage

First, you should construct the object and call the  Parse  function (from constructor or
CCmdLineParser parser(::GetCommandLine());
or
CCmdLineParser parser;
parser.Parse(_T("/Key /Key2:Val /Key3:\"Complex-Value\" -Key4"));

Then, there are two ways of working with results. You can check if some particular key was specified in the commandline:

if(parser.HasKey(_T("Key")) {
	// Do some stuff
}
if(parser.HasKey(_T("Key2")) {
	LPCTSTR szKey2Value = parser.GetVal(_T("Key2"));
	// Do something with value of Key2
}

LPCTSTR szKey3Value = parser.GetVal(_T("Key3"));
if(szKey3Value) {
	// There was key "Key3" in input,  
} else {
	// No key "Key3" in input
}

LPCTSTR szKey4Value = parser.GetVal(_T("Key4"));
// Key4 was found in input, but since no value was specified, 
// szKey4Value points to empty string

Another way to use is to enumerate all keys in command line:

CString sKey, sValue;

CCmdLineParser::POSITION pos = parser.getFirst();
while(!realParser.isLast(pos)) {
	realParser.getNext(pos, sKey, sValue);
	// Do something with current key and value
}

Customization and "how it works"

Repeated keys

If several different values are specified with same key, only the first value is stored. So, if user passes  command   line /Add:One /Add:Two /Add:Two  will be ignored and will not be added to parsed list.

Case sensitive/insensitive

By default, keys are  not  case-sensitive. So,  /KeyOne  is equal to  -keyONE . This is done by converting all keys to lowercase before storing them. If you want to change this behaviour, call  setCaseSensitive(true)  or call the constructor with the second argument set to true:
CCmdLineParser parser(::GetCommandLine(), true);
This will switch the parser to case-sensitive mode, and if the user passes  -key , then  GetKey(_T("Key"))  will return false

Syntax

Formally,  command   line  should be in following form:
CommandLine::=[<Key> [,<Key>...]]
<Key>::=<Delimeter>KeyName[<Separator><Value>]
<Value> ::= { KeyValue | <QuoteChar>Quoted Key Value<QuoteChar>} ][
<Delimeter>::= { - | / }
<Separator>::= { : }
<QuoteChar>::= { " }

Values for <Delimeter><Separator> and <QuoteChar> are stored in static variables m_sDelimeters,m_sValueSep and m_sQuotes respectively. If you want to change them (for instance, allow user to specify quoted values in apostrophes), you can do it in the beginning of CmdLineParser.cpp:

const TCHAR CCmdLineParser::m_sQuotes[] = _T("\"\'");
Note: If you want to change  m_sDelimeters , space must be the first character of this string. Also, if you have your own  CString  class with other name than  CString , you can change it in the beginning of  CmdLineParser.h :
typedef MyOwnCString CCmdLineParser_String;
That's it! ;)



转自:http://www.codeproject.com/Articles/2773/Command-line-parser

Introduction

Getting list of arguments from command line is a common task which is required by a lot of applications. However, there is no standard solution (as far as I know ;). So I wrote class CCmdLineParser, which can parse arguments fromcommand line, if they are typed in folowing form: 

  • /Key 
  • /KeyWithValue:Value 
  • /KeyWithComplexValue:"Some really complex value of /KeyWithComplexValue"

Of course, multiple keys, Unicode and long (up to 32Kb) command lines are supported.

This implementation requires the MFC or ATL CString class, or some clone with similar interface as well as the STL class map.

Usage

First, you should construct the object and call the  Parse  function (from constructor or
CCmdLineParser parser(::GetCommandLine());
or
CCmdLineParser parser;
parser.Parse(_T("/Key /Key2:Val /Key3:\"Complex-Value\" -Key4"));

Then, there are two ways of working with results. You can check if some particular key was specified in the commandline:

if(parser.HasKey(_T("Key")) {
	// Do some stuff
}
if(parser.HasKey(_T("Key2")) {
	LPCTSTR szKey2Value = parser.GetVal(_T("Key2"));
	// Do something with value of Key2
}

LPCTSTR szKey3Value = parser.GetVal(_T("Key3"));
if(szKey3Value) {
	// There was key "Key3" in input,  
} else {
	// No key "Key3" in input
}

LPCTSTR szKey4Value = parser.GetVal(_T("Key4"));
// Key4 was found in input, but since no value was specified, 
// szKey4Value points to empty string

Another way to use is to enumerate all keys in command line:

CString sKey, sValue;

CCmdLineParser::POSITION pos = parser.getFirst();
while(!realParser.isLast(pos)) {
	realParser.getNext(pos, sKey, sValue);
	// Do something with current key and value
}

Customization and "how it works"

Repeated keys

If several different values are specified with same key, only the first value is stored. So, if user passes  command   line /Add:One /Add:Two /Add:Two  will be ignored and will not be added to parsed list.

Case sensitive/insensitive

By default, keys are  not  case-sensitive. So,  /KeyOne  is equal to  -keyONE . This is done by converting all keys to lowercase before storing them. If you want to change this behaviour, call  setCaseSensitive(true)  or call the constructor with the second argument set to true:
CCmdLineParser parser(::GetCommandLine(), true);
This will switch the parser to case-sensitive mode, and if the user passes  -key , then  GetKey(_T("Key"))  will return false

Syntax

Formally,  command   line  should be in following form:
CommandLine::=[<Key> [,<Key>...]]
<Key>::=<Delimeter>KeyName[<Separator><Value>]
<Value> ::= { KeyValue | <QuoteChar>Quoted Key Value<QuoteChar>} ][
<Delimeter>::= { - | / }
<Separator>::= { : }
<QuoteChar>::= { " }

Values for <Delimeter><Separator> and <QuoteChar> are stored in static variables m_sDelimeters,m_sValueSep and m_sQuotes respectively. If you want to change them (for instance, allow user to specify quoted values in apostrophes), you can do it in the beginning of CmdLineParser.cpp:

const TCHAR CCmdLineParser::m_sQuotes[] = _T("\"\'");
Note: If you want to change  m_sDelimeters , space must be the first character of this string. Also, if you have your own  CString  class with other name than  CString , you can change it in the beginning of  CmdLineParser.h :
typedef MyOwnCString CCmdLineParser_String;
That's it! ;)
<think>好的,用户想了解CommandLine的用法,特别是示例和教程。首先,我需要整理用户提供的引用资料,里面提到了几个不同的命令行参数解析库,比如command-line-args、Utility.CommandLine.Arguments、Apache Taverna Commandline和Fluent Command Line Parser。这些库分别用不同的语言实现,比如JavaScript、C#、Java等,所以用户可能需要跨语言的解决方案,或者他们可能对特定语言感兴趣。但用户的问题没有明确说明语言,所以需要涵盖不同库的示例。 接下来,我需要根据系统级指令来组织回答。必须使用中文,正确使用LaTeX格式的行内和独立公式,不过用户的问题主要涉及命令行解析,可能数学公式较少,但如果有相关算法或表达式的话需要注意格式。另外,生成相关问题是必须的。 用户提供的引用资料中有四个不同的库,每个都有不同的使用方式。需要分别给出每个库的基本用法示例,并确保代码块正确展示。例如,对于JavaScript的command-line-args,需要展示如何定义选项和解析参数。对于C#的Utility.CommandLine.Arguments,可能需要展示属性标记和解析方法。Apache Taverna的示例是Java命令行调用,而Fluent Command Line Parser则是C#的流畅接口。这些都需要分别处理,确保示例代码正确无误。 同时,回答的结构要清晰,逐步介绍每个库的用法。可能需要分点说明,每个库作为一个子部分,给出安装方法、示例代码和运行命令。此外,相关问题的生成需要涵盖不同方面,比如不同语言的库推荐、参数解析的原理、如何处理复杂参数等,这样用户可以根据需求进一步探索。 需要注意的是,用户的问题可能更倾向于通用命令行参数解析的方法,但引用的资料涉及具体库,因此需要平衡两者。可能需要先介绍通用步骤,再结合具体库的示例。但根据用户提供的引用,重点应放在提到的库上,所以应分别展示每个库的用法。 最后,确保引用标识正确添加到每个库的说明末尾,例如[^1]、[^2]等,并且相关问题与内容相关,能够引导用户深入了解更多细节。需要检查所有LaTeX语法是否正确,尽管可能用到的数学公式不多,但如果有变量或代码中的数学表达式,需正确使用$...$格式。</think>### CommandLine 工具使用方法详解 命令行参数解析是开发命令行工具的核心功能,以下是不同语言的常用库及示例: #### 一、JavaScript 库 `command-line-args` 1. **安装**:`npm install command-line-args` 2. **基本用法**: ```javascript const commandLineArgs = require('command-line-args'); const optionDefinitions = [ { name: 'input', alias: 'i', type: String }, { name: 'verbose', alias: 'v', type: Boolean } ]; const options = commandLineArgs(optionDefinitions); console.log(options); ``` 3. **运行命令**: ```bash node app.js -i data.txt -v ``` 该库支持类型推断和别名机制,适合快速开发。 #### 二、C# 库 `Utility.CommandLine.Arguments` 1. **定义参数类**: ```csharp using Utility.CommandLine; class Arguments { [Argument('f', "file")] public static string FilePath { get; set; } } ``` 2. **解析参数**: ```csharp ArgumentsParser.Parse(args); Console.WriteLine($"文件路径:{Arguments.FilePath}"); ``` 3. **运行命令**: ```bash app.exe -f config.xml ``` 支持自动绑定到静态属性,语法简洁。 #### 三、Java 库 Apache Taverna 1. **执行工作流**: ```bash java -jar taverna-cli.jar -workflow myworkflow.t2flow -input name=value ``` 2. **参数说明**: - `-workflow` 指定工作流文件路径 - `-input` 设置输入参数 适用于科学计算工作流的执行[^3]。 #### 四、C# 流畅接口库 `Fluent Command Line Parser` ```csharp var parser = new FluentCommandLineParser<ApplicationArgs>(); parser.Setup(arg => arg.InputFile) .As('i', "input") .Required(); var result = parser.Parse(args); ``` 支持强类型参数配置和验证机制[^4]。 $$ \text{参数解析时间复杂度:} O(n) \text{(n为参数个数)} $$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值