defopt icon indicating copy to clipboard operation
defopt copied to clipboard

feature request: support "count" action

Open Spectre5 opened this issue 3 years ago • 2 comments

I'd like to be able to use the argparase "count" action. A common use for this is something like program -v to enable verbose message and then program -vv or program -v -v to increase the verbosity.

Is there a simple way to achieve this with a custom parser? It wouldn't actually accept a string, so maybe not. I have no tried that though.

I suppose this could use the same solution as proposed in #95. I've added another comment there.

Spectre5 avatar Jan 22 '22 07:01 Spectre5

I don't think this is possible right now. Something like #95 is perhaps possible. Alternatively I thought that we may be able to introduce a type alias like defopt.Count = int and special-case those, but type aliases don't exist at runtime; or a NewType (defopt.Count = NewType("Count", int)) but I think that would prevent (from a mypy PoV) from directly calling the function with an int, which may or may not be a problem?

anntzer avatar Jan 22 '22 11:01 anntzer

Ya, I had considered some sort of custom type too, but I had the same concern with mypy. Perhaps a solution could be found in a union type with a custom one (considered first) or an int (i.e. Union[CustomInt, int]). With that said, at this point we're changing the function anyway and then the solution in #95 is better and could cover this use case.

Spectre5 avatar Jan 22 '22 18:01 Spectre5