Why I love D: function parameter reification

Andrey Zherikov andrey.zherikov at gmail.com
Wed Jun 8 23:48:42 UTC 2022


On Wednesday, 8 June 2022 at 21:47:16 UTC, H. S. Teoh wrote:
> Today, I grappled with the following situation: I have a bunch 
> of functions with parameters that perform certain operations, 
> that I want to expose to the user via the program's 
> command-line arguments.  Also, for maximum user-friendliness, 
> the program's help text should accurately describe each 
> function (treated as a subcommand on the command line) and what 
> parameters each takes.
>
> The traditional solution, of course, is simple: write a 
> showHelp() function that prints some text describing each 
> function and its parameters, then in main() write a big switch 
> statement over function name, and each case block parses the 
> program arguments, converts them to a suitable form, and 
> invokes the function.

If you change your functions to callable structs moving function 
arguments to struct members then you can get command line 
parsing, help text generation and much more with just few lines 
of code using [argparse](https://code.dlang.org/packages/argparse)



More information about the Digitalmars-d mailing list