ArgMacros.ArgMacrosModule

Performant, macro-only, pure Julia package for parsing command line arguments. Uses macros to generate the parsing code within your main function, directly storing results in typed local variables.

Basic usage:

julia_main()
    @inlinearguments begin
        @argumentrequired Int foo "-f" "--foo"
        @argumentdefault Int 5 bar "-b" "--bar"
        ...
        @positionalrequired String baz
        ...
        @positionaloptional String anotheroption
    end

    x::Int = foo + bar # required and default values are typed and completely type stable
    y::String = baz * something(anotheroption, "optional arguments may have nothing value")
    ...
end

See the documentation before using.

ArgMacros._converttype!Method

If x is not a string (i.e. literal passed as default) assume direct conversion is possible. Does NOT validate.

ArgMacros._converttype!Method

Convert a vector of strings to Number type with parse or other types with direct conversions

ArgMacros._pop_count!Method

Remove multiple occurrences of flag from args and return the count removed

ArgMacros._validateorderMethod

Enforce argument declaration ordering: Flagged → Required Positional → Optional Positional → Leftover Positional

Throw ArgumentError if ordering violates this rule.

ArgMacros.@allowextraargumentsMacro
@allowextraarguments

Disables the default behavior of printing a message and exiting the program when not all values in ARGS could be assigned to specified arguments.

This will not capture any arguments, use @positionalleftover if you want to capture extra arguments that could not otherwise be assigned.

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @allowextraarguments
end
ArgMacros.@arghelpMacro
@arghelp help_text::String

Add help string for an argument. Applied to the preceding declared argument

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @argumentflag v "-v" "--verbose"
    @arghelp "Display additional output"
    ...
end
ArgMacros.@argtestMacro
@argtest argname func [desc]

Apply func to the value stored in argname, printing an error message (optionally specified by desc) and the program if func returns false. Test skipped if argname has value nothing (only possible for optional arguments). This macro must be used AFTER declaring the arugment with another macro.

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @positionalrequired String input_file
    @argtest input_file isfile "Couldn't find the input file."
    ...
end
ArgMacros.@argumentcountMacro
@argumentcount local_name flag::String

Store the number of occurrences of flag in local_name with type Int.

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @argumentcount verbose "-v"
    ...
end
ArgMacros.@argumentdefaultMacro
@argumentdefault type default_value local_name flags::String...

Attempt to get an argument specified by the given flags and store in the variable local_name with the specified type. Store the default value instead if the flags cannot be found. Default value automatically converted to specified type.

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @argumentdefault String "output.txt" output_file "-o" "--output"
    ...
end
ArgMacros.@argumentflagMacro
@argumentflag local_name flags::String...

Store true in the variable local_name with type Bool if one or more of the flags is found. Otherwise, store false.

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @argumentflag verbose "-v" "--verbose"
    ...
end
ArgMacros.@argumentoptionalMacro
@argumentoptional type local_name flags::String...

Attempt to get an argument specified by the given flags and store in the variable local_name with the type Union{type, Nothing}. Store nothing if the flags cannot be found.

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @argumentoptional String output_file "-o" "--output"
    ...
end
ArgMacros.@argumentrequiredMacro
@argumentrequired type local_name flags::String...

Get a required argument specified by the given flags and store in the variable local_name with the specified type.

Print a message and exit the program if the value is not found or cannot be converted to the specified type.

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @argumentrequired String output_file "-o" "--output"
    ...
end
ArgMacros.@beginargumentsMacro
@beginarguments begin ... end

This macro is deprecated beginning in ArgMacros v0.2.0 Please use @inlinearguments, which has the same interface Or consider @structarguments, @tuplearguments, and @dictarguments depending on your use case

ArgMacros.@dictargumentsMacro
@dictarguments begin ... end

Denote and setup a block with other macros from ArgMacros Return a Dict with the arguments instead of dumping them in the enclosing namespace

