www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - getopt Basic usage

reply James Gray <test test.com> writes:
I am trying to use getopt and would not like the program to throw 
an unhandled exception when parsing command line options. Is the 
following, adapted from the first example in the getopt 
documentation, a reasonable approach?


import std.getopt;

string data = "file.dat";
int length = 24;
bool verbose;
enum Color { no, yes };
Color color;

void main(string[] args)
{

  try {
  auto helpInformation = getopt(
    args,
    std.getopt.config.stopOnFirstNonOption,
    "length",  &length,    // numeric
    "file",    &data,      // string
    "verbose", &verbose,   // flag
    "color", "Information about this color", &color);    // enum

   if (helpInformation.helpWanted)
   {
    defaultGetoptPrinter("Some information about the program.",
      helpInformation.options);
   }
  }
  catch(Exception e) {
   import std.stdio : writeln;
   writeln(e.msg, "\nFor more information use --help");
  }
}
Aug 14 2020
next sibling parent reply mw <mingwu gmail.com> writes:
On Saturday, 15 August 2020 at 04:09:19 UTC, James Gray wrote:
 I am trying to use getopt and would not like the program to 
 throw an unhandled exception when parsing command line options. 
 Is the following, adapted from the first example in the getopt 
 documentation, a reasonable approach?
life is short, use a good library (instead of the raw std lib, or re-invent the wheels): https://code.dlang.org/packages/commandr
Aug 14 2020
parent James Gray <test test.com> writes:
On Saturday, 15 August 2020 at 04:39:58 UTC, mw wrote:
 On Saturday, 15 August 2020 at 04:09:19 UTC, James Gray wrote:
 I am trying to use getopt and would not like the program to 
 throw an unhandled exception when parsing command line 
 options. Is the following, adapted from the first example in 
 the getopt documentation, a reasonable approach?
life is short, use a good library (instead of the raw std lib, or re-invent the wheels): https://code.dlang.org/packages/commandr
I will have a look at that. Thanks.
Aug 15 2020
prev sibling next sibling parent reply MoonlightSentinel <moonlightsentinel disroot.org> writes:
On Saturday, 15 August 2020 at 04:09:19 UTC, James Gray wrote:
 I am trying to use getopt and would not like the program to 
 throw an unhandled exception when parsing command line options.
Try passing config.passThrough, it should disable the exception for unknown arguments.
Aug 15 2020
parent James Gray <test test.com> writes:
On Saturday, 15 August 2020 at 09:48:26 UTC, MoonlightSentinel 
wrote:
 On Saturday, 15 August 2020 at 04:09:19 UTC, James Gray wrote:
 I am trying to use getopt and would not like the program to 
 throw an unhandled exception when parsing command line options.
Try passing config.passThrough, it should disable the exception for unknown arguments.
Thanks a lot for the suggestion. But to me that will produce a program with very unusual behaviour (i.e. ignoring unrecognised arguments).
Aug 15 2020
prev sibling parent reply Jon Degenhardt <jond noreply.com> writes:
On Saturday, 15 August 2020 at 04:09:19 UTC, James Gray wrote:
 I am trying to use getopt and would not like the program to 
 throw an unhandled exception when parsing command line options. 
 Is the following, adapted from the first example in the getopt 
 documentation, a reasonable approach?
I use the approach you showed, except for writing errors to stderr and returning an exit status. This has worked fine. An example: https://github.com/eBay/tsv-utils/blob/master/number-lines/src/tsv_utils/number-lines.d#L48
Aug 15 2020
parent James Gray <test test.com> writes:
On Saturday, 15 August 2020 at 17:04:17 UTC, Jon Degenhardt wrote:
 On Saturday, 15 August 2020 at 04:09:19 UTC, James Gray wrote:
 I am trying to use getopt and would not like the program to 
 throw an unhandled exception when parsing command line 
 options. Is the following, adapted from the first example in 
 the getopt documentation, a reasonable approach?
I use the approach you showed, except for writing errors to stderr and returning an exit status. This has worked fine. An example: https://github.com/eBay/tsv-utils/blob/master/number-lines/src/tsv_utils/number-lines.d#L48
Thank you I will have a look at the link.
Aug 15 2020