Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

```@docs
ArgParseSettings
@project_version
```
3 changes: 2 additions & 1 deletion src/ArgParse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export
set_default_arg_group!,
import_settings!,
usage_string,
parse_args
parse_args,
@project_version

import Base: show, getindex, setindex!, haskey

Expand Down
37 changes: 37 additions & 0 deletions src/settings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1465,3 +1465,40 @@ function import_settings!(settings::ArgParseSettings,
end
return settings
end

"""
@project_version
@project_version(filename::AbstractString)

Reads the version from the Project.toml file at the given filename, at compile time.
If no filename is given, defaults to `Base.current_project()`.
Intended for use with the [`ArgParseSettings`](@ref) constructor,
to keep the settings version in sync with the project version.

## Example

```jl
ArgParseSettings(add_version = true, version = @project_version)
```
"""
macro project_version(filename::String=Base.current_project())
project_version(filename)
end

macro project_version(expr::Expr)
project_version(eval(expr))
end

function project_version(filename::AbstractString)::String
re = r"^version\s*=\s*\"(.*)\"\s*$"
for line in eachline(filename)
if startswith(line, "[")
break
end
if !occursin(re, line)
continue
end
return match(re, line)[1]
end
throw(ArgumentError("Could not find a version in the file at $(filename)"))
end
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
version = "1.0.0"

[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
23 changes: 22 additions & 1 deletion test/argparse_test08.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# test 08: read args from file
# test 08: read args from file, read version from project

@testset "test 08" begin

Expand Down Expand Up @@ -63,4 +63,25 @@ end
@ap_test_throws ArgParseSettings(fromfile_prefix_chars=['Å'])
@ap_test_throws ArgParseSettings(fromfile_prefix_chars=['8'])

# default project version
@test stringversion(ArgParseSettings(
add_version = true,
version = @project_version
)) == "1.0.0\n"

# project version from filepath
@test stringversion(ArgParseSettings(
add_version = true,
version = @project_version "Project.toml"
)) == "1.0.0\n"

# project version from expression that returns a filepath
@test stringversion(ArgParseSettings(
add_version = true,
version = @project_version(joinpath(@__DIR__, "Project.toml"))
)) == "1.0.0\n"

# throws an error if the file doesn't contain a version
@test_throws ArgumentError ArgParse.project_version("args-file1")

end