Example

function julia_main()
    args = @dictarguments begin
        ...
        @argumentrequired Int foo "-f" "--foo"
        @argumentdefault Int 5 bar "-b" "--bar"
        ...
    end
    ...
end
ArgMacros.@helpdescriptionMacro
@helpdescription description::String

Add description for the help screen

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @helpdescription "Lorem ipsum dolor sit amet"
    ...
end
ArgMacros.@helpepilogMacro
@helpepilog epilog::String

Add epilog for the help screen, displayed after rest of help

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @helpepilog "Lorem ipsum dolor sit amet"
    ...
end
ArgMacros.@helpusageMacro
@helpusage usage_text::String

Add usage text for the help screen Automatically prepended with "Usage: "

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    @helpusage "example.jl foo [bar] [-v]"
    ...
end
ArgMacros.@inlineargumentsMacro
@inlinearguments begin ... end

Denote and setup a block with other macros from ArgMacros

Example

function julia_main()
    @inlinearguments begin
        ...
        @argumentrequired Int foo "-f" "--foo"
        @argumentdefault Int 5 bar "-b" "--bar"
        ...
    end
    ...
end
ArgMacros.@positionaldefaultMacro
@positionaldefault type default_value local_name [help_name::String]

Attempt to get a positional argument and store in variable local_name with the specified type. Store the default value instead if an argument cannot be found.

Positional arguments are read in order after all flag/option arguments have been read. help_name used instead of local_name in messages to user if specified.

Default value automatically converted to specified type.

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @positionaldefault String output_file "output"
    ...
end
ArgMacros.@positionalleftoverMacro
@positionalleftover type local_name [help_name]

Get any leftover positional arguments after all other arguments have been parsed, and store in variable local_name with the type Vector{type}. This vector will be empty if there are no leftover arguments.

This macro must be used after all other arguments have been declared, as positional arguments are read in order after all flag/option arguments have been read. Using this macro means all input will be captured by this argument if not by another, so @allowextraarguments is not necesesary when this macro is used. help_name used instead of local_name in messages to user if specified.

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @positionalleftover String file_names "files"
    ...
end
ArgMacros.@positionaloptionalMacro
@positionaloptional type local_name [help_name]

Attempt to get a positional argument and store in variable local_name with the type Union{type, Nothing}. Store nothing if an argument is not found.

Positional arguments are read in order after all flag/option arguments have been read. help_name used instead of local_name in messages to user if specified.

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @positionaloptional String output_file "output"
    ...
end
ArgMacros.@positionalrequiredMacro
@positionalrequired type local_name [help_name::String]

Attempt to get a positional argument and store in variable local_name with the specified type.

Positional arguments are read in order after all flag/option arguments have been read. help_name used instead of local_name in messages to user if specified.

Print a message and exit the program if a value is not found or cannot be converted to the specified type.

Must be used in @beginarguments begin ... end block

Example

@beginarguments begin
    ...
    @positionalrequired String output_file "output"
    ...
end
ArgMacros.@structargumentsMacro
@structarguments mutable typename begin ... end

Denote and setup a block with other macros from ArgMacros Defines an optionally mutable struct type based on the arguments and a zero-argument constructor which will generate an instance of the struct based on the parsed arguments.

Example

@structarguments false Args begin
    ...
    @argumentrequired Int foo "-f" "--foo"
    @argumentdefault Int 5 bar "-b" "--bar"
    ...
end

function julia_main()
    args = Args()
    ...
end
ArgMacros.@tupleargumentsMacro
@tuplearguments begin ... end

Denote and setup a block with other macros from ArgMacros Return a NamedTuple with the arguments instead of dumping them in the enclosing namespace

Example

function julia_main()
    args = @tuplearguments begin
        ...
        @argumentrequired Int foo "-f" "--foo"
        @argumentdefault Int 5 bar "-b" "--bar"
        ...
    end
    ...
end