Skip to content

Usage Text

Jan Michelfeit edited this page Feb 2, 2016 · 1 revision

Generating usage text with EasyOpt

After you configure all program options (see Configuring options), you can easily generate usage text describing all the registered options. The usage text can be retrieved as a string with EasyOpt.GetUsage().

Usage consists of description of program usage which you can set with EasyOpt.UsageDescription property and description of options.

Descriptions for each option or parameter are set in their respective constructors or with their UsageText and UsageName properties, respectively.

Here is an example program and its usage text:

static void Main(string[] args) {
  EasyOpt parser = new EasyOpt();
 
  var verbose = OptionFactory.Create(false, "Be more verbose");
  parser.AddOption(verbose, 'v', "verbose");
 
  var fileParam = new StringParameter(true, "FILE");
  var file = OptionFactory.Create(false, "Write output to FILE", fileParam);
  parser.AddOption(file, 'f', "file");
 
  var lineWidthParam = new IntParameter(true, "WIDTH", 80);
  lineWidthParam.AddConstraint(new LowerBoundConstraint(1));
  var lineWidth = OptionFactory.Create(false, "Wrap lines at WIDTH", lineWidthParam);
  parser.AddOption(lineWidth, "line-width");
 
  parser.UsageDescription = "program [OPTION]... [FILE]...";
 
  try {
    parser.Parse(args);
  }
  catch (EasyOptException e) {
    Console.Write( parser.GetUsage() );
    return;
  }
 
  String[] arguments = parser.GetArguments();
 
  bool isVerbose = verbose.Value;
  String outputFile = file.Value;
  int maxLineWidth = lineWidth.Value;
}
program [OPTION]... [FILE]...

    -v, --verbose
        Be more verbose

    -f, --file=FILE
        Write output to FILE

    --line-width=WIDTH
        Wrap lines at WIDTH

Options are listed in the same order they were registered by EasyOpt.AddOption().

Clone this wiki locally