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

Mike Wey mike-wey at example.com
Mon Dec 11 21:24:41 UTC 2017


On 11-12-17 21:58, Jordi Gutiérrez Hermoso wrote:
> but instead, the docstring from getopt is only generated if all 
> arguments are valid, i.e. when it's the least needed because the user 
> already knew what to input.
> 
> What's the proper style, then? Can someone show me a good example of how 
> to use getopt and the docstring it automatically generates?


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);
}
```

-- 
Mike Wey


More information about the Digitalmars-d-learn mailing list