Papierkorb / toka

A type-safe, object-oriented option parser
MIT License
8 stars 4 forks source link

Type-Safe for Support for Positional Arguments #5

Open stellarpower opened 1 month ago

stellarpower commented 1 month ago

Hi,

This looks a lot neater than using OptParse, nice work!

I usually am writing short scripts, where I want mainly positional arguments. It seems Toka just keeps these at the end as an array of strings for me to process. I'd like to be able to include the type-safety the same way as flagged options, but, without using the flags.

Would it potentially be possible to include unsigned integers in the mappings as a way of achieving this? Such that something like this:

Toka.mapping({
    1: String , # Mandatory option
    2: Uint32 , 
    3: String?, # Optional option
})

would also parse the found positional arguments. Perhaps specifying a name would be useful as well, but we could potentially also just index in with []:

Toka.mapping({
    1: { :name    , String  }, # Mandatory option
    2: { :shoeSize, Uint32  }, 
    3: { :nickname, String? }, # Optional option
})

Sure there would be many ways to achieve it, but however you think suits bets, would be nice functionality to have.

Thanks!

Papierkorb commented 1 month ago

Hello!

I'm surprised to hear that toka still works with a recent compiler!

Anyway, interesting idea! It's suggest extending the advanced feature, e.g. have a positional: Int32 that, if given, parses the nth positional argument out of the positional arguments list.

I've not used Crystal in a long time, so I don't think I'll be of much use implementing this feature. Would you like to take over the toka repository? I only ask to be kept in the credits section somewhere 😊

Cheers!

stellarpower commented 1 month ago

Hey, yes, I know what you mean haha! I sometimes get very long backtraces out of it with code that previously just worked but something has updated somewhere and then it's broken.

Crystal is probably the sort of thing I don't have time to maintain (and like you, whilst I do like using it for script sorts of things, sometimes can go a long time without writing any), but I can modify as I go on projects. So now I know Toka exists I'll probably use it going forwards, make a fork some time and see if I can add this feature next time I need to parse arguments. May be promoting it on the forum etc. might get some others using it who can contribute back features - as Crystal has a lot of this stuff in the stdlib I hadn't really thought of looking elsewhere, an I expect others might be in the same boat.

Thanks!