Skip to content

Commit

Permalink
Merge pull request #67 from BKaperick/fix_argparsing
Browse files Browse the repository at this point in the history
Fix ARGS parsing
  • Loading branch information
dmolina authored Dec 17, 2023
2 parents 85a57ac + 1e41546 commit a1942e2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/DaemonMode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,45 @@ function serverRun(run, sock, shared, print_stack, fname, args, reviser)

end

"""
parse_arguments(args_str::String)
Parse the argument string handling quoted arguments, and escaped quotes correctly.
# Parameters
- shared: string of arguments separated by one or many spaces
"""
function parse_arguments(args_str::String)
args_out = []
quotes = Set(['\'','"'])
whitespace = Set([' '])
escape_chars = Set(['\\'])
in_quote = false
escaped = false
current = ""
for c in collect(args_str)
if ~escaped && c in quotes
in_quote = ~in_quote
elseif c in whitespace
if in_quote
current = string(current, c)
elseif length(current) > 0
push!(args_out, current)
current = ""
end
elseif ~(c in escape_chars)
current = string(current, c)
end
escaped = false
if c in escape_chars
escaped = true
end
end
push!(args_out, current)
return args_out
end

"""
serverRunFile(sock, shared)
Expand All @@ -389,7 +428,7 @@ function serverRunFile(sock, shared, print_stack, reviser)
dir = readline(sock)
fname = readline(sock)
args_str = readline(sock)
args = split(args_str, " ")
args = parse_arguments(args_str)

if !isempty(args) && isempty(args[1])
empty!(args)
Expand Down Expand Up @@ -508,7 +547,7 @@ function runfile(fname::AbstractString; args=String[], port = PORT, output=stdou
println(sock, token_runfile)
println(sock, pwd())
println(sock, fcompletename)
println(sock, join(args, " "))
println(sock, string("\"", join(args, "\" \""), "\""))
line = readline(sock)
token_size = length(token_ok_end)

Expand Down
10 changes: 10 additions & 0 deletions test/test_argparse.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Test
using DaemonMode

@testset "Test argument parsing" begin
@test DaemonMode.parse_arguments("arg1 \"arg2 is in quotes\" arg3") == ["arg1", "arg2 is in quotes", "arg3"]

@test DaemonMode.parse_arguments("arg1 arg2 \"arg3 is in quotes\"") == ["arg1", "arg2", "arg3 is in quotes"]

@test DaemonMode.parse_arguments("arg1 arg2 \"arg3 has escaped \\\"quotes\\\"") == ["arg1", "arg2", "arg3 has escaped \"quotes\""]
end

0 comments on commit a1942e2

Please sign in to comment.