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

Any idea why this happens? Error: 'from' is only allowed at top level #96

Closed pb-cdunn closed 5 years ago

pb-cdunn commented 5 years ago
     Error: Build failed for package: pb
        ... Details:
        ... Execution failed with exit code 1
        ... Command: "/mnt/software/n/nim/0.19.9/Nim/bin/nim" c --noBabelPath -d:release --path:"/scratch/cdunn/repo/nim-falcon/nimbleDir/pkgs/cligen-0.9.19"  -o:"/localdisk/scratch/cdunn/repo/utils/pb" "/localdisk/scratch/cdunn/repo/utils/src/pb.nim"
        ... Output: pb.nim(8, 11) template/generic instantiation of `dispatch` from here
        ... ../../nim-falcon/nimbleDir/pkgs/cligen-0.9.19/cligen.nim(674, 14) template/generic instantiation of `dispatchGen` from here
        ... ../../nim-falcon/nimbleDir/pkgs/cligen-0.9.19/cligen.nim(573, 5) Error: 'from' is only allowed at top level

Oddly, this does not happen for another package, using the same $NIMBLE_DIR. Here is my simple program:

import cligen

proc main(extras: seq[string], int_dummy: int = 42, string_dummy: string = "hello") =
    echo "pb main"

if isMainModule:
  dispatch(main, short={}, help={})
pb-cdunn commented 5 years ago

Even simpler:

import cligen
import os

proc main*(): int =
    echo "pb main"

if isMainModule:
  dispatch(main)

Same error!

pb-cdunn commented 5 years ago
if isMainModule:

must be

when isMainModule:

Of course!

pb-cdunn commented 5 years ago

Btw, this is super cool:

% ./pb data -h
Unknown subcommand "data".  Maybe you meant one of:
        dataset

Great idea!

c-blake commented 5 years ago

Glad you figured out your mistake.

On the topic of your final comment, did you read my message about maybe doing Mercurial/gdb/gnuplot-like acceptance of the shortest uniquely identifying prefix command/option/enum value? In this example, the command would have just executed if you had no other commands prefixed by "data".

That idea has a strong flavor of Do What I Mean / I don't care how verbose the CLI author made things, I'm going to use the short way. Indeed, it creates almost a middle-step from "short options" to "longest options" with this intermediate shortest matching prefix "long-ish" option/string matching.

It doesn't really obsolete the suggestion framework, though it might make more sense to expand that to understand the difference between "does not match a unique prefix" vs "no string starts with that prefix". and, of course, like the long options, subcommands, and enums it would apply to all three cases. It probably helps more in multi-command/enum settings.

It's kind of a rare/obscure feature, though in some ways multi-commands are all a bit more rare/idiosyncratic. Personally, I like it a lot. One of the frustrations I have moving between mercurial and git repos is git requiring all this specificity..I miss just "st" instead of the longer "status". But I get that people (especially detail-oriented programmers) also want specificity. So, if I ever do add this it will be easy to turn off or not on by default.

pb-cdunn commented 5 years ago

On the topic of your final comment, did you read my message about maybe doing Mercurial/gdb/gnuplot-like acceptance of the shortest uniquely identifying prefix command/option/enum value? In this example, the command would have just executed if you had no other commands prefixed by "data".

I like the current way better. I'm not a fan of automating everything, but as a user I love to be informed of what I probably should have done. It saves time, and it feels empowering.

c-blake commented 5 years ago

Ok. One vote for "off by default" if I ever add that. Fair enough. :-)

pb-cdunn commented 5 years ago

Actually, accepting truncated sub-commands would be very useful, similar to truncated options.

c-blake commented 5 years ago

Hmm. What did you think I meant by shortest uniquely identifying prefix if not truncation? I think that means the same thing..Just one has more a "set of choices" perspective while the other is maybe more user-centered.

pb-cdunn commented 5 years ago

Oh, then yes. Up-vote!

c-blake commented 5 years ago

Ok. This one's a bit more work, though. Normalizing a string for matching purposes requires the whole set of choices, not just the identifier itself..normalizing is sort of the "maximum truncation that preserves uniqueness". So, internal stuff has to change to support it/make those choice sets available at matching time. Not sure when/if I'll get around to it, but it's obv. quite off topic for this issue. :-) You just mentioned liking the suggestion framework and this is a related idea.