ArgMacros: How to use variables as defaults?

How do you use variables within @tuplearguments of ArgMacros?

#!/usr/bin/env julia

const usage = "thisscript [--val1=<val1>] [--val2=<val2>]"
const val2_default = 9

args = @tuplearguments begin
  @helpusage Main.usage # -> no method matching var"@helpusage"( . . .
  @argumentdefault Int 3 val1 "--val1"
  @argumentdefault Int Main.val2_default val2 "--val2"
end
@show args

In the above, I tried to use two global variables within the macro call. One attempt fails: Main.usage causes a “no method” error. The other is 90% successful: Main.val2_default fetches the correct value (when @helpusage uses a String literal) but the help message shows

. . .
--val2
      (Type: Int, Default: Main.val2_default)

I’m sure that I’m not able to solve this problem only because I don’t know “these things” well enough.

Hi!

I’m not familiar with the ArgMacros.jl package, but by looking at its documentation, I’d say that you aren’t allowed to use variables and named constants in the macros for usage string and default values; you must inline these. I.e.,

args = @tuplearguments begin
  @helpusage "thisscript [--val1=<val1>] [--val2=<val2>]"
  @argumentdefault Int 3 val1 "--val1"
  @argumentdefault Int 9 val2 "--val2"
end
@show args
1 Like

you aren’t allowed

Do you think that that represents 1) an inherent (insurmountable) restriction, 2) a deliberate design decision for some reason, 3) a complexity that would result if variables are allowed? Again I’m not at all familiar with how macro works . . .

I’d guess it is an inherent restriction, because the macro implementation relies on the fact that these values are known at parse time. Perhaps it would be possible to adjust the macro to accept variables/constants as well las literals, but it would certainly complicate things.

So it’s either 1 or 3, I’m not sure which without deeper investigation.