ArgParse visualization of argument type and default value

Hello all,

does anybody know if there is a way to customize the way ArgParse shows the default type and value for arguments where this is set? I’m particularly annoyed by the fact that e.g. for an UInt value the default is being shown in hexadecimal with all digits rather than in decimal:

(type: UInt64, default: 0x0000000000000064)

I tried perusing the documentation, and there seems to be no way to customize this. The effect seems related to ArgParsestring_cmpact function, that builds the string representation via show(IOContext(..., :compact=>true), ...), which when applied to an UInt rather than an Int will switch to hex.

Hi,

I don’t think there’s a nice way to achieve this. I suppose you could create a PR to add a field default_string::String to ArgParseField, which defaults to using string_compact(default) if default is set. In gen_help_text you’d then just use default_str = mid * "default: " * arg.default_string. I don’t see many situations where this would be useful, though.

If you only want change how all instances of a type get printed, you could create a wrapper type (e.g. MyUInt) which is basically UInt, except for show. I guess this would be a lot of work, although there might be some packages with useful macros.

If you’re comfortable with a bit of type piracy, the simplest approach would be to (re)define string_compact. For example,

using ArgParse

function ArgParse.string_compact(x::UInt)
	return string(x, base=10)
end

function parse_commandline()
    s = ArgParseSettings()

    @add_arg_table! s begin
        "--arg"
            arg_type = UInt
            default = 100
    end

    return parse_args(s)
end

parse_commandline()

in a file ap.jl yields

> julia ap.jl --help
usage: ap.jl [--arg ARG] [-h]

optional arguments:
  --arg ARG   (type: UInt64, default: 100)
  -h, --help  show this help message and exit

Thanks for the feedback. The custom type sounds like a good idea. Provisionally, since we don’t need the full range of the UInt and we need to check that the given argument is (strictly) positive anyway, I’ve just turned it into a simple Int), but I’ll be looking into making it its own type.