c-blake / cligen

Nim library to infer/generate command-line-interfaces / option / argument parsing; Docs at
https://c-blake.github.io/cligen/
ISC License
509 stars 24 forks source link

initFromCL doesn't work with `ref object` #118

Closed timotheecour closed 4 years ago

timotheecour commented 4 years ago
#[
Error: default value is not a tuple or object
    else: error "default value is not a tuple or object"
]#

type Foo = ref object # bug
# type Foo = object # ok
  a1, a2: string
let dfl* = Foo(a1: "junk")
proc main(a: Foo) = discard

when isMainModule:
  import cligen
  var app = initFromCL(dfl)
  app.main()
c-blake commented 4 years ago

Ok. Any thoughts on how to make it work? Does getTypeImpl work on such in other contexts?

timotheecour commented 4 years ago

untried, but maybe something like:

proc process[T](dfl: T) =
  when T is ref | ptr:
    doAssert dfl != nil # could handle this too maybe
    process(dfl[])
  elif T is object:
    sameAsBefore()
c-blake commented 4 years ago

dfl[].getTypeImpl does not work. What we need is to peel the ref part of the type off. We may then need either autoderef or a bunch of [] elsewhere in generated code, but the first step is looping over the right .children.

c-blake commented 4 years ago

E.g., it looks like we could do a branch after of ntyObject

  of ntyRef: ti = ti[0].getTypeImpl[2]

That actually makes your example compile and even generates the correct help message! Could probably do similar for ptr and ptr|ref to tuple types. Running fails in the generated setters, though. We probably just need to set a flag in that branch and elsewhere do the [] conditionally upon that flag. PR welcome if you want to work on it. I won't have time to do more until at least this weekend.

c-blake commented 4 years ago

I could be more careful about checking that the T in ptr|ref T is an object and this could (maybe?) be generalized to tuples, but the basic usage you were asking for should work. PRs welcome if you want to dot those i's and cross those t's.

timotheecour commented 4 years ago

@c-blake wonderful, thanks!