Skip to content

Commit

Permalink
Move the 'del' kwarg in parse_I() back to a positional arg. (#1619)
Browse files Browse the repository at this point in the history
  • Loading branch information
joa-quim authored Dec 21, 2024
1 parent 085ed2a commit 58e8f8c
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 49 deletions.
36 changes: 23 additions & 13 deletions src/common_options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ function parse_R(d::Dict, cmd::String; O::Bool=false, del::Bool=true, RIr::Bool=

if (RIr)
if (isa(val, GItype))
opt_I = parse_I(d, "", [:I :inc :increment :spacing], "I")
opt_I = parse_I(d, "", [:I :inc :increment :spacing], "I", true)
(opt_I == "") && (cmd *= " -I" * arg2str(val.inc))::String
opt_r = parse_r(d, "")[2]
(opt_r == "") && (cmd *= " -r" * ((val.registration == 0) ? "g" : "p"))
else # Here we must parse the -I and -r separately.
cmd = parse_I(d, cmd, [:I :inc :increment :spacing], "I", del=del)
cmd = parse_I(d, cmd, [:I :inc :increment :spacing], "I", del)
cmd = parse_r(d, cmd, del)[1]
end
end
Expand Down Expand Up @@ -1396,8 +1396,8 @@ end

# ---------------------------------------------------------------------------------------------------
function parse_F(d::Dict, cmd::String)::String
cmd = add_opt(d, cmd, "F", [:F :box], (clearance="+c", fill=("+g", add_opt_fill), inner="+i",
pen=("+p", add_opt_pen), rounded="+r", shaded=("+s", arg2str), shade=("+s", arg2str)) )
cmd = add_opt(d, cmd, "F", [:F :box], (clearance="+c", fill=("+g", add_opt_fill), inner="+i", pen=("+p", add_opt_pen),
rounded="+r", shaded=("+s", arg2str), shade=("+s", arg2str)) )
end

# ---------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1678,7 +1678,7 @@ function parse_common_opts(d::Dict, cmd::String, opts::VMs; first::Bool=true, is
for opt in opts
if (opt == :RIr) cmd, o = parse_RIr(d, cmd)
elseif (opt == :R && !ignore_R) cmd, o = parse_R(d, cmd)
elseif (opt == :I) cmd = parse_I(d, cmd, [:I :inc :increment :spacing], "I")
elseif (opt == :I) cmd = parse_I(d, cmd, [:I :inc :increment :spacing], "I", true)
elseif (opt == :J && !ignore_J) cmd, o = parse_J(d, cmd)
elseif (opt == :JZ) cmd, o = parse_JZ(d, cmd; is3D=is3D, O=!first)
elseif (opt == :G) cmd, = parse_G(d, cmd)
Expand Down Expand Up @@ -1745,11 +1745,11 @@ function parse_theme(d::Dict, del::Bool=true)
end

# ---------------------------------------------------------------------------------------------------
function parse_these_opts(cmd::String, d::Dict, opts, del::Bool=true)::String
function parse_these_opts(cmd::String, d::Dict, opts)::String
# Parse a group of options that individualualy would had been parsed as (example):
# cmd = add_opt(d, cmd, "A", [:A :horizontal])
for opt in opts
cmd = add_opt(d, cmd, string(opt[1]), opt; del=del)
cmd = add_opt(d, cmd, string(opt[1]), opt)
end
return cmd
end
Expand All @@ -1759,7 +1759,7 @@ end
parse_G(d::Dict, cmd::String) = parse_helper(cmd, d, [:G :save :write :outgrid :outfile], " -G")

# ---------------------------------------------------------------------------------------------------
function parse_I(d::Dict, cmd::String, symbs, opt::String; del::Bool=true)::String
function parse_I(d::Dict, cmd::String, symbs, opt::String, del::Bool)::String
# Parse the quasi-global -I option. But arguments can be strings, arrays, tuples or NamedTuples
# At the end we must recreate this syntax: xinc[unit][+e|n][/yinc[unit][+e|n]] or
get_that_string(arg)::String = string(arg)::String # Function barrier. Shuting up JET, etc.
Expand Down Expand Up @@ -1990,11 +1990,11 @@ function build_pen(d::Dict, del::Bool=false)::String
end
lw::String = ""
else
lw = add_opt(d, "", "", [:lw :lt :linewidth :linethick :linethickness]; del=del) # Line width
lw = add_opt(d, "", "", [:lw :lt :linewidth :linethick :linethickness]) # Line width
end
(lw == "" && find_in_dict(d, [:line])[1] !== nothing) && (lw = "0.5p") # Means, accept also line=true

ls::String = add_opt(d, "", "", [:ls :linestyle]; del=del) # Line style
ls::String = add_opt(d, "", "", [:ls :linestyle]) # Line style
lc::String = parse_pen_color(d, [:lc :linecolor], del)
out::String = ""
if (lw != "" || lc != "" || ls != "")
Expand Down Expand Up @@ -2330,7 +2330,7 @@ function add_opt_1char(cmd::String, d::Dict, symbs::Vector{Matrix{Symbol}}; del:
end

# ---------------------------------------------------------------------------------------------------
function add_opt(d::Dict, cmd::String, opt::String, mapa::NamedTuple; del::Bool=true)::String
function add_opt(d::Dict, cmd::String, opt::String, mapa::NamedTuple)::String
cmd_::String = ""
for k in keys(mapa)
((val_ = find_in_dict(d, [k], false)[1]) === nothing) && continue # This mapa key was not used
Expand All @@ -2346,7 +2346,17 @@ function add_opt(d::Dict, cmd::String, opt::String, mapa::NamedTuple; del::Bool=
return cmd
end

function add_opt(d::Dict, cmd::String, opt::String, symbs::VMs, mapa=nothing; grow_mat=nothing, del::Bool=true, expand::Bool=false, expand_str::Bool=false)::String
#
function add_opt(d::Dict, cmd::String, opt::String, symbs::VMs)::String
((val = find_in_dict(d, symbs, true)[1]) === nothing) && return cmd
isa(val, AbstractDict) && (val = Base.invokelatest(dict2nt, val))
isa(val, NamedTuple) && return cmd # Happens when the inline 'inset' passed a NT with options for inset itself and not the module it called
args = arg2str(val)::String
return (opt != "") ? string(cmd, " -", opt, args) : string(cmd, args)
end
#

function add_opt(d::Dict, cmd::String, opt::String, symbs::VMs, mapa; grow_mat=nothing, del::Bool=true, expand::Bool=false, expand_str::Bool=false)::String
# Scan the D Dict for SYMBS keys and if found create the new option OPT and append it to CMD
# If DEL == false we do not remove the found key.
# 'grow_mat=mat', is a special case to append to a matrix (can't realy be done in Julia)
Expand All @@ -2358,7 +2368,7 @@ function add_opt(d::Dict, cmd::String, opt::String, symbs::VMs, mapa=nothing; gr

if ((val = find_in_dict(d, symbs, del)[1]) === nothing)
if (expand && isa(mapa, NamedTuple))
cmd = add_opt(d, cmd, opt, mapa; del=del)
cmd = add_opt(d, cmd, opt, mapa)
end
return cmd
elseif (expand_str && isa(mapa, NamedTuple)) # Use the mapa KEYS as possibe values of 'val'
Expand Down
2 changes: 1 addition & 1 deletion src/gdal_tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ function GMT_opts_to_GDAL(f::Function, opts::Vector{String}, kwargs...)
#f == gdalgrid ? append!(opts, ["-txe", s[1], s[2], "-tye", s[3], s[4]]) : append!(opts, ["-projwin", split(opt_R[4:end], '/')[[1,4,2,3]]...]) # Ugly
end
((opt_J = GMT.parse_J(d, "", default=" ")[1]) != "") && append!(opts, ["-a_srs", opt_J[4:end]])
if ((opt_I = GMT.parse_I(d, "", [:I :inc :increment :spacing], "I")) != "") # Need the 'I' to not fall into parse_I() exceptions
if ((opt_I = GMT.parse_I(d, "", [:I :inc :increment :spacing], "I", true)) != "") # Need the 'I' to not fall into parse_I() exceptions
t = split(opt_I[4:end], '/')
(length(t) == 1) ? append!(opts, ["-tr", t[1], t[1]]) : append!(opts, ["-tr", t[1], t[2]])
end
Expand Down
10 changes: 1 addition & 9 deletions src/gmt2kml.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ Parameters
- $(opt_V)
- $(opt_write)
- $(opt_append)
- $(_opt_bi)
- $(_opt_di)
- $(opt_e)
- $(_opt_f)
- $(_opt_h)
- $(_opt_i)
- $(opt_swap_xy)
To see the full documentation type: ``@? gmt2kml``
"""
Expand All @@ -83,10 +76,9 @@ function gmt2kml_helper(cmd0::String, arg1; kwargs...)
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode

cmd, = parse_common_opts(d, "", [:R :V_params :bi :di :e :f :h :i :yx])
cmd = parse_these_opts(cmd, d, [[:A :altitude_mode], [:D :descript], [:E :extrude], [:F :feature_type],
cmd = parse_these_opts(cmd, d, [[:A :altitude_mode], [:D :descript], [:E :extrude], [:F :feature_type], [:G :fill],
[:I :icon], [:K :not_over], [:L :extra_data], [:N :feature_name], [:O :overlay], [:Qa :wiggles], [:Qi :wiggle_fixedazim], [:Qs :wiggle_scale], [:S :ilscale], [:T :title], [:Z :attrib]])

cmd = add_opt(d, cmd, "G", [:G :fill])
cmd *= add_opt_pen(d, [:W :pen], opt="W")

cmd, got_fname, arg1 = find_data(d, cmd0, cmd, arg1)
Expand Down
16 changes: 8 additions & 8 deletions src/gmtbinstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ Parameters
To see the full documentation type: ``@? gmtbinstats``
"""
function binstats(fname::String=""; nbins=0, kwargs...)
function binstats(fname::String; nbins=0, kwargs...)
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
arg1 = read_data(d, fname, "", nothing, " ", false, true)[2] # Make sure we have the data here
binstats(arg1; nbins=0, d...)
binstats_helper(arg1, nbins, d)
end
function binstats(arg1; nbins=0, kwargs...)
#function binstats(cmd0::String="", arg1=nothing; nbins=0, kwargs...)

function binstats(arg1; nbins=0, kwargs...)
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
#arg1 = read_data(d, "", "", arg1, " ", false, true)[2] # Make sure we have the data here
data = mat2ds(arg1)
binstats_helper(mat2ds(arg1), nbins, d)
end

function binstats_helper(data::GDtype, nbins::Int, d::Dict{Symbol,Any})

isa(data, Vector{<:GMTdataset}) && isempty(data[1].ds_bbox) && set_dsBB!(data)

cmd, = parse_common_opts(d, "", [:G :I :R :V_params :a :bi :di :e :f :g :h :i :q :r :w :yx])
Expand Down Expand Up @@ -116,6 +118,4 @@ function binstats(arg1; nbins=0, kwargs...)
end

# ---------------------------------------------------------------------------------------------------
#binstats(arg1; kw...) = binstats("", arg1; kw...)

const gmtbinstats = binstats # Alias
11 changes: 7 additions & 4 deletions src/gmtconvert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ Parameters
To see the full documentation type: ``@? gmtconvert``
"""
function gmtconvert(cmd0::String="", arg1=nothing; kwargs...)
gmtconvert(cmd0::String; kwargs...) = gmtconvert_helper(cmd0, nothing; kwargs...)
gmtconvert(arg1; kwargs...) = gmtconvert_helper("", arg1; kwargs...)

function gmtconvert_helper(cmd0::String, arg1; kwargs...)
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
gmtconvert_helper(cmd0, arg1, d)
end
# ---------------------------------------------------------------------------------------------------
function gmtconvert_helper(cmd0::String, arg1, d::Dict{Symbol,Any})

cmd, = parse_common_opts(d, "", [:V_params :write :append :a :b :bo :d :e :f :g :h :i :o :q :s :w :yx])
cmd = parse_these_opts(cmd, d, [[:A :hcat], [:C :n_records], [:D :dump], [:E :first_last], [:F :conn_method],
Expand All @@ -64,6 +70,3 @@ function gmtconvert(cmd0::String="", arg1=nothing; kwargs...)
(!contains(cmd, " -b") && isa(out, GDtype) && cmd0 != "") && file_has_time!(cmd0, out) # Try to guess if time columns
return out
end

# ---------------------------------------------------------------------------------------------------
gmtconvert(arg1; kw...) = gmtconvert("", arg1; kw...)
8 changes: 5 additions & 3 deletions src/gmtinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ Parameters
gmtinfo(cmd0::String; kwargs...) = gmtinfo_helper(cmd0, nothing; kwargs...)
gmtinfo(arg1; kwargs...) = gmtinfo_helper("", arg1; kwargs...)

function gmtinfo_helper(cmd0::String, arg1; kwargs...)
d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
gmtinfo_helper(cmd0, arg1, d)
end
# ---------------------------------------------------------------------------------------------------

function gmtinfo_helper(cmd0::String, arg1; kwargs...)::Union{String, GMTdataset}

d = init_module(false, kwargs...)[1] # Also checks if the user wants ONLY the HELP mode
function gmtinfo_helper(cmd0::String, arg1, d::Dict{Symbol,Any})::Union{String, GMTdataset}

cmd, = parse_common_opts(d, "", [:V_params :e :f :i :o :r :w :yx])
(endswith(cmd, "-:")) && (cmd *= "i") # Need to be -:i not -: to not swap output too
Expand Down
2 changes: 1 addition & 1 deletion src/grdvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function grdvector(arg1, arg2; first=true, kwargs...)
max_extrema = max(abs(info[5]), abs(info[6]), abs(info2[5]), abs(info2[6])) # The max of the absolute extremas
as = 1.05 * max_extrema * sqrt((n_rows*n_cols) / (w*h)) # Autoscale (approx). Idealy it should be max magnitude.

opt_I = parse_I(d, "", [:I :inc :increment :spacing], "I")
opt_I = parse_I(d, "", [:I :inc :increment :spacing], "I", true)
multx = multy = 1
if (opt_I == "")
# To estimate the "jumping" factor, we use a virtual 'maxlen' that is the maximum length that
Expand Down
2 changes: 1 addition & 1 deletion src/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ function bar3(cmd0::String="", arg=nothing; first=true, kwargs...)
(opt_base == "") ? push!(d, :base => 0) : push!(d, :base => opt_base)
arg1 = gmt("grd2xyz", arg1) # Now arg1 is a GMTdataset
else
opt_S = parse_I(d, "", [:S :width], "So", del=true)
opt_S = parse_I(d, "", [:S :width], "So", true)
if (opt_S == "")
opt_S = parse_bar_cmd(d, :bar, "", "So"; no_u=true)[1]
end
Expand Down
18 changes: 9 additions & 9 deletions test/test_common_opts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
@test GMT.parse_l(Dict(:l => "ai ai"), "")[2] == " -l\"ai ai\""
@test GMT.parse_l(Dict(:l => (text="ai ai", vspace=3)), "")[2] == " -l\"ai ai\"+G3"
@test GMT.parse_n(Dict(:n => (bicubic=true,antialiasing=true,bc=:g)), "")[2] == " -nc+bg"
@test GMT.parse_I(Dict(:inc => (x=1.5, y=2.6, unit="meter")),"", [:I :inc], "I") == " -I1.5e/2.6e"
@test GMT.parse_I(Dict(:inc => (x=1.5, y=2.6, unit="m")),"", [:I :inc], "I") == " -I1.5m/2.6m"
@test GMT.parse_I(Dict(:inc => (x=1.5, y=2.6, unit="data")),"", [:I :inc], "I") == " -I1.5/2.6u"
@test GMT.parse_I(Dict(:inc => (x=1.5, y=2.6, extend="data")),"", [:I :inc], "I") == " -I1.5+e/2.6+e"
@test GMT.parse_I(Dict(:inc => (x=1.5, y=2.6, unit="nodes")),"", [:I :inc], "I") == " -I1.5+n/2.6+n"
@test GMT.parse_I(Dict(:inc => (2,4)),"", [:I :inc], "I") == " -I2/4"
@test GMT.parse_I(Dict(:inc => [2 4]),"", [:I :inc], "I") == " -I2/4"
@test GMT.parse_I(Dict(:inc => "2"),"", [:I :inc], "I") == " -I2"
@test GMT.parse_I(Dict(:inc => "2"),"", [:I :inc], "") == "2"
@test GMT.parse_I(Dict(:inc => (x=1.5, y=2.6, unit="meter")),"", [:I :inc], "I", true) == " -I1.5e/2.6e"
@test GMT.parse_I(Dict(:inc => (x=1.5, y=2.6, unit="m")),"", [:I :inc], "I", true) == " -I1.5m/2.6m"
@test GMT.parse_I(Dict(:inc => (x=1.5, y=2.6, unit="data")),"", [:I :inc], "I", true) == " -I1.5/2.6u"
@test GMT.parse_I(Dict(:inc => (x=1.5, y=2.6, extend="data")),"", [:I :inc], "I", true) == " -I1.5+e/2.6+e"
@test GMT.parse_I(Dict(:inc => (x=1.5, y=2.6, unit="nodes")),"", [:I :inc], "I", true) == " -I1.5+n/2.6+n"
@test GMT.parse_I(Dict(:inc => (2,4)),"", [:I :inc], "I", true) == " -I2/4"
@test GMT.parse_I(Dict(:inc => [2 4]),"", [:I :inc], "I", true) == " -I2/4"
@test GMT.parse_I(Dict(:inc => "2"),"", [:I :inc], "I", true) == " -I2"
@test GMT.parse_I(Dict(:inc => "2"),"", [:I :inc], "", true) == "2"
@test GMT.parse_JZ(Dict(:JZ => "5c"), "")[1] == " -JZ5c"
@test GMT.parse_JZ(Dict(:Jz => "5c"), "")[1] == " -Jz5c"
@test GMT.parse_JZ(Dict(:aspect3 => 1), " -JX10")[1] == " -JX10 -JZ10"
Expand Down

0 comments on commit 58e8f8c

Please sign in to comment.