-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Value type aliases. #32
Comments
I love writing CLI apps and I'd been dying to replace the framework I'd been using for something better, perhaps forking one, so contributing has been fun. |
Hey! Once again, you've got an interesting enhancement proposal. I don't take it any way as a discredit to Cova, just that you're looking to meet your own project requirements. On the contrary, I'm always on the lookout for feasible new ideas, so I appreciate it! To the proposal itself, I'd like to clear up a few notions that might've been missed about Values and how they work first, then see if there's an area we can make this work. The Conversely, the In your example, There's also a chance I'm missing something while reading through it. If so, please help me understand better. |
To me it sounds like the most usefully written parse_fn's should cover the needs of valid_fn, erroring out if the parsing fails or if the successfully parsed value is invalid. This seems better to me than separate functions. |
That's fair. Thankfully, there's nothing stopping anyone from using More to your issue though, which part isn't working? To me, it looks like your example works as intended, but I'm sure I'm missing something. |
I made that parsePath work that way because for some reason I thought input type needed to equal output type, so what I sent does work, but what I was trying to demonstrate is that I could do it better if the types could differ. Since they can, I'll update you when I've adjusted things. |
Gotcha. I knew I was missing something. Please do let me know when you've tried again. Separately, I've updated the mechanics for adding your own types as The details are in these two commits (which I added the wrong issue reference to): You can see an example of overwriting |
My only concern is that when I make the option's value type an OS specific file address (which is a number, not a path), I still want the option's help menu to say that they're supposed to provide a path and not the actual file address. The reason I have to do this at all is because Windows doesn't expose its equivalent to stdin/out/err as file accessible via a file path, such as is available in Unix systems; the only way to do this is to rely on that address I talked about before. |
Just to make sure I'm tracking, you're saying you want the Option parsed differently depending on the OS? As in, Windows gets parsed into a Int while (presumably) Linux gets parsed to a String? If so, you could probably use some compile time code to change that specific Option altogether depending on the targeted OS. Or, you could use the same technique after the fact during analysis, but it sounds like you're trying to do that during parsing. If this is a path you're interested in, I could try and create an example. |
Looking at it more carefully, I think this is the part you're addressing more here. With how things are currently set up, I would use a different Alternatively, you can make a custom |
No, I do not want it to handle anything differently based on OS. To clarify, what I want is for Cova to ask the user for a string (a path), and I want it to be able to parse that path from within my parse_fn and store the result for me not as a string, but as the host OS's address to that file. It's very important that stdin/out/err is handled correctly, and because Windows doesn't make its equivalent available via a path, I can only go by file descriptor (look I remembered the term). Current function: pub fn parsePath(arg: []const u8, _: mem.Allocator) ![]const u8 {
os.access(arg, os.F_OK) catch |err| {
// Windows doesn't make stdin/out/err available via system path,
// so this will have to be handled outside Cova
if (mem.eql(u8, arg, "-")) return arg;
return err;
};
return arg;
} |
I assumed you meant file descriptor or that file address was a related term I just hadn't heard yet, haha. Ok, so using file descriptor regardless of OS. Tracking that part now. If I'm not mistaken, file descriptors are just UInts (or maybe Ints?) that are pointer casted to file locations. That means you're likely going to set up an Option with a U/Int child Type. The remaining issue seems like the Type hint from the Usage message. Since you can't change the actual Type hint with In that case, would it help to have an |
I don't think that's a good solution, I think the simpler the solution the better. There's gotta be a way to specify a different value from the outside than is stored on the inside. And I'm only asking for this because I think a framework like this should be able to define every aspect of your CLI without having additional code related to it bleeding into your project. If cova wasn't so close to that already, I wouldn't be asking for this, is what I'm saying. Apologies for confusions. |
No need to apologize, I enjoy the challenge! An even simpler solution could be a type alias. Think |
Much simpler indeed, and ironically is a better description of what I was originally suggesting at the beginning of this issue! |
- Added `child_type_alias` field to `Value.Typed` that will be used in place of the actual Child Type for Usage/Help messages. - Updated Docs to reflect Type Aliases. - Closes #32
How does |
I can try and create an example later, but this comes down to either me adding |
I'm actually going to attempt the latter now that I've thought this through a little more; I'll let you know how it goes. |
Sounds good! When I have more free time, I'll also look to make an example and possibly implement a few changes to callbacks to make things both more clear and flexible. |
I think dropping the requirement that the input and return values of
parse_fn
share the same type would lead to a more efficient/flexible CLI framework, but it would increase the code size.I can show it's benefit here:
where specifically parsePath() cannot perform all parsing actions due to this type limitation. I think adjustment could literally support any kind of value parsing from the commandline to the program directly! (reducing the CLI code bleeding into in the meat of your program)
Let me know what you think (and I promise in no way am I suggesting your project wasn't already wonderful, I'm just excited to see what I could do with this)
The text was updated successfully, but these errors were encountered: