c-blake / cligen

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

Fix `parseopt3.getopt` usage example #228

Closed ZoomRmc closed 10 months ago

ZoomRmc commented 10 months ago

A tiny doc fix to process the CmdLineKind enum exhaustively.

Perhaps a candidate for a runnableExample (yet to be used in cligen)?

c-blake commented 10 months ago

Yeah... I never added runnableExample s. We can PR those, too if you like. Happy to just merge this if it's ready.

ZoomRmc commented 10 months ago

Yeah... I never added runnableExample s. We can PR those, too if you like. Happy to just merge this if it's ready.

Well, adding them will require a considerable extension of the provided examples to make them fully compile. If you're ok with it, I'll add one here (and possibly to some other user-facing routines).

c-blake commented 10 months ago

FWIW, this works both with cligen/parseopt3 and with std/parseopt -- if such compatibility matters to you for a runnableExample (as per private correspondence):

import strutils, cligen/parseopt3 # can use std/parseopt if wanted

var p = initOptParser("-cj4 --second bar --blah:42 --third foo".split,
                      shortNoVal = {'c'}, longNoVal = @["third"])

for kind, key, val in p.getopt():
  case kind
  of cmdEnd: doAssert(false) # Doesn't happen with getopt()
  of cmdShortOption, cmdLongOption:
    if val == "": echo "Option: ", key
    else: echo "Option and value: ", key, ", ", val
  of cmdArgument: echo "Argument: ", key
  else: discard # cligen also has `cmdError`

with output:

Option: c
Option and value: j, 4
Option and value: second, bar
Option and value: blah, 42
Option: third
Argument: foo

Yes, cligen has a cmdError that std/parseopt does not - I am not sure that makes for the best example code.. maybe with a comment?

Maybe we should also add a firstArg: string overload to parseopt3 for cross-compatibility? Just some thoughts. I'm happy with any doc changes you find clarifying, really. I'll just merge this for now.