8000 add argtypes for better autocompletion by Roger-luo · Pull Request #190 · comonicon/Comonicon.jl · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add argtypes for better autocompletion #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2022
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 src/Comonicon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export @cast, @main, cmd_error, cmd_exit
include("compat.jl")
include("configs.jl")
include("exceptions.jl")
include("argtypes.jl")
include("ast/ast.jl")
include("codegen/julia.jl")
include("codegen/zsh.jl")
Expand Down
70 changes: 70 additions & 0 deletions src/argtypes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module Arg

export FileName, DirName,
UserName, Path, Prefix, Suffix,
@Prefix_str, @Suffix_str

abstract type ArgType end

struct Path <: ArgType
content::String
end

struct DirName <: ArgType
content::String
end

struct FileName <: ArgType
content::String
end

struct UserName <: ArgType
content::String
end

struct Prefix{name} <: ArgType
content::String
end

struct Suffix{name} <: ArgType
content::String
end

macro Prefix_str(s::String)
return Prefix{Symbol(s)}
end

macro Suffix_str(s::String)
return Suffix{Symbol(s)}
end

Base.show(io::IO, x::Prefix{name}) where name = print(io, "Prefix\"", name, "\"")
Base.show(io::IO, x::Suffix{name}) where name = print(io, "Suffix\"", name, "\"")

# use rust-like enum instead?
# @renum ArgType begin
# Path
# DirName
# FileName
# UserName
# Prefix(name::String)
# AnyType(name::String)
# end

function Base.tryparse(::Type{T}, s::AbstractString) where {T <: ArgType}
return T(s)
end

function Base.tryparse(::Type{Prefix{name}}, s::AbstractString) where {name}
prefix = string(name)
startswith(s, prefix) && return Prefix{name}(s[length(prefix)+1:end])
return
end

function Base.tryparse(::Type{Suffix{name}}, s::AbstractString) where {name}
suffix = string(name)
endswith(s, suffix) && return Suffix{name}(s[1:end-length(suffix)])
return
end

end
5 changes: 4 additions & 1 deletion src/ast/ast.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module AST

export ComoniconExpr, Description, LeafCommand, NodeCommand, Entry, Argument, Option, Flag, print_cmd
export ComoniconExpr,
Description,
LeafCommand, NodeCommand,
Entry, Argument, Option, Flag, print_cmd

include("types.jl")
include("printing.jl")
Expand Down
8 changes: 8 additions & 0 deletions test/argtype.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Test
using Comonicon: Arg

@testset "tryparse(::ArgType, s)" begin
@test tryparse(Arg.Path, "random-string").content == "random-string"
@test tryparse(Arg.Prefix"file-", "file-content").content == "content"
@test tryparse(Arg.Suffix".py", "script.py").content == "script"
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ end
include("options.jl")
end

@testset "argtype" begin
include("argtype.jl")
end

@testset "ast" begin
include("ast/ast.jl")
end
Expand Down
0