//---------------------------------------------------------------------

 

  /// <summary>

  ///   Contains the parsed command line arguments. This consists of two

  ///   lists, one of argument pairs, and one of stand-alone arguments.

  /// </summary>

  public class CommandArgs

  {

      //---------------------------------------------------------------------

 

      private readonly Dictionary<string, string> mArgPairs = new Dictionary<string, string>();

 

      //---------------------------------------------------------------------

 

      private readonly List<string> mParams = new List<string>();

 

      /// <summary>

      ///   Returns the dictionary of argument/value pairs.

      /// </summary>

      public Dictionary<string, string> ArgPairs

      {

          get { return this.mArgPairs; }

      }

 

      /// <summary>

      ///   Returns the list of stand-alone parameters.

      /// </summary>

      public List<string> Params

      {

          get { return this.mParams; }

      }

  }

 

  //---------------------------------------------------------------------

 

  /// <summary>

  ///   Implements command line parsing

  /// </summary>

  public class CommandLine

  {

      //---------------------------------------------------------------------

 

      /// <summary>

      ///   Parses the passed command line arguments and returns the result

      ///   in a CommandArgs object.

      /// </summary>

      /// The command line is assumed to be in the format:

      /// 

      /// CMD [param] [[-|--|\]&lt;arg&gt;[[=]&lt;value&gt;]] [param]

      /// 

      /// Basically, stand-alone parameters can appear anywhere on the command line.

      /// Arguments are defined as key/value pairs. The argument key must begin

      /// with a '-', '--', or '\'. Between the argument and the value must be at

      /// least one space or a single '='. Extra spaces are ignored. Arguments MAY

      /// be followed by a value or, if no value supplied, the string 'true' is used.

      /// You must enclose argument values in quotes if they contain a space, otherwise

      /// they will not parse correctly.

      /// 

      /// Example command lines are:

      /// 

      /// cmd first -o outfile.txt --compile second \errors=errors.txt third fourth --test = "the value" fifth

      /// <param name="args"> array of command line arguments </param>

      /// <returns> CommandArgs object containing the parsed command line </returns>

      public static CommandArgs Parse(string[] args)

      {

          var kEqual = new char[] {'='};

 

          var kArgStart = new char[] {'-', '\\'};

 

          var ca = new CommandArgs();

 

          var ii = -1;

 

          var token = NextToken(args, ref ii);

 

          while (token != null)

          {

              if (IsArg(token))

              {

                  var arg = token.TrimStart(kArgStart).TrimEnd(kEqual);

 

                  string value = null;

 

                  if (arg.Contains("="))

                  {

                      // arg was specified with an '=' sign, so we need

 

                      // to split the string into the arg and value, but only

 

                      // if there is no space between the '=' and the arg and value.

 

                      var r = arg.Split(kEqual, 2);

 

                      if (r.Length == 2 && r[1] != string.Empty)

                      {

                          arg = r[0];

 

                          value = r[1];

                      }

                  }

 

                  while (value == null)

                  {

                      var next = NextToken(args, ref ii);

 

                      if (next != null)

                      {

                          if (IsArg(next))

                          {

                              // push the token back onto the stack so

 

                              // it gets picked up on next pass as an Arg

 

                              ii--;

 

                              value = "true";

                          }

 

                          else if (next != "=")

                          {

                              // save the value (trimming any '=' from the start)

 

                              value = next.TrimStart(kEqual);

                          }

                      }

                  }

 

                  // save the pair

 

                  ca.ArgPairs.Add(arg, value);

              }

 

              else if (token != string.Empty)

              {

                  // this is a stand-alone parameter. 

 

                  ca.Params.Add(token);

              }

 

              token = NextToken(args, ref ii);

          }

 

          return ca;

      }

 

      //---------------------------------------------------------------------

 

      /// <summary>

      ///   Returns True if the passed string is an argument (starts with 

      ///   '-', '--', or '\'.)

      /// </summary>

      /// <param name="arg"> the string token to test </param>

      /// <returns> true if the passed string is an argument, else false if a parameter </returns>

      private static bool IsArg(string arg)

      {

          return (arg.StartsWith("-") || arg.StartsWith("\\"));

      }

 

      //---------------------------------------------------------------------

 

      /// <summary>

      ///   Returns the next string token in the argument list

      /// </summary>

      /// <param name="args"> list of string tokens </param>

      /// <param name="ii"> index of the current token in the array </param>

      /// <returns> the next string token, or null if no more tokens in array </returns>

      private static string NextToken(string[] args, ref int ii)

      {

          ii++; // move to next token

 

          while (ii < args.Length)

          {

              var cur = args[ii].Trim();

 

              if (cur != string.Empty)

              {

                  // found valid token

 

                  return cur;

              }

 

              ii++;

          }

 

          // failed to get another token

 

          return null;

      }

  }

最新文章

  1. UVALive 6911---Double Swords(贪心+树状数组(或集合))
  2. 《利用python进行数据分析》读书笔记 --第一、二章 准备与例子
  3. UINavigationItem UINavigationBar 关系分析
  4. 1-11 ICMP协议
  5. iso socket基础2
  6. INFORMIX数据库常用命令
  7. 深入理解Java内存模型(六)——final
  8. 固定DIV样式
  9. LeeCode-Sqrt(x)
  10. 从苹果系统InstallESD.dmg里提取IOS
  11. 深度学习在graph上的使用
  12. 使用Java API操作HDFS文件系统
  13. 打造springboot高性能服务器(spring reactor的使用)
  14. ORA-12514: TNS:监听程序当前无法识别连接描述符中请
  15. 基于设备树的controller学习(2)
  16. Android Studio 1.1.0 “关联源码” 或者“导入源码” ,又或者插件包
  17. python3 mock
  18. 【CSU 1803】2016 (数学)
  19. hdu 5920(模拟)
  20. VirtualBox network / study environment setup for RHEL

热门文章

  1. Mysql问题随记
  2. java使用POST发送soap报文请求webservice返回500错误解析
  3. 使用Apache IO库操作IO与文件
  4. jsp Ajax请求(返回xml数据类型)
  5. 解决在Python中使用Win32api报错的问题,No module named win32api
  6. 关闭是否只查看安全传送的网页内容提示框 和 是否允许运行软件,如ActiveX控件和插件提示框
  7. linux下的同步与互斥
  8. python 添加日期
  9. 几种导入osm(openstreetmap)数据的方法
  10. GO程序设计2——面向过程基础知识