public static bool GetOptions(string[] args, String sopts, LongOpt[] lopts, Options options, out StringBuilder message) { message = new StringBuilder(); Getopt.Getopt getopt = new Getopt.Getopt( Assembly.GetExecutingAssembly().GetName().Name, args, sopts, lopts) { Opterr = false }; options.Mode = Mode.SateliteAssembly; options.Verbose = false; options.CompilerName = Path.DirectorySeparatorChar == '/' ? "mcs" : "csc"; options.ShowUsage = false; options.CheckFormat = false; options.DebugMode = false; int option; while ((option = getopt.getopt()) != -1) { switch (option) { case 1: options.InputFiles.Add(getopt.Optarg); break; case 2: options.CheckFormat = true; break; case 3: options.Mode = Mode.Resources; break; case 4: options.DebugMode = true; Trace.WriteLine("Debug mode is ON"); break; case '5': options.CompilerName = getopt.Optarg; break; case ':': message.AppendFormat("Option {0} requires an argument", getopt.OptoptStr); return false; case '?': message.AppendFormat("Invalid option '{0}'", getopt.OptoptStr); return false; case 'r': options.BaseName = getopt.Optarg; break; case 'o': options.OutFile = getopt.Optarg; break; case 'd': options.OutDir = getopt.Optarg; break; case 'l': options.LocaleStr = getopt.Optarg; break; case 'L': options.LibDir = getopt.Optarg; break; case 'v': options.Verbose = true; break; case 'h': options.ShowUsage = true; return true; default: PrintUsage(); return false; } } if (getopt.Opterr) { message.AppendLine(); message.Append("Error in command line options. Use -h to read options usage"); return false; } return true; }
/// <summary> /// Produce an optstring suitable for the Getopt constructor /// given an array of LongOpts suitable for the Getopt /// constructor /// </summary> public static string digest(LongOpt[] longOpts) { StringBuilder sb = new StringBuilder(); foreach (LongOpt o in longOpts) { sb.Append((char)o.Val); if (o.HasArg == Argument.Optional) sb.Append("::"); if (o.HasArg == Argument.Required) sb.Append(":"); } return sb.ToString(); }
/// <summary> /// Construct a Getopt instance with given input data that is capable /// of parsing long options as well as short. /// </summary> /// <param name="progname"> /// The name to display as the program name when printing errors. /// </param> /// <param name="argv"> /// The string array passed as the command ilne to the program. /// </param> /// <param name="optstring"> /// A string containing a description of the valid short args for this /// program. /// </param> /// <param name="longOptions"> /// An array of <see cref="LongOpt"/> objects that describes the valid /// long args for this program. /// </param> public Getopt(string progname, string[] argv, string optstring, LongOpt[] longOptions) : this(progname, argv, optstring, longOptions, false) { }
/// <summary> /// Construct a Getopt instance with given input data that is capable /// of parsing long options and short options. Contrary to what you /// might think, the flag <paramref name="longOnly"/> does not /// determine whether or not we scan for only long arguments. Instead, /// a value of true here allows long arguments to start with a /// '<c>-</c>' instead of "<c>--</c>" unless there is a conflict with /// a short option name. /// </summary> /// <param name="progname"> /// The name to display as the program name when printing errors /// </param> /// <param name="argv"> /// The string array passed as the command ilne to the program. /// </param> /// <param name="optstring"> /// A string containing a description of the valid short args for this /// program. /// </param> /// <param name="longOptions"> /// An array of <see cref="LongOpt"/> objects that describes the valid /// long args for this program. /// </param> /// <param name="longOnly"> /// true if long options that do not conflict with short options can /// start with a '<c>-</c>' as well as "<c>--</c>". /// </param> public Getopt(string progname, string[] argv, string optstring, LongOpt[] longOptions, bool longOnly) { if (optstring.Length == 0) optstring = " "; // This function is essentially _getopt_initialize from GNU getopt this.progname = progname; this.argv = argv; this.optstring = optstring; this.longOptions = longOptions; this.longOnly = longOnly; // Check for application setting "Gnu.PosixlyCorrect" to determine // whether to strictly follow the POSIX standard. This replaces the // "POSIXLY_CORRECT" environment variable in the C version try { if((bool) new AppSettingsReader().GetValue( "Gnu.PosixlyCorrect", typeof(bool))) { this.posixlyCorrect = true; this.cultureInfo = new CultureInfo("en-US"); } else this.posixlyCorrect = false; } catch(Exception) { this.posixlyCorrect = false; } // Determine how to handle the ordering of options and non-options if (optstring[0] == '-') { this.ordering = Order.ReturnInOrder; if (optstring.Length > 1) this.optstring = optstring.Substring(1); } else if (optstring[0] == '+') { this.ordering = Order.RequireOrder; if (optstring.Length > 1) this.optstring = optstring.Substring(1); } else if (this.posixlyCorrect) { this.ordering = Order.RequireOrder; } else { this.ordering = Order.Permute; // The normal default case } }
public static bool GetOptions(string[] args, String sopts, LongOpt[] lopts, Options options, out StringBuilder message) { message = new StringBuilder(); Getopt.Getopt getopt = new Getopt.Getopt( Assembly.GetExecutingAssembly().GetName().Name, args, sopts, lopts) { Opterr = false }; options.Verbose = false; options.ShowUsage = false; options.Recursive = false; options.Overwrite = true; int option; while ((option = getopt.getopt()) != -1) { switch (option) { case 1: options.InputFiles.Add(getopt.Optarg); break; case 2: options.Recursive = true; break; case 3: options.SearchPatterns.Add(getopt.Optarg); break; case 4: options.SetEncoding(getopt.Optarg); break; case 5: options.DetectEncoding = true; break; case ':': message.AppendFormat("Option '{0}' requires an argument", getopt.OptoptStr); return false; case '?': message.AppendFormat("Invalid option '{0}'", getopt.OptoptStr); return false; case 'f': string fileList = getopt.Optarg; Utils.FileUtils.ReadStrings(fileList, options.InputFiles); break; case 'j': options.Overwrite = false; break; case 'o': options.OutFile = getopt.Optarg; break; case 'D': options.InputDirs.Add(getopt.Optarg); break; case 'v': options.Verbose = true; break; case 'h': options.ShowUsage = true; return true; default: PrintUsage(); return false; } } if (getopt.Opterr) { message.AppendLine(); message.Append("Error in command line options. Use -h to read options usage"); return false; } return true; }