argument parsing into structure

Basile B. b2.temp at gmx.com
Wed Jun 26 14:58:08 UTC 2019


On Wednesday, 26 June 2019 at 09:40:06 UTC, JN wrote:
> On Wednesday, 26 June 2019 at 05:38:32 UTC, Jesse Phillips 
> wrote:
>> Sometimes a good API isn't the right answer. I like getopt as 
>> it is but I wanted a little different control. So I wrote up 
>> an article on my work around.
>>
>> https://dev.to/jessekphillips/argument-parsing-into-structure-4p4n
>>
>> I have another technique for sub commands I should write about 
>> too.
>
> http://code.dlang.org/packages/argsd

I think we are several having written alternative getopt() 
systems.
I made one too last year, it allows to write tools very nicely.
Recent example for a device flasher:

---
module app;

import
     iz.options;

struct Handler
{
private:

public:

     /// Handle the "--help" command.
     @Argument("--help", "prints the command line usage", 
ArgFlags(ArgFlag.allowShort))
     static void getHelp();

     /// Handle the "--info" command.
     @Argument("--info", "prints the list of connected mods", 
ArgFlags(ArgFlag.allowShort))
     static void getInfo();
     /// Handle the "--readFlash" command.
     @Argument("--readFlash", "read the firmware from the mod and 
save it to the specified file")
     static void readFlash(string fname);

     /// Handle the "--writeFlash" command.
     @Argument("--writeFlash", "read the firmware from the 
specified file and send it to the mod")
     static void saveFlash(string fname);

     /// Handle the "--reset" command.
     @Argument("--reset", "reset to factory state", 
ArgFlags(ArgFlag.allowShort))
     static void reset();
}

void main(string[] args)
{
     if (!handleArguments!(CanThrow, Handler)(args[1..$]) || 
args.length == 1)
         writeln(help!Handler);
}
---

https://github.com/Basile-z/iz/blob/master/import/iz/options.d#L379

Though I've detected several little flaws since it was written, 
but I don't care much  anymore about my own stuff these days...


More information about the Digitalmars-d-announce mailing list