GithubHelp home page GithubHelp logo

Comments (4)

c-blake avatar c-blake commented on June 18, 2024

AFAIK, Nim procs themselves cannot distinguish if they were called with an explicit value or got the default. That implies the proc in question must know it is a command. In that case we can just have the parser populate a Table (or HashSet) with actually passed commands and the proc can test if they were passed with if "foo" in myProcNamePassed. A Table with a list of values might be more general.

from cligen.

c-blake avatar c-blake commented on June 18, 2024

(I should have said "an explicit value that happens to be the default", of course.)

Thinking about this a little more -- I would say that it is NOT A GREAT IDEA IN GENERAL since it may make the wrapped proc uncallable as an API call from other Nim code. I think of the encouraging retention of API callability as a truly "system positive" feature of cligen, but if it's just for some proc main() and just for a CLI, and you don't care about Nim callers, and it's for backward compatibility, then, eh...There is a way to do it already by redefining argParse for the type of the parameter in question:

import cligen, cligen/parseopt3, cligen/argcvt, strutils, parseutils

var alphaSet = false

proc argParse(dst: var int, dfl: int, a: var ArgcvtParams): bool =
  if a.key == "a" or optionNormalize(a.key) == "alpha":
    alphaSet = true
  let stripped = strip(a.val)
  if len(stripped) == 0 or parseInt(stripped, dst) != len(stripped):
    ERR("Bad value: \"$1\" for option \"$2\"; expecting $3\n$4" %
        [ a.val, a.key, "int", a.help ])
    return false
  return true

proc foo(alpha: int=1, beta: int=2) =
  if alphaSet:
    echo "alpha set changed by user"
  echo alpha, " ", beta

dispatch(foo)

That gets you specificity for any name and any type you want and since I think it's a bad idea in general, that is probably enough support for it.

from cligen.

c-blake avatar c-blake commented on June 18, 2024

Well, I went ahead and added an easier approach for this, but I would strongly encourage only very judicious have-to-use usage.

from cligen.

c-blake avatar c-blake commented on June 18, 2024

Oh, and see test/DetectSet2.nim for a full example. Rather than some binary thing like, well, really all of your proposals, this solution lets the CLI author see the full sequence of unconverted string values that the parser saw for any given parameter name. If certain parameters are string data not needing any conversion that's full information.

To show the CLI author converted values would also be nice, and what would be needed there is also what would be needed to completely separate parsing from dispatching. E.g., you could just have the parser return a Table[string, seq[Any]]. That Any could be some variant type that we synthesized on a per-wrapped-proc basis from the types of values in the parameter list. Then you generate both a parser and a dispatcher where the usual for-dummies mode is dispatcher(parser()). Or even better the parser could be "updating" such that it updates a Table[string, seq[Any]] from the seq[string]. Then it could even be called more than once and tag each value in the seq[Any] with a "source" like config file, environment, command { so really maybe more seq[tuple[source: int, value: Any]] }.

I'm not quite sure if this is actually possible, though..at least not without requiring users to enter type names which IMO would sacrifice a lot of usability. cligen currently leverages the type inference Nim can do for param=def in parameter lists and var param=def. So, right now, CLI authors don't need type names in signatures. I think you would need a type name to define that Any variant. Also, it would probably be a lot of work -- close to a total re-write.

from cligen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.