From 9d8c527211edd2bc74fa548f30f69f8d21868fef Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 15 Feb 2007 14:28:12 +0000 Subject: Parsing des argument ligne de commandes --- getopt/GetOpt.cs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 getopt/GetOpt.cs (limited to 'getopt/GetOpt.cs') diff --git a/getopt/GetOpt.cs b/getopt/GetOpt.cs new file mode 100644 index 0000000..a158073 --- /dev/null +++ b/getopt/GetOpt.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Specialized; +using System.Collections.Generic; + +namespace Gnu +{ + /// + /// An implementation of the getopt standard, as used by Gnu GetOpt + /// + public class GetOpt + { + StringCollection m_params = new StringCollection(); + StringCollection m_extras = new StringCollection(); + List m_args = new List(); + + public GetOpt(string[] CommandLineArgs, string ParametersDescription) + { + // Import the string array into the collection + foreach(string s in CommandLineArgs) + { + m_params.Add(s); + } + + // Parse the params description + for(int i = 0; i < ParametersDescription.Length; i++) + { + Arg a = new Arg(); + a.Flag = ParametersDescription[i]; + if((ParametersDescription.Length > i + 1) && (ParametersDescription[i + 1] == ':')) + { + a.TakesParameter = true; + i++; + } + + m_args.Add(a); + } + } + + public Arg NextArg() + { + SnarfExtras(); + + if(m_params.Count == 0) + return null; + + foreach(Arg a in m_args) + { + if(a.Flag == m_params[0][1] && m_params[0][0] == '-' ) + { + Arg matched = a; + try + { + if(a.TakesParameter) + { + matched.Parameter = m_params[1]; + m_params.RemoveAt(1); + } + } + catch(Exception) + { + } + + m_params.RemoveAt(0); + return matched; + } + } + + if(m_params[0][0] == '-') + { + Arg tempa = new Arg(); + tempa.Flag = m_params[0][1]; + tempa.TakesParameter = false; + return tempa; + } + return null; + } + + public StringCollection Extras + { + get + { + SnarfExtras(); + + return m_extras; + } + } + + private void SnarfExtras() + { + // Parameters must start with a hyphen + while((m_params.Count > 0) && (m_params[0][0] != '-')) + { + m_extras.Add(m_params[0]); + m_params.RemoveAt(0); + } + } + } +} -- cgit v1.1