C# 借助CommandLine 写命令行工具 在数据库中创建job

首先需要用到  CommandLine.dll

提供两个下载链接,云盘是我自己上传的,也就是我在用的

http://commandline.codeplex.com/

https://pan.baidu.com/s/1nX7hc_xrdGn2RjR-HuXnAQ

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

先创建Option类,这个类定义了一些可以接受的参数

using CommandLine;
using CommandLine.Text;

namespace Commandline
{
    class Options
    {
        /// <summary>
        /// 前面的参数用-*  后面的用--***后面会说
        /// </summary>
        [Option('n', "writeNO", Required = false, HelpText = "Use this option to write a")]
        public int Write_No { get; set; }

        [Option('a', "writeA", Required = false, HelpText = "Use this option to write b")]
        public bool Write_A { get; set; }

        [Option('b', "writeB", Required = false, HelpText = "Use this option to write c")]
        public bool Write_B { get; set; }

        [Option('c', "writeC", Required = false, HelpText = "Use this option to write d")]
        public bool Write_C { get; set; }

        [Option('s', "writestr", Required = false, HelpText = "Use this option to write e")]
        public string Write_str { get; set; }


        [ParserState]
        public IParserState LastParserState { get; set; }


        private string _heading;
        public Options()
        {
            this._heading = "V*o***S*nc";
        }
        [HelpOption]
        public string GetUsage()
        {
            return HelpText.AutoBuild(this,
            (HelpText current) =>
            {
                current.Heading = this._heading;
                current.Copyright = new CopyrightInfo("S**pe I*t****l Team", 2018);
                HelpText.DefaultParsingErrorsHandler(this, current);
            });

            //return HelpText.AutoBuild(this,
            //(HelpText current) => 
            //HelpText.DefaultParsingErrorsHandler(this, current));
            //这个就不要_heading了
        }
    }
}

在主函数中测试一下:

using System;

namespace Commandline
{
    class Program
    {
        static void Main(string[] args)
        {
            var option = new Options();
            try
            {
                if (CommandLine.Parser.Default.ParseArguments(args, option))
                {
                    if (option.Write_No != 0)
                    {
                        Write(option.Write_No.ToString());
                    }
                    if (option.Write_A)
                    {
                        Write("A");
                    }
                    if (option.Write_B)
                    {
                        Write("B");
                    }
                    if (option.Write_C)
                    {
                        Write("C");
                    }
                    if (option.Write_str!=null)
                    {
                        Write(option.Write_str);
                    }
                }
            }
            catch (Exception e)
            {
                throw;
            }
            Console.ReadKey();
        }

        private static void Write(string str)
        {
            Console.WriteLine("Command Line tell me to write {0}", str);
        }
    }
}

现在开始调式:

以下面的参数为例

[Option('s', "writestr", Required = false, HelpText = "Use this option to write e")]
        public string Write_str { get; set; }
参数填写 -s hello 或者 --writestr "hello"
前面的用-* 后面的参数用--*** 数字或者string可加“”也可不加
参数的先后顺序不影响程序的执行顺序,可对照下面的输出结果

在solution右击,添加如下参数

参数有误会出现:

上面的参数输出结果:

如果有涉及到对数据库的操作 在数据库中创建job  给出相应参数 来实现自动run

在Jobs上右击new一个job

填写相应的Name 和 Description

将所写程序的debug下的文件拷贝到数据库所在机器或者服务器并记录该路径

Step的操作按照下图进行操作:

制定一个计划来跑这个job,按照下图进行操作

Game Over

MrNou
原文地址:https://www.cnblogs.com/yangsirc/p/8582877.html