国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.NET命令行解析器示例程序(命令行選項功能)

2024-07-10 13:14:54
字體:
供稿:網(wǎng)友

示例需求

拷貝文件,如:CopyFiles -s "E:/Framework/Tenoner - 副本 (2)" -p "*.csproj" -t "E:/Framework/Tenoner - 副本 (2)/Bak",可以支持:深度拷貝、拷貝符合指定模式的文件、是否覆蓋等選項。

使用 CommandLineParser
CommandLineParser 是一個輕量級的工具,使用非常簡答,官方也有教程。

選項類

復(fù)制代碼 代碼如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using CommandLine;
using CommandLine.Text;

namespace CopyFiles
{
    class Options
    {
        [Option(
            's', "source", Required = true,
            HelpText = "源目錄。")]
        public string SourcePath { get; set; }

        [Option(
            'p', "pattern", Required = true,
            HelpText = "文件模式。")]
        public string SearchPattern { get; set; }

        [Option(
            't', "target", Required = true,
            HelpText = "目標目錄。")]
        public string TargetPath { get; set; }

        [Option('a', "all", DefaultValue = true,
            HelpText = "是否包含所有目錄?")]
        public bool AllDirectories { get; set; }

        [Option('o', "overwrite", DefaultValue = true,
            HelpText = "是否覆蓋所有文件?")]
        public bool Overwrite { get; set; }

        [Option('v', "verbose", DefaultValue = true,
            HelpText = "是否打印消息?")]
        public bool Verbose { get; set; }

        [HelpOption]
        public string GetUsage()
        {
            return HelpText.AutoBuild(this);
        }

        public void WriteLine(string format, params object[] args)
        {
            if (this.Verbose)
            {
                Console.WriteLine(string.Format(format, args));
            }
        }
    }
}

工具類

復(fù)制代碼 代碼如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using CommandLine;
using Happy.Utils;

namespace CopyFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            var options = new Options();
            if (Parser.Default.ParseArguments(args, options))
            {
                FileUtil.Copy(
                    options.SourcePath,
                    options.SearchPattern,
                    options.TargetPath,
                    (sourceFile, targetFile) =>
                    {
                        options.WriteLine("拷貝文件:{0} 到 {1}", sourceFile, targetFile);
                    },
                    (exceptionInfo) =>
                    {
                        options.WriteLine(exceptionInfo.Exception.Message);

                        exceptionInfo.ExceptionHandled = true;
                    },
                    options.AllDirectories,
                    options.Overwrite);
            }
        }
    }
}



.NET命令行解析器示例程序(命令行選項功能)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 交口县| 长顺县| 二连浩特市| 遵义县| 青田县| 万宁市| 乐亭县| 佛坪县| 银川市| 吴江市| 金山区| 通渭县| 班戈县| 乌海市| 河东区| 杨浦区| 汤原县| 大同县| 军事| 余庆县| 靖西县| 佳木斯市| 嫩江县| 广汉市| 连江县| 房产| 玉屏| 太和县| 同江市| 临汾市| 铜山县| 农安县| 浦城县| 新竹县| 玉门市| 九龙县| 江孜县| 原平市| 修武县| 育儿| 云林县|