Comments (4)
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.
(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.
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.
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)
- Unhelpful compilation error when "short" param name is given as string HOT 5
- `parseHelps` fails on a single element HOT 6
- FR: Display textual representations of available enum values in the `clValType` column HOT 9
- How do I make global options? HOT 14
- [Bug] nim 1.0.6/1.4.8 doesn't have isNaN/isnan HOT 3
- How to use `--version`? HOT 4
- truncate default args in help menu HOT 5
- Sharing param help messages between commands HOT 2
- Some tiny remarks HOT 1
- Removing entries from a seq results in a non-zero exit HOT 10
- Setting `clCfg.helpSyntax = ""` doesn't prevent inclusion of the syntax into the binary HOT 15
- No error for passing non-static table to `help` or `short` of `dispatch` HOT 1
- No interpolation of const values in a literal passed to `help` or `short` of `dispatch` HOT 9
- [Feature request] Support for std/options HOT 7
- Sync `clCfg.version` with package HOT 3
- turning off prefix completion? HOT 14
- How to bold usage and clCfg.useHdr text from code? HOT 16
- `--help` on the multicommand programs without the command should at least list the available commands HOT 13
- "Bad enum value" response starts to and then doesn't list alternatives to the empty string HOT 2
- `strUt` requires Nim 1.2+ HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cligen.