Tyrrrz / CliFx

Class-first framework for building command-line interfaces
MIT License
1.48k stars 60 forks source link

Custom type converters #80

Closed oshustov closed 3 years ago

oshustov commented 3 years ago

Here is implementation via built in System.ComponentModel.TypeConverter and System.ComponentModel.TypeDescriptor types.

User have to define type and simple converter for it: image

Then bind the converter to the type via CliApplicationBuilder: image

Pros: -validation support -simple in implementation -flexible (user can inherit directly from TypeConverter, but it can be difficult and maybe dangerous) -no custom attributes or other settings required

Cons: -using of the built in type (which adds an attribute in runtime) potentionaly might make some issues with other stuff which are using also this mechanism to convert object types

Closes #41

Tyrrrz commented 3 years ago

Hey, finally got time to review this.

The TypeDescriptor approach looks scary indeed. How about we instead extend CommandParameterAttribute/CommandOptionAttribute to accept an optional Type? ConverterType? This way the user can control conversion on per-type basis. For now, let's not bother with a global configuration for this. Using the standard base converter type is probably not required, as it's unlikely that some existing conversion will be useful in our scenarios. What do you think?

oshustov commented 3 years ago

Hey, thanks for the review. You are right, that approach was the next thing I thought about. I going to implement it and create a new PR instead of this one.

Tyrrrz commented 3 years ago

Closing in favor of #81