What's the proper way to use std.getopt?

Seb seb at wilzba.ch
Mon Dec 11 23:35:53 UTC 2017


On Monday, 11 December 2017 at 21:24:41 UTC, Mike Wey wrote:
> On 11-12-17 21:58, Jordi Gutiérrez Hermoso wrote:
>> [...]
>
>
> I would use something like this, print the help information for 
> --help, print an error for invalid arguments:
>
>
> ```
> try
> {
> 	auto helpInformation = getopt(
> 		args,
> 		"input|i", "The input", &input,
> 		"output|o", "The output", &output
> 	);
>
> 	if (helpInformation.helpWanted)
> 	{
> 		defaultGetoptPrinter("Description", helpInformation.options);
> 		exit(0);
> 	}
> }
> catch (GetOptException e)
> {
> 	writeln(e.msg);
> 	exit(1);
> }
> ```

D style would be to use sth. like this (instead of try/catch):

```
scope(failure) {
   e.msg.writeln;
   1.exit;
}
```


More information about the Digitalmars-d-learn mailing list