Skip to content

Commit

Permalink
Support main callback with ref parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-zherikov committed Oct 23, 2024
1 parent ee69ce3 commit 8ef5ba7
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions source/argparse/api/cli.d
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ template CLI(Config config, COMMAND)
}

static int parseArgs(alias newMain)(string[] args, COMMAND initialValue = COMMAND.init)
if(__traits(compiles, { newMain(COMMAND.init); }))
if(__traits(compiles, { newMain(initialValue); }))
{
alias value = initialValue;

Expand All @@ -140,7 +140,7 @@ template CLI(Config config, COMMAND)
}

static int parseArgs(alias newMain)(string[] args, COMMAND initialValue = COMMAND.init)
if(__traits(compiles, { newMain(COMMAND.init, string[].init); }))
if(__traits(compiles, { newMain(initialValue, string[].init); }))
{
alias value = initialValue;

Expand Down Expand Up @@ -205,11 +205,20 @@ alias CLI(COMMANDS...) = CLI!(Config.init, COMMANDS);

unittest
{
// Ensure that CLI.main is compilable
struct Args {}

mixin CLI!({
Config cfg;
cfg.errorHandler = (string s) {};
return cfg;
}(), Args).main!((_){});
enum Config cfg = { errorHandler: (string s) {} };

mixin CLI!(cfg, Args).main!((_){});
}

unittest
{
// Ensure that CLI.main is compilable with reference parameter in callback
struct Args {}

enum Config cfg = { errorHandler: (string s) {} };

mixin CLI!(cfg, Args).main!((ref _){});
}

0 comments on commit 8ef5ba7

Please sign in to comment.