carlobaldassi/ArgParse.jl

Unpredictable behavior when printing from inside custom `parse_item` function.

jbdaley opened this issue · 4 comments

On Julia 0.6 running ArgParse 0.5.0

using ArgParse
struct Foo end
function ArgParse.parse_item(::Type{Foo}, x::AbstractString)
  return Foo()
end
settings = ArgParseSettings(description = "Argparse Test")
@add_arg_table settings begin
  "--foo"
    arg_type = Foo
    required = true
    nargs = '+'
end
argsDict = parse_args(settings)

Reports too many arguments when running julia foo.jl --foo foo bar, but works fine if arg_type is replaced with String.

This is fixed on latest master. Until I release a new version (hopefully soon) you can Pkg.checkout("ArgParse").

I believe I over simplified my minimal example. I checkout out master 224ce77 to be sure. The following works when arg_type = Foo, but not when arg_type = Bar. The issue seems to actually be having print statements in parse_item methods.

using ArgParse
struct Foo end
function ArgParse.parse_item(::Type{Foo}, x::AbstractString)
  return Foo()
end
struct Bar end
function ArgParse.parse_item(::Type{Bar}, x::AbstractString)
  println()
  return Bar()
end
settings = ArgParseSettings(description = "Argparse Test")
@add_arg_table settings begin
  "--foo"
    arg_type = Bar
    required = true
    nargs = '+'
end
argsDict = parse_args(settings)

I see. After a short investigation, this seems like a Julia bug (still present in 0.7), whereby printing anything to stdout/stderr while the parsing is proceeding is causing all sorts of unexpected behavior. I'll try to find a minimal example and I'll open an issue.

this is the culprit: JuliaLang/julia#27168