Thread overview
getopt Basic usage
Aug 15, 2020
James Gray
Aug 15, 2020
mw
Aug 16, 2020
James Gray
Aug 15, 2020
MoonlightSentinel
Aug 16, 2020
James Gray
Aug 15, 2020
Jon Degenhardt
Aug 16, 2020
James Gray
August 15, 2020
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");
 }
}



August 15, 2020
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


August 15, 2020
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.

August 15, 2020
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
August 16, 2020
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).
August 16, 2020
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.
August 16, 2020
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.