C# 经典实例 第一章 类和泛型 #1.5 解析命令行参数

本文介绍了如何使用C#处理命令行参数,包括解析、验证和使用。通过Argument、ArgumentDefinition和ArgumentSemanticAnalyzer类,遵循Visual C#.NET命令行格式,解析并验证如`-output:`等选项。示例展示了如何处理无效参数,以及如何通过ArgumentSemanticAnalyzer进行参数验证和获取。
摘要由CSDN通过智能技术生成

问题:

需要应用程序以标准格式(在1.5.3中介绍)接受一个或多个命令行参数。你需要访问和解析传递给应用程序的完整的命令行。

解决方案:

在例1-5中,结合使用以下类来帮助解析命令行参数:Argument\ArgumentDefinition和ArgumentSemanticAnalyzer。

例1-5:Argument类:

using System;
using System.Diagnostics;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;

public sealed class Argument
{
    public string Original { get; }
    public string Switch { get; private set; }
    public ReadOnlyCollection<string> SubArguments { get; }
    private List<string> subArguments;
    public Argument(string original)
    {
        Original = original; Switch = string.Empty;
        subArguments = new List<string>();
        SubArguments = new ReadOnlyCollection<string>(subArguments);
        Parse();
    }

    private void Parse()
    {
        if (string.IsNullOrEmpty(Original))
        {
            return;
        }
        char[] switchChars = { '/', '-' };

        if (!switchChars.Contains(Original[0]))

        {
            return;
        }

        string switchString = Original.Substring(1);
        string subArgsString = string.Empty;
        int colon = switchString.IndexOf(':');

        if (colon >= 0)
        {
            subArgsString = switchString.Substring(colon + 1);
            switchString = switchString.Substring(0, colon);
        }

        Switch = switchString;

        if (!string.IsNullOrEmpty(subArgsString))
            subArguments.AddRange(subArgsString.Split(';'));
    }

    // 一组谓词,提供关于参数的有用信息
    // 使用lambda表达式实现 
    public bool IsSimple => SubArguments.Count == 0;
    public bool IsSimpleSwitch =>
        !string.IsNullOrEmpty(Switch) && SubArguments.Count == 0;
    public bool IsCompoundSwitch => !string.IsNullOrEmpty(Switch) && SubArguments.Count == 1;
    public bool IsComplexSwitch => !string.IsNullOrEmpty(Switch) && SubArguments.Count > 0;
}

public sealed class ArgumentDefinition
{
    public string ArgumentSwitch { get; }
    public string Syntax { get; }
    public string Description { get; }
    public Func<Argument, bool> Verifier { get; }

    public ArgumentDefinition(string argumentSwitch, string syntax, string description, Func<Argument, bool> verifier)
    {
        ArgumentSwitch = argumentSwitch.ToUpper();
        Syntax = syntax;
        Description = description;
        Verifier = verifier;
    }

    public bool Verify(Argument arg) => Verifier(arg);
}

public sealed class ArgumentSemanticAnalyzer
{

    private List<ArgumentDefinition> argumentDefinitions = new List<ArgumentDefinition>();
    private Dictionary<string, Action<Argument>> argumentActions = new Dictionary<string, Action<Argument>>();

    public ReadOnlyCollection<Argument> UnrecognizedArguments { get; private set; }
    public ReadOnlyCollection<Argument> MalformedArguments { get; private set; }
    public ReadOnlyCollection<Argument> RepeatedArguments { get; private set; }

    public ReadOnlyCollection<ArgumentDefinition> ArgumentDefinitions =>
        new ReadOnlyCollection<ArgumentDefinition>(argumentDefinitions);

    public IEnumerable<string> DefinedSwitches => from argumentDefinition in argumentDefinitions select argumentDefinition.ArgumentSwitch;

    public void AddArgumentVerifier(ArgumentDefinition verifier) => argumentDefinitions.Add(verifier);

    public void RemoveArgumentVerifier(ArgumentDefinition verifier)
    {
        var verifiersToRemove = from v in argumentDefinitions
                                where v.ArgumentSwitch == verifier.ArgumentSwitch
                                select v;
        foreach (var v in verifiersToRemove)
            argumentDefinitions.Remove(v)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值