c-blake / cligen

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

Allow custom --version argument #169

Closed vitreo12 closed 3 years ago

vitreo12 commented 3 years ago

Hello, First of all, great job on this! I have been using this package quite a lot :)

I have been trying to dispatch a function that has its own version argument, but I am getting this error:

Error: duplicate case label

I found a workaround by explicitly setting clCfg.version, but I find this to be an ugly workaround, as it doesn't allow, for example, to access the version variable in my code, for example to have a custom printing.

Is there a way to allow this? I'd like to avoid naming my version argument to something else

c-blake commented 3 years ago

First, you're welcome. Second...Hmm. There should be no generated case "version" if clCfg.version.len == 0. You should be able to do what you want (though you should need to import cligen pre-proc definition so it can raise VersionOnly).

Yeah...running nim c -d:printDispatch junk.nim on some

import cligen
proc foo(fersion="hi") = discard
dispatch(foo)

shows the bug right away. I'm generating a case version even with a clCfg.version.len == 0. Oops! I will fix that in a short while. Sorry.

c-blake commented 3 years ago

Also, in terms of your problem "in the large"/workaround, you should look at test/Version.nim for other ways to set the version string.

c-blake commented 3 years ago

I just pushed a fix that lets the below work as I believe you want:

import cligen
proc foo(version=false) =
  if version:
    echo "my version is 1.0"
    raise newException(VersionOnly, "")
dispatch(foo)

I would still recommend looking at test/Version.nim for other ways to set your clCfg.version, though.

vitreo12 commented 3 years ago

Thanks a lot! :)

c-blake commented 3 years ago

@kaushalmodi advocated the git describe --tags HEAD used in the versionGit branch of test/Version.nim, for example, which lets you at compile-time put in the most relevant sort of version string.

c-blake commented 3 years ago

Also, in case anyone found this closed issue from a search, another reason to deploy this fix is just if someone happens to want to wrap a proc with a parameter named "version" that means something very different (e.g. "fetch this version" of something...). I am aware that "help" and "helpsyntax" theoretically have the same problem, but both seem less likely to collide in practice and serve a more critical internal function.

kaushalmodi commented 3 years ago

@kaushalmodi advocated the git describe --tags HEAD used in the versionGit branch of test/Version.nim

Yes, I still use it in my projects using cligen. And I still use cligen :D It's just that I am not too active on github lately.